The C language is not simple: sizeof

Source: Internet
Author: User

The C language is not simple: sizeof

Q: How many keywords are in C language?

Answer: 32.

It doesn't matter if you cannot answer the question. Normally, we are playing the art of the program, rather than carrying numbers. However, this special number 1 <5 is also very memorable -.-.


Q: Is sizeof a function or a keyword?

When you see this problem for the first time, you may find it a bit confusing. sizeof is a function, because sizeof is followed by parentheses. This is obviously a symbol of a function. But now that I have asked this question, you may have guessed that sizeof is not a function and it is a keyword! For a long time, because of sizeof's "standard usage", sizeof is easily understood as a function. Run the following code:

#include 
 
  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 output 4 correctly. It can be seen that it is really not a function. However, sizeof int is incorrect. It produces the following error:

error: expected primary-expression before "int"
Because int can only be followed by auto, unsigned, etc. Besides, how can we understand sizeof int? I want to calculate the size? I still want to represent the int variable of the "sizeof" type.

The following is a summary after reading the relevant information:

When you want to calculate the size of a variable, such as a variable of the basic type (int, double) and a struct variable (instance), no parentheses are required,

Brackets cannot be omitted when a type is to be calculated.

In any case, a bracket is added. The first is unity, which makes the program more readable and error-prone. My goal is not to omit this bracket, but to omit it will not make things better. What I want to express is sizeof, on the other hand: because we now know that sizeof is not a function but a keyword, its nature is completely different. The function is determined at runtime, while the keywords are determined at compilation. Try to run the following program:

#include 
 
  int main(){    int cc[10];    printf("%d\n", sizeof cc[999999999]);    getchar();    return 0;}
 

The above program does not go wrong, but outputs 4 correctly. This is not because sizeof really finds the memory that is offset by 999999999 locations relative to the first cc address. As described above, sizeof is determined during compilation, therefore, it determines the size based on the cc [X] type. Because sizeof does not access these addresses, there is no problem in the above program syntax, but logically it seems that there is no practical significance. Can you think of something that proves that sizeof really didn't "execute" in brackets? It is actually quite simple. Try to run the following code:

printf("%d\n", sizeof(printf("hactrox")));
The result is 4, and "hactrox is not output! If sizeof is not output, it means that sizeof does not call anything in brackets, but why is it 4? Because sizeof is determined by type. For printf or function, sizeof is determined by its return value type. Because printf returns the number of output characters, which is of the int type, it is equivalent to sizeof (int), so it is 4. You may not know this feature of printf. Try to run the following code:

#include 
 
  int main(){int num = printf("hactrox\n");    printf("%d\n", num);    getchar();    return 0;}
 
A more direct example is as follows:

# Include
 
  
Int fun () {printf ("hactrox"); return 5;} int main () {printf ("% d \ n", sizeof (fun ())); // The function is not executed, and 4 getchar (); return 0;} is output based on the return value type ;}
 

There is another thing to note about sizeof. Try running the following program in your mind and get a result before running it:

#include 
 
  #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 is no output for an array? The reason is that sizeof returns an unsigned int. When int is compared with an unsigned int, int is converted to an unsigned int. When int is <0, this forced conversion is naturally tragic, the result is MaxValue (unsigned int)-abs (int). When d =-1, it is naturally converted into a very large number, of course, the for Loop will not be executed.

# Include
 
  
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", ); // unsigned int should be output by % u instead of % d getchar (); return 0 ;}
 

Finally, what should I do if I want to output the maximum value of the unsigned int in C language? TIPS:

# Include
 
  
Int main () {printf ("Max value of unsigned int is: % u \ n ",~ (Unsigned int) 0); // The result can only be expressed as printf ("Max value of int is: % d \ n ",~ (Unsigned int) 0> 1); // % d is used for the result, and % u can be used to indicate getchar (); return 0 ;}
 


Related Article

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.