integer-promotion

Bitshift and integer promotion?

旧时模样 提交于 2019-11-26 08:23:00
问题 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; unsigned long long y = x << 32ULL; I would expect the type of the right-hand operand to cause the left-hand operand to be promoted to unsigned long long so that

Addition of two chars produces int

怎甘沉沦 提交于 2019-11-26 06:43:25
问题 I\'ve made a simple program and compiled it with GCC 4.4/4.5 as follows: int main () { char u = 10; char x = \'x\'; char i = u + x; return 0; } g++ -c -Wconversion a.cpp And I\'ve got the following: a.cpp: In function ‘int main()’: a.cpp:5:16: warning: conversion to ‘char’ from ‘int’ may alter its value The same warning I\'ve got for the following code: unsigned short u = 10; unsigned short x = 0; unsigned short i = u + x; a.cpp: In function ‘int main()’: a.cpp:5:16: warning: conversion to

Type conversion - unsigned to signed int/char

妖精的绣舞 提交于 2019-11-26 01:49:09
问题 I tried the to execute the below program: #include <stdio.h> int main() { signed char a = -5; unsigned char b = -5; int c = -5; unsigned int d = -5; if (a == b) printf(\"\\r\\n char is SAME!!!\"); else printf(\"\\r\\n char is DIFF!!!\"); if (c == d) printf(\"\\r\\n int is SAME!!!\"); else printf(\"\\r\\n int is DIFF!!!\"); return 0; } For this program, I am getting the output: char is DIFF!!! int is SAME!!! Why are we getting different outputs for both? Should the output be as below ? char is

Type conversion - unsigned to signed int/char

試著忘記壹切 提交于 2019-11-26 00:56:02
I tried the to execute the below program: #include <stdio.h> int main() { signed char a = -5; unsigned char b = -5; int c = -5; unsigned int d = -5; if (a == b) printf("\r\n char is SAME!!!"); else printf("\r\n char is DIFF!!!"); if (c == d) printf("\r\n int is SAME!!!"); else printf("\r\n int is DIFF!!!"); return 0; } For this program, I am getting the output: char is DIFF!!! int is SAME!!! Why are we getting different outputs for both? Should the output be as below ? char is SAME!!! int is SAME!!! A codepad link . This is because of the various implicit type conversion rules in C. There are

Why must a short be converted to an int before arithmetic operations in C and C++?

喜夏-厌秋 提交于 2019-11-25 22:46:30
问题 From the answers I got from this question, it appears that C++ inherited this requirement for conversion of short into int when performing arithmetic operations from C. May I pick your brains as to why this was introduced in C in the first place? Why not just do these operations as short ? For example ( taken from dyp\'s suggestion in the comments ): short s = 1, t = 2 ; auto x = s + t ; x will have type of int . 回答1: If we look at the Rationale for International Standard—Programming