Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiplying char and int together in C

Today I found the following:

#include <stdio.h>

int main(){
char x = 255;
int z = ((int)x)*2;

printf("%d\n", z); //prints -2

return 0;

}

So basically I'm getting an overflow because the size limit is determined by the operands on the right side of the = sign??

Why doesn't casting it to int before multiplying work?

In this case I'm using a char and int, but if I use "long" and "long long int" (c99), then I get similar behaviour. Is it generally advised against doing arithmetic with operands of different sizes?

like image 359
14 revs, 12 users 16% Avatar asked Apr 16 '10 00:04

14 revs, 12 users 16%


People also ask

Can I multiply char with int in C?

char is a numeric type, same as int but shorter. It holds a numerical representation of the symbol (ASCII code). Multiplying it with an integer gives you an integer.

Can we multiply int with string?

When you multiply a string by an integer, Python returns a new string. This new string is the original string, repeated X number of times (where X is the value of the integer). In the following example, we're going to multiply the string 'hello' by a few integers.

How do you multiply string to int?

To (properly) multiply an string by an integer, you split the string into characters, repeat each character a number of times equal to the integer, and then stick the characters back together. If the integer is negative, we use its absolute value in the first step, and then reverse the string.

Can we multiply characters in C++?

C++ Multiplication of two Chars You can multiply two characters using multiplication operator.


4 Answers

char can be either signed or unsigned, depending on your compiler.

In your case, it appears to be signed, and 255 is outside the range it can represent (likely, it can only represent numbers from -128 to 127).

So the problem occurs when you assign 255 to your char variable - this results in an implementation-defined value, which in your case, appears to be -1.

When you multiply -1 by 2, you get -2. No mystery there. The cast to (int) does nothing - types narrower than int are always promoted to int or unsigned int before any calculations are done with them.

like image 166
caf Avatar answered Oct 05 '22 21:10

caf


It appears that char is signed on your platform. So the char x = 255 is effectively the same as char x = -1. The cast to int doesn't matter.

Try changing that to:

unsigned char x = 255;
like image 38
R Samuel Klatchko Avatar answered Oct 05 '22 21:10

R Samuel Klatchko


No, you are not getting an overflow on the second line (multiplication). The problem is your compiler is using signed char by default and 255 overflows and will mean -1. Basically, you are initializing variable x with the value of -1. Casting -1 to int will result in -1 (signed operands will get sign-extended in upcasts whereas unsigned operands will get zero-extended).

You can force the char to be unsigned by adding the unsigned prefix:

unsigned char x = 255;
like image 32
mmx Avatar answered Oct 05 '22 19:10

mmx


The other answers nicely explain how your example "works", so I will not explain that again.

However, let me observe that if what you want to use is an "unsigned 8 bit integer", just use <stdint.h>'s uint8_t already (and its 16, 32, 64bit companions) and keep away from all the chars, shorts and ints in this world.

like image 30
ndim Avatar answered Oct 05 '22 20:10

ndim