当前位置:网站首页>C language - Introduction - Foundation - syntax - [identifier, keyword, semicolon, space, comment, input and output] (III)
C language - Introduction - Foundation - syntax - [identifier, keyword, semicolon, space, comment, input and output] (III)
2022-07-04 08:49:00 【Hu Anmin】
What is an identifier ?
stay C In language , Symbolic constant , Variable , Array , Functions and so on need a certain name , We call this name an identifier
Identifier partition ?
keyword , Predefined identifiers and user identifiers !
Naming rules for identifiers ?
Only by letters , Numbers , Underline , Or dollar sign ($) form , And can't start with a number ;
C Languages are case sensitive , Usually, variable and function names are written in lowercase letters ;
Although the length of the name is not specified , But don't write too long , It's best to see the name and know the meaning , such as int max At first glance, it is used to store the maximum value ! also , Not with the following 32 individual C The keyword name of the language is the same ! as well as C The identifier defined by the system in the language , Such as system class library , System constant name system function name , Such as : Format output function :
printf();
The nuptial !
Identifier hump naming
- See the name and know the meaning , It can improve the readability of the code
- Hump named , It can improve the readability of the code
- Hump naming is when variable names or function names are connected by multiple words , When constituting an identifier , The first word begins with a lowercase letter ; The first letter of the second word is capitalized .
- for example :
myFirstName
、myLastName
Such variable names look like humps
What is a keyword ?
C An English word with a special meaning in a language , Usually used to construct statements , Store the data , Define data types and so on
A semicolon
stay C In the program , Semicolons are statement terminators . in other words , Each statement must end with a semicolon , Otherwise, there will be compilation problems . It shows the end of a logical entity . for example
printf(“Hello, World!\n”)
;
Space
Lines that contain only spaces , It's called a blank line , Maybe with comments ,C The compiler will completely ignore it . stay C in , Spaces are used to describe spaces 、 tabs 、 Line breaks and comments . Spaces separate parts of a statement , Let the compiler recognize an element in a statement , Where to end , Where does the next element start . therefore , In the following sentence :
int age;
ad locum ,int and age There must be at least one space character between ( It's usually a blank character ), So that the compiler can distinguish them . On the other hand , In the following sentence :
fruit = apples + oranges; // The total number of fruits obtained
fruit and =, perhaps = and apples The space character between is not required , But to enhance readability , You can add some spaces as needed .
notes
What is annotation for , Comments are for programmers to see , It's not for the computer . In learning , You can add comments next to the code you write , Let yourself know what this code is for now or in the future , What's the usage? , What do you need to pay attention to ; At work , The program is not in the charge of itself , It is the joint efforts of multiple programmers , At this time, comments are needed to help others understand the code they write . Don't underestimate comments , Large companies pay great attention to the specification of code and comments .
I added some comments on it . Notes can be expressed in two ways ,
- The first is
//
, Indicates this line // All the following characters are comments ; - The second is
/* */
, This is a multi paragraph comment , When you want to express a long comment , It is more convenient to use multi paragraph comments .
Other symbols
Curly braces
{ }
: The body of the function , Compound statements and initialization of arrays ;parentheses
()
: Function definition is used to enclose parameters , Or it can be used to modify the operation order , such as :(a + 1) 2 and a + (1 2)square brackets
[]
Define array types and application elements ;Order number
.
: Reference member in structure ;
It's on the Internet , We will introduce it in detail later in our study , And repeated use , Here is only for understanding
Input and output
I / O function (printf and scanf) yes C Two very important functions in language , It's also learning C Two functions that language must learn . stay C Language program , Almost no program doesn't need these two functions , Especially the output function (printf), So these two functions have to be mastered .
If you want to use in the program printf perhaps scanf, Then you must include the header file stdio.h. Because these two functions are included in the header file .
The function of the output function is to output the results of the program running to the screen , The function of the input function is to assign values to variables in the program through the keyboard . It can be said that input-output function is the interface between user and computer . among printf Is very powerful , The usage is very flexible , It's hard to master ; and scanf The usage of is relatively fixed , But there's a lot to pay attention to .
printf Output
printf Function is a formatted output function , Used to output data to the screen .printf Function is called by : printf( Formatted string , parameter list );
Column : printf(" My heart is full of bandits , You can't roll it , My heart is full of stones , Don't turn around .\n");
among \n
The meaning of a new line
printf There are four formats
printf(" character string \n");
printf(" Output controller ", Output parameters );
printf(" Output controller 1 Output controller 2…", Output parameters 1, Output parameters 2, …);
printf(" Output controller Non output controller ", Output parameters );
All with % The beginning is basically the output control character , Non output controller , It's the characters that will be output as is
demonstration :
int age=18;
char xb='x';
double weight=62.5;
char name[21]; // A character array
memset(name,0,sizeof(name)); // Apply for a piece of memory with a length of name Size
strcpy(name, " Xi Shi "); // strcpy The function copies the string
printf(" My name is :%s, Sex :%c, Age :%d year , weight %lf kg .\n",name,xb,age,weight);
There are some functions above that we may , No contact , No problem , It will be explained later , At present, you can
Common controllers
Controller | explain |
---|---|
%d | Output according to the actual length of decimal integer data . |
%ld | Output long integer data . |
%md | m The width of the specified output field . If the number of digits in the data is less than m, Then fill in the space at the left end , If more than m, Then output according to the actual number of digits . |
%u | Output unsigned integer (unsigned). It can also be used to output unsigned integers %d, This is to convert unsigned numbers to signed numbers , Then the output . But it's better not to write this when programming , Because it's a conversion , send CPU Do one more idle . |
%c | Used to output a character . |
%f | Used to output real numbers , Including single precision and double precision , Output in decimal form . Do not specify field width , Automatically assigned by the system , The whole integer part is output , Decimal part output 6 position , exceed 6 The rounding of bits . %lf It is used for input Equivalent to %f If you use %f Data chaos |
%.mf | Output real number after decimal point m position , Be careful m There's a point in front . have access to %.2f To keep it 2 position perhaps %.3f Retain 3 position …… Pay attention to the decimal point Not less 了 |
%o | Output as octal integer , This one is rarely used , Just get to know . |
%s | Used to output string . use %s The output string is the same as the previous direct output string . But first define character array or character pointer to store or point to string , Let's talk about this later . |
%x( or %X or %#x or %#X) | Output integer in hexadecimal form , This is very important . |
%p | Print address data |
%x、%X、%#x、%#X The difference between
Be sure to master %x( or %X or %#x or %#X), Because when debugging, it is often necessary to output all binary codes in memory , Then display it in hexadecimal . Let's write a program to see what the four differences are :
# include <stdio.h>
int main(void)
{
int i = 47;
printf("%x\n", i); //2f
printf("%X\n", i); //2F
printf("%#x\n", i); //0x2f
printf("%#X\n", i); //0X2F
return 0;
}
As can be seen from the output : If it's lowercase x, The output letters are lowercase ; If it's capital X, The output letters are uppercase ; If you add one #, It's output in standard hexadecimal form . It's better to add one #, Otherwise, if there is no letter in the output hexadecimal number, it will be mistaken for a decimal number ! All in all , No addition # Easy to misunderstand . But if the output 0x2f or 0x2F, Then people will know it's hexadecimal . and %#x and %#X in , I think capital is better , Because capitalization is the absolute standard hexadecimal notation .
How to output %d、\ And double quotes
printf There is an output controller in %d, The escape character is preceded by a backslash \, And double quotes . Have you ever thought about such a question : How to pass these three symbols printf Output to the screen ? To export %d Just add one more... To the front %, To export \ Just add one more... To the front \, To output double quotation marks, just add a \ that will do . The procedure is as follows :
# include <stdio.h>
int main(void)
{
printf("%%d\n"); //%d
printf("\\\n"); //\ printf("\"\"\n"); //""
return 0;
}
printf yes C A very important function in language . After the above study, we found that , It's not hard . Just program more and practice more , I will soon master .
Secondly, after learning this section, you should know why you need “ Output controller ”. Because all the data in the computer is binary 0、1 Code , So when outputting, you should use “ Output controller ” Tell the computer how to display binary data .
In output controller ,%d、%f、%s、%c Is the most commonly used , They are output integers 、 The set of real Numbers 、 String and character control characters . of %x、%X、%#x、%#X The difference between the four usages , Just need to know .
scanf Input
Program is the medium of human-computer interaction , There must be input as well as output stay C In language , There are multiple functions to get user input from the keyboard :
- scanf(): and printf() similar ,scanf() You can enter multiple types of data .
- getchar()、getche()、getch(): All three functions are used to enter a single character .
- gets(): Get a row of data , And treat it as a string .
scanf() Is the most flexible 、 The most complicated 、 The most common input function , But it can't completely replace other functions , Everyone should know something .
scanf Function is a format input function , Used to accept data entered from the keyboard , After user input data is completed , press return (Enter) End input .
scanf Function is called by :scanf( Format control string , Address table column );
Be careful , Not in scanf The last addition of the formatted string of \n. among ,
The function of format control string and printf The same function
, But you can't display non formatted strings , That is, the prompt string cannot be displayed ,The address of each variable is given in the address table column
. The address is determined by the address operator “&” Followed by variable names . for example :&a、&b Each represents a variable a And variables b The address of . and scanf Functions are essentially assigning values to variables , But you need to write the address of the variable , Such as &a This address is given by the compiler system in memory a The address of the variable assignment . stay C In language , Using the concept of address , This is different from other languages .
Input integer
For the format of input integer %d Express , List integer variable names in parameters , Used to save input data . Before entering data , Usually output a sentence to prompt the user . The same below .
int age=0;
printf(" Please enter your age :"); // Prompt text not to wrap , Let the user type later , The same below .
scanf("%d",&age); // Sign before variable name &, Don't ask why , Later on .
The input character
For the format of input characters %c Express , List character variable names in parameters , Used to save input data .
char xb=0;
printf(" Please enter your family name :");
scanf("%c",&xb); // Sign before variable name &
Enter floating point number
Input floating-point number format %lf Express , List floating point variable names in parameters , Used to save input data .
double weight=62.5;
printf(" Please enter your weight :");
scanf("%lf",&weight); // Sign before variable name &.
Input string
Format of input string %s Express , List string variable names in parameters , Used to save input data .
char name[21];
memset(name,0,sizeof(name));
printf(" Please enter your name :");
scanf("%s",name); // Pay attention , String variable name can be unsigned &, Don't ask why , Later on .
Enter multiple content
Call once scanf Function can input multiple data , After all data input, press enter to end the input .
int c = 0, d = 0;
printf(" Please enter 2 Integer user spaces separated :\n");
scanf("%d %d", &c, &d); // Enter two integers and assign them to c、d
printf("c*d=%d\n", c*d); // Calculation c*d And output
return 0;
scanf() There are two spaces separated by %d, Followed by two variables , This requires us to enter two integers at once , And assign them to c and d. Be careful "%d %d" There are spaces between , So there should also be spaces when entering data . about scanf(), The format of input data should be consistent with that of control string .
scanf and printf Compare
Actually scanf and printf Very similar , It's just the opposite :
scanf("%d %d", &a, &b); // Get two integers entered by the user , Assign values to variables a and b
printf("%d %d", a, b); // Put the variable a and b The value of is output on the display
They all have format control strings , There's a list of variables . The difference is ,scanf You have to bring a & Symbol .& It's called an address character , That is, get the address of the variable in memory . Then the value we input on the console will be stored in , In the corresponding memory address , Using variables directly is equivalent to taking out the value corresponding to the memory address .
putchar and getchar
putchar: Output a character to the screen
getchar: Get a character from the keyboard
边栏推荐
- How to play dapr without kubernetes?
- ArcGIS应用(二十二)Arcmap加载激光雷达las格式数据
- Question 49: how to quickly determine the impact of IO latency on MySQL performance
- 根据数字显示中文汉字
- Group programming ladder race - exercise set l2-002 linked list de duplication
- How to send pictures to the server in the form of file stream through the upload control of antd
- How to choose solid state hard disk and mechanical hard disk in computer
- What if I forget the router password
- Codeforces Global Round 21(A-E)
- [BSP video tutorial] stm32h7 video tutorial phase 5: MDK topic, system introduction to MDK debugging, AC5, AC6 compilers, RTE development environment and the role of various configuration items (2022-
猜你喜欢
随机推荐
没有Kubernetes怎么玩Dapr?
How does Xiaobai buy a suitable notebook
没有Kubernetes怎么玩Dapr?
Getting started with microservices: gateway gateway
Private collection project practice sharing [Yugong series] February 2022 U3D full stack class 007 - production and setting skybox resources
Codeforces Round #803 (Div. 2)(A-D)
Question 49: how to quickly determine the impact of IO latency on MySQL performance
Codeforces Round #803 (Div. 2)(A-D)
[error record] no matching function for call to 'cacheflush' cacheflush();)
How to play dapr without kubernetes?
ctfshow web255 web 256 web257
Three paradigms of database design
std::is_ union,std::is_ class,std::integral_ constant
转:优秀的管理者,关注的不是错误,而是优势
随机事件的关系与运算
What if I forget the router password
@Role of requestparam annotation
ctfshow web255 web 256 web257
20220701 Barbalat引理证明
Learn nuxt js