In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces the C language three statements what matters needing attention, the text is very detailed, has a certain reference value, interested friends must read!
Before we get into today's topic, we need to know that in C, a statement is separated by a semicolon (;).
It is easy to understand, for example:
int a=3;//statement 1printf("Please advise! ");//statement 2;//statement 3---empty statement
The content of today's explanation was his introduction to some details of these three sentences. (Not to mention these words.)
Branch statements: if, switch
Loop: while, for, do while
goto statement
Additional small knowledge (if you meet later, you can learn here)
In C language: 0 means false--------
EOF: EOF is a computer term short for End Of File, which has a value of-1. This character is usually present at the end of the text to indicate the end of the data.
getchar():(From MSDN, you can translate it yourself.)
Read a character from a stream (getc, getwc), or get a character from stdin (getchar, getwchar).
Return Value
Each of these functions returns the character read. To indicate an read error or end-of-file condition, getc and getchar return EOF. For getc and getchar, use ferror or feof to check for an error or for end of file. (Personal understanding: if read correctly, the ASCII code value of the corresponding character is returned; if read fails/ends/errors, EOF is returned)
getchar Return type is int. (Because it may return ASCII values, or EOF (-1))
scanf() skips spaces, tabs, and newlines when reading characters (I won't go into detail here, but there will be a special chapter later)
system ()--a function that executes system commands--from header files
shutdown -s--shutdown command, shutdown -s -t time--automatic shutdown after time seconds, shutdown -a--cancel shutdown
If you want the program to shut down within 60 seconds: system("shutdown -s -t 60");
strcmp: A function that compares string sizes. To compare the sizes of strings A and B, the code is as follows: strcmp(A,B);
If the return value =0, then the two strings are equal; if the return value>0, then the string length of A is greater than B; if the return value 18) printf("underage");else printf("adult"); printf("can fall in love! ")
This code results: adult can love it!
However, if age=8, the output result is: minors can fall in love! This is not what we want.
So you need to enclose these two statements after else with { }.
dangling else: else matches its nearest unmatched if
The code is as follows: (you can think about what the answer is)
int a=0;int b=2;if(a==1) if(b==2) printf("hehe\n");else printf("haha\n");
You know, this question doesn't print out at all.
This problem not only requires us to know that else matches the nearest if, but also requires us to pay attention to the writing style of the code
Here recommend a book about code style "high quality C-C++ programming", we can find the electronic version of the study.
3. Switch statement
The switch statement is often used in multi-branch situations
For example:
Input 1, output Monday
Input 2, output Tuesday
…
Input 6, output Saturday
Input 7, output Sunday
If…else if…else if…else is more complicated, then switch is a better choice.
3.1 sentence structure
switch(integer expression){ case integer constant expression: statement;}
Note:
After switch is an integer expression (such as int type, char type)[can use characters, because the character is actually a type of integer, the underlying storage is the ASCII value corresponding to the character]
case is followed by an integer constant expression (e.g. 1, 1+2)
But if it is the following code, can you output the day of the week corresponding to the number? And you can think about that.
int day=0;scanf("%d",&day);switch(day){ case 1: printf("Mon\n"); case 2: printf("Tue\n"); case 3: printf("Wed\n"); case 4: printf("Thus\n"); case 5: printf("Fri\n"); case 6: printf("Sat\n"); case 7: printf("Sun\n");}
And the answer is, if I type 1, I print out all the weeks. Because the value I entered is an entry, if I enter 2, I enter from case2, but there seems to be no exit to stop the statement, so I output it all the way back until the switch ends.
Then, when the switch is broken, the switch becomes a real branch.
However, break is not necessarily added. Add it according to your needs, such as the following code (print workdays and rest days according to numbers)
int day=0;scanf("%d",&day);switch(day){ case 1: case 2: case 3: case 4: case 5: printf("weekday\n"); break; case 6: case 7: printf("Rest Day\n"); break;//can be omitted, but it is recommended to add //default: // printf("input error"); //break;}
Let's talk about default. Without default, as in the above code. If the output is not a number from 1 to 7, nothing is printed on the bundle. But you can add a default, if the input does not meet the above requirements, then the input error is displayed, you can remind yourself. (on demand)
As for the default position, it can not be at the end, as long as other requirements meet the meaning of the question, it can be placed in other positions.
Finally, we can check whether everyone understands switch well through a piece of code:
int n=1;int m=2;switch(n){ case 1:m++; case 2:n++; case 3: switch(n) {//switch allows nesting case 1: n++; case 2: m++; n++; break; } case 4: m++; break; default: break;}printf("m=%d,n=%d\n",m,n);
If your answer is 5, 3, then you understand. If not, you can compare it with the switch above and think about it again.
4. Circular statements
Loop statement: A group of statements that are repeatedly executed is called the loop body. Whether it can continue to repeat determines the termination condition of the loop. A loop structure is a flow structure that repeatedly executes a program under certain conditions, and a program that is repeatedly executed is called a loop body. A loop statement is composed of the loop body and the termination condition of the loop.
C language loop structure is used: while, for, do while
4.1 while loop (similar to do while)
In a while loop, break is used to permanently terminate the loop (for, do while applies).
The code is as follows:
int i=1;while(i
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.