C language summary_Function knowledge with additional C language practice questions

Get into the habit of writing together! This is the 17th day of my participation in the "Nuggets Daily New Plan · April Update Challenge", click to view the details of the event .

1. Function Definition

 //定义一个函数
 int func(int a,int b)
 {
     
 }
 ​
 <函数的返回值类型> <函数名称>(函数的形参列表,....)
 {
     函数代码主体部分;
 }
复制代码

<The return value type of the function>: The return value type can be any type supported by the C language. Basic data types, pointers, structures, enumerations... If the function is executed and does not need to return a value, you can declare the return value type of the function as an empty type. Void function name: It cannot conflict with the library function name, and the naming rules are the same as the variable naming rules. Formal parameter list of the function: The parameters passed in by the function in the execution line are of the same type as the return value type definition method. If there are multiple formal parameters, you can use commas to separate the return values ​​of the function: If the function needs to return data to the caller after the function is executed, you can use return, which can only return one value.

 #include <stdio.h>
 int func(int,int);  //声明func函数
 //int func(int a,int b);//声明func函数
 ​
 int main(void)
 {
     int a;
     a=func(12.34,56.78); //形参传入之后,会转为整型
     printf("a=%d\n",a);
     return 0;
 }
 ​
 //定义一个函数
 int func(int a,int b)
 {
     int c;
     c=a+b;
     return c; //给调用者返回结果
 }
复制代码

2. Function exercises

(1) Write a function that determines whether a year is a leap year. (Condition: Divisible by 4 but not divisible by 100 or divisible by 400)

 #include <stdio.h>
 int func_year(int year); //声明函数
 int main(void)
 {
     int year; //c89标准
     int err=0;
     printf("输入一个年份:");
     scanf("%d",&year);
     err=func_year(year);//调用函数
     if(err==1) 
     {
         printf("闰年!\n");
     }
     else if(err==0)
     {
         printf("平年!\n");
     }
     else
     {
         printf("输入的年份错误!\n");
     }
     return 0;
 }
复制代码

(2) This function is called by the main function to judge the normal year and leap year.

 /*
 函数功能: 判断平年和闰年
 返回值  : 0表示平年,1表示闰年,负数表示错误
 */
 int func_year(int year)
 {
     if(year<1900)return -1; //加一个限制条件
     if((year%4==0&&year%100!=0)||year%400==0)
     {
         return 1;
     }
     return 0;
 }
复制代码

(3) Enter a temperature in Fahrenheit and output the temperature in Celsius. The calculation formula is (Fahrenheit-32)×5÷9, and the result is required to be kept to two decimal places.

 #include <stdio.h>
 float func_temp(float temp); //声明函数
 int main(void)
 {
     float temp;
     printf("输入一个温度值:");
     scanf("%f",&temp);
     printf("temp=%.2f\n",func_temp(temp));
     return 0;
 }
 ​
 /*
 函数功能: 计算温度
 返回值  : 摄氏度
 */
 float func_temp(float temp)
 {
     //(华氏度-32)×5÷9
     return (temp-32)*5/9.0;
 }
复制代码

(4) Encapsulate the function and print the following patterns: Palindromic triangle, the formal parameter can determine the number of lines.

      1
      121
     12321
    1234321
复制代码

(5) Calculate the percentage and automatically convert the data

 #include <stdio.h>
 int main(void)
 {
     float data;
     data=(10/60.0)*100;  //运算时,需要一个数据是浮点数,运算中才可以使用浮点方式存储
     printf("data=%.0f%%\n",data);
     return 0;
 }
复制代码

(6) Example of function return value: limited range

 #include <stdio.h>
 int func(int a);
 int main(void)
 {
     printf("%d\n",func(200));
     return 0;
 }
 ​
 int func(int a)
 {
     return (a==100);  //限定范围值为0和1
 }
复制代码

(6) Generate random number password

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>

char pwdcont[] = "0123456789";

char* GeneratePassword(int pwd_size)
{
	int i;
	int random;
	char *Password = (char *)malloc(pwd_size + 1);

	//获取时间种子
	srand((unsigned)time(NULL));

	for (i = 0; i < pwd_size; i++)
	{
		random = rand() % (strlen(pwdcont));
		*(Password + i) = pwdcont[random];
	}

	*(Password + i) = '\0';
	return Password;
}

int main()
{
	int random;
	char *Password;
	srand((unsigned)time(NULL));

	for (int i = 0; i < 10; i++)
	{
		Sleep(100);
		random = rand() % 6;//密码的长度范围 (6-63) 
		while (random < 6)random++;
		printf("random = %d\n", random);
		Password = GeneratePassword(random);
		printf("Password = %s\n", Password);
	}
	free(Password);
	return 0;
}
复制代码

3. The scope of variables

3.1 Global and local variables

     const int c;  //定义只读变量
     static int b; //定义静态变量
复制代码

Note: The variable definition scope is divided into global variables and local variables. 1. Local variables and global variables can have the same name. 2. Limited use of local variables if the local variable name is the same as the global variable name.

 #include <stdio.h>
 void func(int);
 int data=123; //全局变量(公用变量)
 ​
 int main(void)
 {
     int data=456; //局部变量
     printf("data1=%d\n",data);
     func(666);
     return 0;
 }
 ​
 void func(int data)
 {
     printf("data2=%d\n",data);
 }
复制代码

3.2 Read-only variables

 #include <stdio.h>
 void func(int);
 const int data=888; //只读变量
 int main(void)
 {
     //data=666; 错误的
     printf("%d\n",data);
     return 0;
 }
 ​
 void func(int data)
 {
     printf("data2=%d\n",data);
 }
