Summary of C/C++ language basic grammar (2) End-of-term life-saving dry goods

  The basic grammar of the C language final exam, just read these few blog posts! (Dry goods two)

  Summary of C language final exam grammar:

   C language basic grammar knowledge chapter 2

   Sections One and Two: Data Output (1)

	1、使用printf和scanf函数时,需加 #include "stdio.h" (标准输入输出函数头文件)
	2、printf函数可有多个参数,
	3、printf()函数的调用格式为:printf("<格式化字符串>", <参量表>);
	4.printf()函数也有不少技巧,比如输出如下图形:
		***
		 *
		***
	仅用一行printf();函数即可!
#include <stdio.h>
int main(){
    
    
	int x=017;
	printf("***\n"" * \n""***\n");
	return 0;
}

运行结果:
		***
		 *
		***

   5. int x=017 (x is an octal number);

	printf(“%d”, x); 15		十进制输出
	printf(“%o”, x); 17 		十六进制输出
	printf(“%#o”,x); 017	    八进制加前缀
	printf(“%x”, x); f  		十六进制输出
	printf(“%#x”,x); 0xf		十六进制加前缀

6、

   	int x=12,y=34;  注意这种题型
    char z=‘a’;     
      printf(“%d ”,x,y); 一个格式说明,两个输出变量,后面的y不输出
      printf(“%c”,z);       结果为:12a

7, must be recited

Insert picture description here
for example:

printf(“%2d”,123 );  第二部分有三位,大于指定的两位,原样输出123
printf(“%5d”,123 );  第二部分有三位,小于指定的五位,左边补两个空格  123
printf(“%10f”,1.25 ); 小数要求补足6位的,没有六位的补0,。结果为  1.250000 
printf(“%5.3f”,125 ); 小数三位,整个五位,结果为1.250(小数点算一位)
printf(“%3.1f”,1.25 );小数一位,整个三位,结果为1.3(要进行四舍五入)

   Section 3 Data Input
1. Scanf ("a=%d, b=%d", &a, &b) Points for attention in the exam, you need to bear in mind that the scanf function input format and code designation format must be consistent, common test points.

   The input on the terminal must be a=12, b=34 to correctly assign 12 and 34 to a and b. Nothing is different.

  2. scanf("%d,%d",x,y); this way of writing is absolutely wrong, scanf() function should not lose the address character &!
   should be changed to scanf("%d,%d", &x, &y );
   3. Pay special attention to the inspection of the pointer in scanf

例如: int x=2;int *p=&x;
scanf(“%d”,x);   错误          scanf(“%d”,p);正确
scanf(“%d”,&p);  错误         scanf(“%d”,*p)错误

   4. Specify the length of input (exam focus)

终端输入:1234567 
scanf(“%2d%4d%d”,&x,&y,&z);x为12,y为3456,z为7
终端输入:1 234567     由于1和2中间有空格,所以只有1位给x
scanf(“%2d%4d%d”,&x,&y,&z);x为12,y为2345,z为67

   5. Characters and integers are close relatives:

int x=97;
printf(“%d”,x);   结果为97
printf(“%c”,x);   结果为 a

   6. The difference between characters and integers when inputting (usually in the exam)
scanf ("%d", &x); at this time, enter 1, and pay special attention to the integer 1
scanf ("%c", &x); at this time, enter 1 , Pay special attention to that the character '1' ASCII is an integer 49.
   Supplementary explanation:
   1) Investigation of scanf function format:
  note that the second part of the function is an address like &a , not a; scanf(“%d%d% * d”,&a,&b,&c); * is discarded Enter the third data .

  Additional:
   <1> scanf(); The function has a return value (int type): ①When the input format is different from the specified format, it returns 0;   ②The function detects the end of the file (end of file can also enter Ctrl+z (or Ctrl+d) (different computer input is different, input once or twice) to tell the computer that the input is over.) Or return EOF when the input is wrong (EOF is a special class defined in stdio.h, which means -1).   ③scanf(); The return value of the function is equal to the number of items entered correctly, and the input is ended when an error occurs.   ④ If %d in scanf is written consecutively, such as "%d%d%d", when inputting data, the data cannot be separated by commas, only blank characters (space or tab key or enter key) Separate-"2 (space) 3 (tab) 4" or "2 (tab) 3 (carriage return) 4" and so on. If it is "%d,%d,%d", you need to add "," when inputting data, such as "2,3,4". (The meaning here is actually to keep the code consistent with the input format at runtime.) <2> The examination of putchar and getchar functions: char a = getchar(); There is no parameter, you get a character you input from the keyboard to the variable a .   putchar('y') outputs the character y to the screen.   <3> How to realize the exchange of the values ​​in the two variables x and y (requires memorization)    can not set x=y, y=x; use intermediate variables t=x; x=y; y=t. (The reason is that direct assignment leads to data loss, so you need to use the intermediate variable t to transfer.)   <4> How to achieve a program that retains three decimal places and rounds the fourth digit, (memory)
  



  
  



   y=(int)(x*100+0.5)/100.0   这个保留两位,对第三位四舍五入
   y=(int)(x*1000+0.5)/1000.0 这个保留三位,对第四位四舍五入
   y=(int)(x*10000+0.5)/10000.0 这个保留四位,对第五位四舍五入
   This has generalized meaning. Note that x = (int)x removes the decimal part.

Guess you like

Origin blog.csdn.net/qq_43515862/article/details/114277012