Thread: sizeof on string literal defined in #define VS char*

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    228

    sizeof on string literal defined in #define VS char*

    Consider the following:
    Code:
    #define STR "some long string"
     
    int main(int argc, char* argv[]) {
        char c* = "some long string";
        printf("%d\n", sizeof(c))
        printf("%d\n", sizeof(STR));
        return 0;
    }
    This prints out:
    8
    17
    What’s the explanation for the difference?
    Shouldn't I expect the same result in both cases?

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    3,547
    Use strlen instead of sizeof to get length of C-strings (zero byte terminated char arrays).

    Note: The size returned does not count the zero byte at the end.

    Tim S.
    Last edited by stahta01; 02-11-2019 at 03:41 AM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    26,527
    Recall that c is a pointer, whereas STR is an array of char including the terminating null character.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,030
    Also compare with
    Code:
        char c[] = "some long string";
        char c[100] = "some long string";
    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.

  5. #5
    Registered User john.c's Avatar
    Join Date
    Dec 2017
    Posts
    593
    It should also be mentioned that it is incorrect to use "%d" to print the return value of the sizeof operator unless you also explicitly cast the return value to an int. sizeof returns size_t which is not only unsigned but can be 64 bits even when int is 32 bits. If that's the case on your system then the only reason you get the correct output is because your machine is little endian so that the low 32-bits comes first.

    So to print the value of sizeof you should either use the correct format spec, "%zu", or cast it to int:
    Code:
    #include <stdio.h>
    #include <string.h>
     
    int main() {
        char str[100] = "a string";  // Salem's example
     
        printf("zu: %zu\n", sizeof str);
        printf(" d: %d\n", (int)sizeof str);
     
        printf("\nstrlen: %zu\n", strlen(str)); 
         
        printf("\nNumber of bytes (chars) in:\n");
        printf("  int:    %zu\n", sizeof(int));
        printf("  size_t: %zu\n", sizeof(size_t));
     
        printf("\nCheck endianness:\n");
        int n = 1;
        printf("  %s endian\n", *(char*)&n ? "little" : "big");
     
        return 0;
    }
    Let him who is not come to logic be plagued with continuous and everlasting filth.
    - John of Salisbury, 1160

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 06-20-2016, 07:49 AM
  2. User defined literal
    By cstryx in forum C++ Programming
    Replies: 4
    Last Post: 02-16-2015, 05:49 PM
  3. create user define literal?
    By thetinman in forum C++ Programming
    Replies: 1
    Last Post: 07-14-2014, 03:51 AM
  4. Most proper way to convert string literal to char*
    By Epy in forum C++ Programming
    Replies: 15
    Last Post: 09-18-2013, 07:11 AM
  5. char* ptr="HELLO"; String Literal:Stack/Heap/Data Segment
    By forumuser in forum C Programming
    Replies: 9
    Last Post: 09-20-2007, 04:53 AM

Tags for this Thread