复制代码

3.3 Static variables

 静态变量测试
 #include <stdio.h>
 int func(void);
 int main(void)
 {
     int i,data;
     for(i=0;i<5;i++)
     {
         data=func();
     }
     printf("data=%d\n",data);
     return 0;
 }
 ​
 int func(void)
 {
     //int data=0; //局部变量,生命周期随着函数调用结束而终止。
     static int data=0; //静态变量,生命周期与main函数一样。 
       //static int data=0 只有第一次执行有效
     data++; //data=data+1  ,1
     return data;
 }
复制代码

3.4 Static global variables

 #include <stdio.h>
 ​
 //int data;  全局变量,可以在其他.c文件引用
 static int data=0; //静态全局变量-----局部变量
 //静态全局变量: 表示该data变量不能被其他文件所引用。
 //防止全局变量,重命名。
 ​
 int main(void)
 {
     return 0;
 }
复制代码

3.5 Static functions

 #include <stdio.h>
 static int func(void);  
 ​
 int main(void)
 {
     func();
     return 0;
 }
 ​
 //定义静态函数,表示该函数只能在本文件使用。
 static int func(void)
 {
     printf("123\n");
 }
复制代码

3.6 Initialization of variables

 #include <stdio.h>
 static int data1;  
 int data2;
 ​
 int main(void)
 {
     int data3; //局部变量
     static int data4;
     printf("data1=%d\n",data1); //0
     printf("data2=%d\n",data2); //0
     printf("data3=%d\n",data3); //未知值
     printf("data4=%d\n",data4); //0
     
     int cnt;
     //cnt++;
     /*
     for(i=0;i<5;i++)
     {
         if(xxxx)data3|=0x1;
         data3<<=1;
     }*/
     return 0;
 }
复制代码

4. List C language practice questions

1.【判断】C 语言程序中,当调用函数时,实参和虚参可以共用存储单元。

对

错

2.【单选】以下关于delete运算符的描述中,错误的是____。

A.对一个指针可以使用多次delete运算符

B.delete必须用于new返回的指针

C.使用delete删除对象时要调用析构函数

D. new和 delete必须匹配使用,否则会出现内存泄露的问题

3.【单选】下列语句能正确表达C为大写字母的表达式是____。

A. c>=’A’ && c<=’Z’

B. c>=’A’ or c<=’Z’

C. c>=’A’ || c<=’Z’

D. c>=’A’ and c<=’Z’

4.【单选】对代码char* p=new char[100],描述正确的是____。

A. p在栈上,new出来的在堆上

B. p在堆上,new出来的在栈上

C. p和new出来的内存都在堆上

D. p和new出来的内存都在栈上

5.【单选】下列不可用作 C 语言标识符的字符序列是____。

A.ab

B.a_1

C.$abc

D.BOOK

6.【单选】下列哪个不是C的关键字____。

A. const

B. continue

C. defaul

D. size

7.【单选】下列选项中,不属于指针常量的是____。

A.函数的名字

B.数组的名字

C.空指针

D.宏函数的名字

8.【单选】下列代码运行结果为____。

int a = 23;

printf("%d\n", a&a);

A. 0

B. 23

C. 46

D.运行错误

9.【判断】一个双目运算符作为类的成员函数重载时,重载函数的参数表中有一个参数。

对

错

10.【判断】函数返回值是指函数被调用之后,执行函数体中的程序段所取得的并返回给主调函数的值。

对

错

11.【单选】char *a, *b;分别指向两个字符串,以下表达式可判断字符串a、b相等的是____。

A. s1=s2

B. strcmp(s1,s2)==0

C. strcpy(s1,s2)==0

D. strlen(s1,s2)

12.【判断】通过 return 语句,函数可以返回一个或多个的返回值。

对

错

13.【多选】C语言中,以下描述错误的是____。

A.调用函数时,函数名必须与被调用的函数名完全一致

B.函数名允许用数字开头

C.函数调用时,不必区分函数名称的大小写

D.在函数体中只能出现一次return语句

14.【多选】在C语言中,关于break的说法正确的是____。

A.break不可用在while循环中

B.break可以用在for循环中

C.break不可用在do while循环中

D.break可以用在switch语句中

15.【多选】C语言中下列数组定义语句中合法的有____。

A. int a[3] = { 0 };

B. int a[3] = { 0, 1, 2 };

C. int a[3] = { 0, 1, 2, 3 };

D. int a[] = { 0, 1, 2 };

16.【多选】下列关于C语言中for循环的描述错误的是____。

A. for循环只能用于循环次数已经确定的情况

B. for循环的循环体语句中,不可以包含多条语句

C. for循环是先执行循环体语句,后判断表达式

D.在for循环中,不能用break 语句跳出循环体

17.【多选】C语言中,以下关于静态变量的说法不正确的是____。

A.静态全局变量过大,可能会导致堆栈溢出

B.静态全局变量的作用域为一个程序的所有源文件

C.函数中的静态变量,在函数退出后不被释放

D.静态变量只可以赋值一次,赋值后则不能改变

18.【多选】结构化程序设计的基本原则包括____。

A.自顶向下

B.逐步求精

C.模块化

D.限制使用 goto 语句

19.【多选】在C语言中,以下说法错误的是____。

A.自定义的函数只能调用库函数

B.函数的定义和调用都可以嵌套

C.函数的定义和调用都不可以嵌套

D.实用的C语言源程序通常由一个或多个函数组成

20.【多选】在C语言中,以下哪些是错误的字符常量____。

A.a

B.”a”

C.’abc’

D.’\101’

复制代码

Guess you like

Origin juejin.im/post/7087381087864750094