C is actually not simple: sizeof

Source: Internet
Author: User

Q: How many keywords are there in C language?

A: 32.

It's okay to answer, it's normal, we play the art of the program, not the back number. But this special digital 1<<5 is also very good to remember-.-。


Q: Is sizeof a function or a keyword?

The first time you see this problem, you may feel a little puzzled, and sizeof is certainly a function, because sizeof is followed by a parenthesis, which is clearly a symbol of the function. But since I asked, you might have guessed thatsizeof is not a function, it's a keyword! the "standard usage" of sizeof has long caused sizeof to be easily understood as a function. Try running the following code:

#include <stdio.h>int main () {    int num = 5;    printf ("%d\n", sizeof (num));    printf ("%d\n", sizeof (int));    printf ("%d\n", sizeof num);    GetChar ();    return 0;}
sizeof num can actually output 4 correctly, so it's really not a function. However, sizeof int is wrong, and it produces the following error:

error:expected primary-expression before "int"
Because int can only be preceded with auto, unsigned, and so on, moreover sizeof int how to understand? Do you want to calculate the size? Or do you want to represent an int variable of type "sizeof"?

Access to the relevant information is summarized as follows:

When you want to calculate the size of a variable, such as a variable of a primitive type (int, double), struct variable (instance), you can use no parentheses,

When you want to calculate a type, you cannot omit parentheses.

No matter what the occasion is to add a parenthesis, one is unity, but can make the program more readable, and not easy to error. My purpose is not to omit this parenthesis, but to omit it and not to make things better. What I want to express is the other side of sizeof: because now we know that sizeof is not a function but a keyword, so its nature is completely different. The function is determined at run time, and the keyword is determined at compile time. difference, try running the following program:

#include <stdio.h>int main () {    int cc[10];    printf ("%d\n", sizeof cc[999999999]);    GetChar ();    return 0;}
The above program does not go wrong, but it outputs 4 correctly. Not because sizeof really looked for the memory that was offset 999,999,999 from the CC's first address, as described above, sizeof is determined at compile time, so it determines the size based on the type of cc[x]. Because sizeof does not have access to these addresses, there is no problem with the program syntax above, but logically it seems to be of little practical significance.

There is another area to be aware of about sizeof. Try the following program in your mind to get a result and run it later:

#include <stdio.h> #define Size_of_array (sizeof (ARRAY)/sizeof (ARRAY[0)) int main () {    int array[] = {1, 2, 3, 4, 5};    for (int d =-1; d < (size_of_array-1); d++)        printf ("%d\n", array[d+1]);        printf ("end\n");    GetChar ();    return 0;}
Is it different from what you think? Why doesn't the array have an output? The reason is that sizeof returns unsigned int, when int is to be converted to unsigned int when compared with unsigned int, the cast is naturally tragic when int <0, and the result is MaxValue ( unsigned int)-abs (int), then when d =-1, the nature turns into a fairly large number, and the for loop will of course not be executed.

#include <stdio.h>int main () {    int a =-1;    unsigned int b = 10000;    if (a > B)        printf ("a > b\n");    else        printf ("A < b\n");        A = (unsigned int) A;    printf ("%u\n", a);  unsigned int to use%u output instead of being used to write%d        getchar ();    return 0;}

Finally, since we just mentioned the maximum value of unsigned int, what do I do if I want to output int or unsigned int in c? Here's a simple tip:

#include <stdio.h>int main () {    printf ("Max value of unsigned int is:%u\n", ~ (unsigned int) 0);       The result can only be used with%u for    printf ("Max Value of int is:%d\n", ~ (unsigned int) 0 >>1);   Results with%d,%u indicated that all can be     getchar ();    return 0;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.