Consider the following code:
void f(byte x) {print("byte");}
void f(short x) {print("short");}
void f(int x) {print("int");}
void main() {
byte b1, b2;
short s1, s2;
f(b1 + b2); // byte + byte = int
f(s1 + s2); // short + short = int
}
In C++, C#, D, and Java, both function calls resolve to the "int" overloads... I a...
I have a query about data type promotion rules in C language standard.
The C99 says that:
C integer promotions also require that "if an int can represent all values of the original type, the value is converted to an int; otherwise, it is converted to an unsigned int."
My questions is in case of a C language expression where unsigned in...
I'm trying to figure out if the C Standard (C90, though I'm working off Derek Jones' annotated C99 book) guarantees that I will not lose precision multiplying two unsigned 8-bit values and storing to a 16-bit result. An example statement is as follows:
unsigned char foo;
unsigned int foo_u16 = foo * 10;
Our Keil 8051 compiler (v7.50 ...
I learned from ----As to when default promotions kick in: default argument promotions are used exactly when the expected type of the argument is unknown, which is to say when there's no prototype or when the argument is variadic.
But an example confusing me is:
void func(char a, char b)
{
printf("a=%p,b=%p\n",&a,&b);
}
int mai...
Hi!
I'm reading something about overload resolution and I found something that bothers me...In the following code:
int const& MaxValue(int const& a, int const& b)
{
return a > b ? a : b;
}
void SomeFunction()
{
short aShort1 = 3;
short aShort2 = 1;
int const & r2 = MaxValue(aShort1, aShort2); // integral promotion
...
Normally, C requires that a binary operator's operands are promoted to the type of the higher-ranking operand. This can be exploited to avoid filling code with verbose casts, for example:
if (x-48U<10) ...
y = x+0ULL << 40;
etc.
However, I've found that, at least with gcc, this behavior does not work for bitshifts. I.e.
int x = 1;
u...
When comparing a type larger than int, with an integer constant, should I place the constant on the left or the right to ensure the correct comparison is performed?
int64_t i = some_val;
if (i == -1)
or should it be:
if (-1 == i)
Are there any circumstances in which either case is not identical to comparison with -1LL (where int64_...
main() {
if ( -1 < (unsigned char) 1 )
printf("less than");
else
printf("NOT less than");
}
what is happening?
(unsigned char)1 converted to (signed char)1
then: (signed)-1 < (signed)1 thus answer "less than"
problem:-
in above code change if ( (-1 < (unsigned int) 1 )
answer "NOT less than".
So its obvious ...
#include<stdio.h>
int main(void)
{
char c = 0x80;
printf("%d\n", c << 1);
return 0;
}
The output is -256 in this case. If I write c << 0 then the output is -128.
I don't understand the logic behind this code.
...