Like Tree3Likes

Printing the size of a character

This is a discussion on Printing the size of a character within the C Programming forums, part of the General Programming Boards category; Why doesn't the following code report the size of '3' as 1 byte as '3' is a character and not ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    385

    Printing the size of a character

    Why doesn't the following code report the size of '3' as 1 byte as '3' is a character and not an integer in this case??

    Code:
    void main()
    {
    printf("%d",sizeof('3'));
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Single quotes indicate a single character.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    That's exactly what I meant. Single quotes should mean single charater and print out the size of '3' as 1 byte, but it inturn prints out 2..

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by juice View Post
    Why doesn't the following code report the size of '3' as 1 byte as '3' is a character and not an integer in this case??

    Code:
    void main()
    {
    printf("%d",sizeof('3'));
    }
    Oh my.. a 2 line program with 3 errors in it...

    1) It's not void main() ... it's int main (void) ... and yes it does matter.

    2) Since main returns an int, you must return an errorlevel at the end of main... usually return 0; is adequate.

    3) '3' is a character literal, it's stored in the program executable image... sizeof() is returning the size of the *pointer* to the location where it's stored.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by CommonTater View Post
    3) '3' is a character literal, it's stored in the program executable image... sizeof() is returning the size of the *pointer* to the location where it's stored.
    Well actually '3' is just an integer value, so if you are getting 2, it's because you (he) have (has) a 25 year old compiler with 16 bit ints.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by juice View Post
    That's exactly what I meant. Single quotes should mean single charater and print out the size of '3' as 1 byte, but it inturn prints out 2..
    2??? It should print 4 ... unless you are on some crappy pre-flintstone 16 bit compiler like Turbo C...

    It sizeof(pointer) is returning 2 you seriously need to update your compiler... I generally suggest Pelles C

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    printf( "%c %d", '3', '3' );
    Double quotes (a string) would give you a string literal, but since he has single quotes, I believe he's just getting an integer value. (The integer value of the character 3. Ascii Table - ASCII character codes and html, octal, hex and decimal chart conversion)


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by quzah View Post
    Code:
    printf( "%c %d", '3', '3' );
    Double quotes (a string) would give you a string literal, but since he has single quotes, I believe he's just getting an integer value. (The integer value of the character 3. Ascii Table - ASCII character codes and html, octal, hex and decimal chart conversion)
    Quzah.
    Hmmm... I think you may be right... string literalls are usually stored in the data segment of the PE image, but it would be simple as pie to store character literals as ints right in the code... Or might this be an implementation defined thing?

    In any case 2 is a disastrous answer for our friend...

  9. #9
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    It reports 2 bytes with Turbo C, and 1 byte with Micrsoft Visual C++ express edition.

  10. #10
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    993
    It reports 2 bytes with Turbo C, and 1 byte with Micrsoft Visual C++ express edition.
    In C, '3' is an int. Any character constant (with a single character) has type int, not char, so sizeof('3') is the same as sizeof(int). However, C++ fixed/changed this, so character constants have type char. That's why you get a difference.
    Salem likes this.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Leave it to microsoft to do things their own --very unusual-- way.

    I get 4 back from Pelles C which is C-99 standards compliant.

  12. #12
    kotin
    Join Date
    Oct 2009
    Posts
    132
    Hi Juice,

    i fell that '3' is treat as integer in sizeof('3'). wt you are expecting output -1 will get if you type cast that one. like sizeof( (char) ('3'));

  13. #13
    kotin
    Join Date
    Oct 2009
    Posts
    132
    Hi Juice,

    i fell that '3' is treat as integer in sizeof('3'). wt you are expecting output -1 will get if you type cast that one. like sizeof( (char) ('3'));

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,306
    Quote Originally Posted by CommonTater
    3) '3' is a character literal, it's stored in the program executable image... sizeof() is returning the size of the *pointer* to the location where it's stored.
    Actually, no, cas is correct. It is true that '3' is a character literal. However, it is not true that "sizeof() is returning the size of the *pointer* to the location where it's stored". Rather, the result of sizeof is the size of '3', which is equivalent to sizeof(int) since character literals are of type int.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,854
    > Leave it to microsoft to do things their own --very unusual-- way.
    Microsoft did nothing wrong - it's purely the difference between C and C++.
    Incompatibilities Between ISO C and ISO C++
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing charcter by character
    By Ron in forum C Programming
    Replies: 8
    Last Post: 06-15-2008, 10:39 PM
  2. printing cd covers the right size?!
    By Leeman_s in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 12-31-2003, 07:03 PM
  3. Printing single character from a string
    By TJJ in forum C Programming
    Replies: 4
    Last Post: 11-05-2003, 06:25 PM
  4. Trouble Printing ASCII Character
    By drdroid in forum C++ Programming
    Replies: 2
    Last Post: 04-27-2002, 09:04 PM
  5. Printing a Character from Binary
    By SavesTheDay in forum C Programming
    Replies: 6
    Last Post: 02-19-2002, 02:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21