C / C ++ operator sizeof: Why sizeof ('a') returns different values?

Possible duplicate:
Character size ('a') in C / C ++

I am new to C and have been embarrassed by this.

C: I tried printing sizeof ('a') in C using the modifier '% zu' and it prints the value 4.

C++: Printing sizeof ('a') in C ++ using cout and printf (using the format above) displays a value of 1.

I believe that the correct value should be 1, since "a" will be considered char. Why doesn't it return 4 in C? Are the sizes of the operations of both in both languages? If so, what is the difference and why does it return a different value? I used gcc compilers in both cases.

+5
source share
2 answers

C 'a' , , 4, ++ char. :

('a') C/++

+11

C () int. ,

#include <stdio.h>

main(int argc, char *argv[])
{
  printf("%zu\n", sizeof('a'));
  printf("%zu\n", sizeof('ab'));
  printf("%zu\n", sizeof('abc'));
  printf("%zu\n", sizeof('abcd'));

  printf("%u\n", 'a');
  printf("%u\n", 'ab');
  printf("%u\n", 'abc');
  printf("%u\n", 'abcd');

  printf("%x\n", 'a');
  printf("%x\n", 'ab');
  printf("%x\n", 'abc');
  printf("%x\n", 'abcd');

  printf("%c\n", 'a');
  printf("%c\n", 'ab');
  printf("%c\n", 'abc');
  printf("%c\n", 'abcd');
}

, 4 == sizef (int), , gcc (Ubuntu 4.4.3-4ubuntu5.1) 4.4. 3. , :

warning: multi-character character constant

, , int, , . 0. , printf

97
24930
6382179
1633837924
61
6162
616263
61626364

literal ( ASCII ): 'a' 0x61).

, :

a
b
c
d

. , printf int char.

++ , char, int.

#include <iostream>

using namespace std;

main(int argc, char *argv[])
{
  cout << sizeof('a') << endl;
  cout << sizeof('ab') << endl;
  cout << sizeof('abc') << endl;
  cout << sizeof('abcd') << endl;

  cout << 'a' << endl;
  cout << 'ab' << endl;
  cout << 'abc' << endl;
  cout << 'abcd' << endl;
}

GCC . C:

1
4
4
4
a
24930
6382179
1633837924

, char, int.

32- Linux, int 4 . , , . 64- .

( ): int C, int.

+4

All Articles