5

I tried to run the following program in C and got some output. Can you help me out why???

#include<stdio.h>

int main()
{
  char x='A';
  printf("%d%d%d",sizeof("3"),sizeof('3'),sizeof(3));
  return 0;
}

The output received is 2 4 4 using gcc in ubuntu 11.04 32 bit.

Similarly in other program:-

#include<stdio.h>

int main()
{
  char x='A';
  printf("%d%d",sizeof('A'),sizeof(x));
  return 0;
}

The output received is 4 1 using GCC in ubuntu 11.04 32 bit.

Can you help me out why the output is this way???

John Dibling
  • 99,718
  • 31
  • 186
  • 324
Rahul
  • 2,515
  • 3
  • 25
  • 29
  • 6
    The tags are misleading, as this doesn't apply to C++. In C, a char literal is an `int`. In C++, it's a `char`. – David Thornley Oct 06 '11 at 18:05
  • 1
    Check this out: http://stackoverflow.com/questions/1392200/sizeof-string-literal – wkl Oct 06 '11 at 18:05
  • Note that when the argument to `sizeof` is not a type (as in all your examples), the parentheses are redundant: `sizeof 3`, `sizeof "3"`, ... are all ok. The parenthesis are redundant here too: `x = ((4) * (10)) + (2);` – pmg Oct 06 '11 at 18:30
  • 1
    @pmg: and they're redundant here, `(4 + (2 * 6) + 8) >> 2`. But personally I don't like the look of `4 + 2 * 6 + 8 >> 2`, and I often put redundant parens in C code. Not that it makes any difference in this case, but `sizeof` has very high precedence, so by habit I always use parens with it. – Steve Jessop Oct 06 '11 at 18:52

5 Answers5

16

In C, char literals are of integer type, so sizeof('3') == sizeof(int). See this C FAQ for details.

This is one of the areas where C and C++ differ (in C++ sizeof('3') is 1).

Actually, correcting my previous assertion, sizeof("3") will yield 2 because "3" is treated as a 2-element character array.

6.3.2.1/3

Except when it is the operand of the sizeof operator or the unary & operator, or is a string literal used to initialize an array, an expression that has type ‘‘array of type’’ is converted to an expression with type ‘‘pointer to type’’ that points to the initial element of the array object and is not an lvalue.

cnicutar
  • 178,505
  • 25
  • 365
  • 392
  • 6
    `sizeof("3")` is performing a string-literal sizeof, which is 2 because it's the length of the literal (plus `\0`). – wkl Oct 06 '11 at 18:06
  • @AtoMerZ: because the C standard says so. AFAIK this is for historical reasons, and because nobody ever wanted to make a breaking change. – Steve Jessop Oct 06 '11 at 18:08
  • 1
    @cnicutar: of course. 6.3.2.1/3 of C99, 'except when it is the operand of the `sizeof` operator... an expression that has type "array of *type*" is converted to an expression with type "pointer to *type*"'. – Steve Jessop Oct 06 '11 at 18:10
  • @Steve Jessop Absolutely awesome! – cnicutar Oct 06 '11 at 18:10
  • @Steve - thanks, I was going through the standard to find the line: The part right before yours is: "Except when it is the operand of the sizeof operator or the unary & operator, or is a string literal used to initialize an array, an expression that has type ‘‘array of type’’ is converted to an expression with type ‘‘pointer to type’’", to clarify why `sizeof("literal")` doesn't return size of the pointer. – wkl Oct 06 '11 at 18:13
4

In C

3: integer literal, type int
'3': char literal, type int
"3": string literal: type char[2]

In your second example, x denotes an object of type char.

pmg
  • 106,608
  • 13
  • 126
  • 198
2

The '3' is converted to type int, which is 4 bytes. However "3" is a string with two bytes. The first byte is the char 3 and the second is the null terminator that gets appended to all strings.

Alfred Fazio
  • 956
  • 7
  • 10
1

"C" is

char mystring[2];
mystring[0] = 'C';
mystring[1] = '\0';

While 'C' is

int mychar;
mychar = 'C';
Francesco Belladonna
  • 11,361
  • 12
  • 77
  • 147
0

'3' is a character constant. In C, character constants have type int (in C++, they have type char). Thus, sizeof '3' == sizeof (int).

"3" is a string literal. It is a 2-element array of char (const char in C++) with the values {'3', '\0'}. sizeof (char) == 1 by definition, thus, sizeof "3" == 2.

3 is an integer constant. It has type int. Thus, sizeof 3 == sizeof (int).

The variable x is declared as char, thus sizeof x == 1.

John Bode
  • 119,563
  • 19
  • 122
  • 198