Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the confusing behaviors in C language?

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "what is the confusion behavior of C language". In the daily operation, I believe that many people have doubts about the confusion behavior of C language. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the questions of "what is the confusion of C language confusion?" Next, please follow the editor to study!

Code 0:

# include int main (void) {int c = 5; switch (c) {case 0... 10: printf ("0 other-> 10\ n"); break; case 11... 20: printf ("11 other-> 20\ n"); break; default: printf ("other\ n");} return 0;}

Output result:

0Mutual-> 10

The above features are supported by common compilers, but are not mentioned in the standard.

Code 1

# include int main (void) {printf ("% m\ n"); return 0;}

Output result:

Success

Equivalent to:

Printf ("% s\ n", stderr (errno))

Since there is no error setting errno in front of your code, errno will be 0, and the corresponding description information will be Success.

Code 2

# include int main (void) {int I = 10; printf ("% zu\ n", sizeof (iTunes +)); printf ("% zu\ n", sizeof (+ + I)); printf ("% d\ n", I); return 0;}

Output result:

4 4 10

The object that sizeof actually works on is the type. Expressions in sizeof are not executed by themselves.

Code 3

# include # include int main (void) {while (1) {fprintf (stdout, "official account"); fprintf (stderr, "programming Zhuji"); sleep (10);} return 0;}

Output result:

Programming Zhuji

Why not export the official account? The reason is that standard input defaults to row buffering, while standard error is unbuffered. This is already explained in "those bizarre buffer problems".

Code 4

# include int main (void) {int a = 10; switch (a) {int b = 20; case 10: printf ("% d\ n", a + b); break; default: printf ("% d\ n", a + b); break;} return 0;}

Output result:

ten

Int b = 20 in switch will not be executed, and you will find a warning when you compile.

Code 4

# include int main (void) {printf ("% c\ n", 4 ["hello official account programming Zhuji"]); return 0;}

Output result:

O

Equivalent to:

Char * str = "hello official account programming Zhuji"; printf ("% c\ n", str [4])

Code 5

/ / Source: official account programming Zhuji / / https://www.yanbinghu.com # include int main (void) {char arr [] = {'haggling Magnum Laoyuo'}; printf ("% s\ n", arr); / / disaster! , may crash return 0;}

Code 6

Useless, but also core dump ultra-short code, you can compile and run:

Main=0

Code 7

# include int main (void) {int arr [] = {5 end 4); for (int I =-1; I < sizeof (arr) / sizeof (int)-1; iTunes +) {printf ("% d\ n", arr [item1]);} printf ("end\ n"); return 0;}

Output result:

End

The reason is also very simple, the result of sizeof (arr) / sizeof (int) is unsigend, the comparison between int type I and unsigned is converted to a very large unsigned number, so the condition of for loop is not satisfied.

Code 8

# include test () {long b = 12345678987654321; return b;} int main (void) {long a = test (); printf ("% ld\ n", a); return 0;}

Output result:

1653732529

Code 9

# include int main (void) {float a = 3; int b = 2; printf ("% d\ n", a return 2); return 0;}

Output result:

1199094392

Reason: floating point numbers are stored in the computer according to the IEEE754 standard

At this point, the study of "what is the confusing behavior of C language" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report