Like Tree1Likes
  • 1 Post By laserlight

printf("%d\n", sizeof(char*));

This is a discussion on printf("%d\n", sizeof(char*)); within the C Programming forums, part of the General Programming Boards category; Code: printf("%d\n, sizeof(char*)); My output was 4 is this compiler specific or just a general default?...

  1. #1
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    407

    printf("%d\n", sizeof(char*));

    Code:
    printf("%d\n, sizeof(char*));
    My output was 4 is this compiler specific or just a general default?

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    4
    It's compiler/CPU specific, but 4 is common nowadays.

    On a 16bit machine, the output would be 2.
    On a 64bit machine, the output would (probably) be 8.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,159
    The sizeof a pointer is implementation defined.
    Lesshardtofind likes this.
    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

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,531
    Also, since sizeof() yields a result of type size_t, it is probably a good idea to convert the result to int before using %d. Little benefits like avoiding undefined behaviour.....
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy in reply to you, it is likely you deserve it. Suck it up, sunshine, and read this, this, and this before posting again.

  5. #5
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    407
    Also, since sizeof() yields a result of type size_t, it is probably a good idea to convert the result to int before using %d. Little benefits like avoiding undefined behaviour.....
    Yea my program did both methods and a few others both casting and not casting yeilded the same result, but I was skeptical of all results which is why I posed the question. The implementation define phrase got me the right thing to google.

    The full print was
    Code:
        printf("%d\n", (int)sizeof(char));
        printf("%i\n", (int)sizeof(char));
        printf("%f\n", (float)sizeof(char));
        printf("%d\n", sizeof(char));
        printf("%i\n", sizeof(char));     
        printf("%f\n", sizeof(char));
        
        printf("%d\n", (int)sizeof(char*));
        printf("%i\n", (int)sizeof(char*));
        printf("%f\n", (float)sizeof(char*));
        printf("%d\n", sizeof(char*));
        printf("%i\n", sizeof(char*));
        printf("%f\n", sizeof(char*));
    OUTPUT:
    1
    1
    1.000000
    1
    1
    1.000000
    4
    4
    4.000000
    4
    4
    4.000000

    in DevC++ 32 bit on a 64 bit OS.
    I figured the answer would change on the pointer but I wasn't sure. And it seemed like
    Code:
    malloc(sizeof(char *));
    Would allocate a really small number of bytes for how long a string could be.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,531
    malloc(sizeof(char *)) is NOT the way to allocate storage to hold a string. It allocates a pointer to char. A pointer to char and a string are VERY different things. Even if, sometimes, they can be operated on in similar ways,

    The usual technique to allocate storage for a C-style string is
    Code:
         char *s = malloc(sizeof(char)*(strlen("Hello") + 1));
         strcpy(s, "Hello");
    In this example, strlen("Hello") will yield 5, the "+1" is needed since a string literal "Hello" is composed of 6 char's (the five letters, plus a trailing character with value zero). It is needed so strcpy(), which copies characters up to and including that trailing zero, will work.

    Computing sizeof(char) (as opposed to "sizeof(char *)" which you are using incorrectly) is optional, since the standard specifies that sizeof(char) is 1. But it doesn't hurt.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy in reply to you, it is likely you deserve it. Suck it up, sunshine, and read this, this, and this before posting again.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    There's a format modifier in C99 that allows you to print size_t's directly: it's z.
    Code:
    printf("%zu\n", sizeof(char*));
    Generally all pointer sizes will be the same and will depend on the word-size of your machine (as mentioned, 2 bytes => 16 bit, 4 bytes => 32 bit, 8 bytes => 64 bit). The standard doesn't specify this however, so it could be anything really.

    Most of the time you don't worry too much about how many bytes things take up, that's why you can say sizeof(type) in the first place. Particularly when dynamically allocating memory, if you want ten Foos you can just say
    Code:
    Foo *foos = malloc(sizeof(Foo) * 10);
    Also, a common idiom when allocating memory is to use the sizeof the variable directly rather than the sizeof its type:
    Code:
    Foo *foos = malloc(sizeof(*foos) * 10);
    This way the type of foos can change and the malloc bit will automatically take the sizeof the new type.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "sizeof" character strings - output issue
    By bunko in forum C Programming
    Replies: 3
    Last Post: 12-03-2008, 06:10 PM
  2. A funny code snippet of "sizeof()"
    By meili100 in forum C++ Programming
    Replies: 4
    Last Post: 05-19-2008, 02:09 AM
  3. Replies: 6
    Last Post: 10-15-2007, 09:05 AM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM

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