+ 1

Find the output of following

#include <stdio.h>    int main() {     char a = 30;     char b = 40;     char c = 10;     char d = (a * b) / c;     printf ("%d ", d);        return 0; }

13th Jun 2018, 9:41 AM
RAVI RANJAN
RAVI RANJAN - avatar
5 odpowiedzi
+ 6
As an important sidenote regarding char type, you must bear in mind that there are 3 kinds of char type in C/C++. 1. signed char : for representing an integral values in the range of [-128, 127] 2. unsigned char : use for representing an integral value in the range of [0, 255] 3. char : for representing a single ASCII character. You shouldn't use it for numerical proposes since its modifier (signed or unsigned) is implementation-defined and as a result, is unreliable for that matter.
13th Jun 2018, 12:28 PM
Babak
Babak - avatar
+ 4
Immortal🌼 You're welcome, dear. RAVI KUMAR Well, if you're willing to squeeze poor little 8bit character to piss bigger output or you are passionate to experiment with famous overflow odometer and make everyone freak out or better than that giving the full authority to sneaky compiler, then everything else would be crap!
13th Jun 2018, 12:55 PM
Babak
Babak - avatar
0
ANS OF THIS QUESTION : At first look, the expression (a*b)/c seems to cause arithmetic overflow because signed characters can have values only from -128 to 127 (in most of the C compilers), and the value of subexpression ‘(a*b)’ is 1200. For example, the following code snippet prints -80 on a 32 bit little endian machine. char d = 1200; printf ("%d ", d); Arithmetic overflow doesn’t happen in the original program and the output of the program is 120. In C, char and shortare converted to int for arithmetic calculations. So in the expression ‘(a*b)/c’, a, b and c are promoted to int and no overflow happens.
13th Jun 2018, 12:43 PM
RAVI RANJAN
RAVI RANJAN - avatar