Extra
Literals
What is a literal in C?For example:
#include <stdio.h>
int main(){
int a = 42; // 42 here is a literal
int b = -15213; // 15213 here is a literal, and '-' is an operator
printf("Hello, world!\n"); //"Hello, world!\n" here is also a literal
}
If you are not familiar with this concept, the article below could help:
Noting that literals also have type, 42 is an int for example.
Consider what are the types of the literals below:
0xFFFFFFFF
2147483647
0x80000000
2147483648
Check your answers...
unsigned
int
unsigned
long
Get confused?The following table may help:
Now you should be able to explain why we use:
-2147483647 - 1
to represent INT_MIN,rather than:
0x80000000 or -2147483648
Type Casting
Type castings are very common in C programming language, most of which are implicit.
int main(){
int i = 42;
unsigned ui = 37;
long l = i * ui;
// equal to: long l = (long)((unsigned) i * ui);
}
If you are not fimiliar with type casting, read this short article:
C- TypeCasting - GeeksforGeeks
Now consider this C program below:
#include <stdio.h>
int main(){
char ci = 0xE9, cj = 0xD5;
int i = 0xE9, j = 0xD5;
int product_c = ci * cj;
int product = i * j;
printf("Product of chars is: 0x%x\n", product_c);
printf("Product of ints is: 0x%x\n", product);
}
What is the output of this program? Try to compile and run it and check your answer. If you are wrong, you could ask your AI agents “what is the ‘Integral Promotion’ in C?” to find out why.
The linking below delivers a very detailed explanation about implicit type casting (or conversion in CPP):