Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confusion about sizeof operator in C

I'm confused about sizeof operator in C.

#include <stdio.h>

int main(void)
{
    char num1=1, num2=2, result;
    result = num1 + num2;
    printf("Size of result: %d \n",sizeof result);
    printf("Size of result: %d \n",sizeof(num1+num2));
}

The results are 1 and 4 respectively. Why does this happen?

like image 801
Jin Avatar asked Jul 14 '16 07:07

Jin


People also ask

What can I use instead of sizeof in C?

Re: Alternative for sizeof operatorNo there is not alternative to sizeof() operator in standard ANSI C. Sizeof() is a compiletime operator can be applied to type too ,i.e. sizeof(int); runtime not call any function so sizeof() is very quick. Why do you will not use sizeof() operator?

Is sizeof a valid operator in C?

Sizeof is a much used operator in the C or C++. It is a compile time unary operator which can be used to compute the size of its operand. The result of sizeof is of unsigned integral type which is usually denoted by size_t.

What is the purpose of the sizeof operator in C?

We generally use the sizeof() operator in the C language so that we can determine the total size of the data type or the expression that is specified in the storage units of char-size. The sizeof() operator consists of just one operand that can either be a cast data type or an expression.

How do I find my size without using the sizeof operator?

The idea is to use pointer arithmetic ( (&(var)+1) ) to determine the offset of the variable, and then subtract the original address of the variable, yielding its size. For example, if you have an int16_t i variable located at 0x0002 , you would be subtracting 0x0002 from 0x0006 , thereby obtaining 0x4 or 4 bytes.


3 Answers

TL;DR answer:

  • sizeof result is same as sizeof(char).
  • sizeof(num1+ num2) is same as sizeof (int) why?

In your case, they produce 1 (guaranteed by standard) and 4 (may vary), respectively.

That said, sizeof produces a result of type size_t, so you should %zu format specifier to print the value.


Why:

First, for the addition operator +, quoting C11, chapter §6.5.6

If both operands have arithmetic type, the usual arithmetic conversions are performed on them.

Regarding usual arithmetic conversions, §6.3.1.8/p1

[....] Otherwise, the integer promotions are performed on both operands.[...]

and then from §6.3.1.1,/p2,

If an int can represent all values of the original type (as restricted by the width, for a bit-field), the value is converted to an int; otherwise, it is converted to an unsigned int. These are called the integer promotions.

So, sizeof(num1+num2) is the same as sizeof(int).

like image 119
Sourav Ghosh Avatar answered Oct 17 '22 22:10

Sourav Ghosh


result is of char type, therefore sizeof is giving 1 while num1+num2 promoted to int type and therefore it gives 4 (size of int).

Note that when an arithmetic operation is performed on a type smaller than that of int and all of it's value can be represented by int then result will be promoted to int type.

like image 28
haccks Avatar answered Oct 17 '22 21:10

haccks


num1 + num2 is becoming integer and hence the output is 4 whereas result is char which outputs 1.

You can refer this article Integer Promotion:

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. These are called the integer promotions. All other types are unchanged by the integer promotions.

like image 27
Rahul Tripathi Avatar answered Oct 17 '22 20:10

Rahul Tripathi