C语言02
头文件引入
#include "arrays.h"
arrays.h
//#ifndef INC_0903_ARRAYS_H
//#define INC_0903_ARRAYS_H
//#endif //INC_0903_ARRAYS_H
//防止重复引入
#pragma once
//空返回值 void
//定义一个函数原型
//void run(int a,int b);
//可以不写参数名
//作用是告诉外部有这个函数可以被访问 ->公开的public
//如果.c里面只实现 -->私有函数
void run (int ,int);
/*
* 参数
* 1.形参 临时在函数的范围内代表某一个值 只有调用时分配空间
* 2.实参 实际参数->可以是任意值
*/
函数
声明:函数原型
作用域
- 在函数或块内部的局部变量
- 在所有函数外部的全局变量
- 在形式参数的函数参数定义中
递归
参数
- 形参:临时在函数的范围内代表某一个值 只有调用时分配空间。在函数定义中出现的参数可以看做是一个占位符,它没有数据,只能等到函数被调用时接收传递进来的数据,所以称为形式参数,简称形参。
- 实参:实际参数->可以是任意值。函数被调用时给出的参数包含了实实在在的数据,会被函数内部的代码使用,所以称为实际参数,简称实参
- 区别:
- 形参变量只有在函数被调用时才会分配内存,调用结束后,立刻释放内存,所以形参变量只有在函数内部有效,不能在函数外部使用。
- 实参可以是常量、变量、表达式、函数等,无论实参是何种类型的数据,在进行函数调用时,它们都必须有确定的值,以便把这些值传送给形参,所以应该提前用赋值、输入等办法使实参获得确定值。
- 实参和形参在数量上、类型上、顺序上必须严格一致,否则会发生“类型不匹配”的错误。当然,如果能够进行自动类型转换,或者进行了强制类型转换,那么实参类型也可以不同于形参类型。
- 函数调用中发生的数据传递是单向的,只能把实参的值传递给形参,而不能把形参的值反向地传递给实参;换句话说,一旦完成数据的传递,实参和形参就再也没有瓜葛了,所以,在函数调用过程中,形参的值发生改变并不会影响实参。
函数调用
void run (int a,int b){
printf("a:%d\nb:%d\n",a,b);
test1();
test3();
}
数组
/**
* Created with CLion.
* Description:
* User:
* Date: 2019-09-03
* Time: 14:21
*/
//#ifndef INC_0903_ARRAYS_H
//#define INC_0903_ARRAYS_H
//#endif //INC_0903_ARRAYS_H
//防止重复引入
#pragma once
//空返回值 void
//定义一个函数原型
//void run(int a,int b);
//可以不写参数名
//作用是告诉外部有这个函数可以被访问 ->公开的public
//如果.c里面只实现 -->私有函数
void run (int ,int);
/*
* 参数
* 1.形参 临时在函数的范围内代表某一个值 只有调用时分配空间
* 2.实参 实际参数->可以是任意值
*/
数组声明
//数组的声明
void test1() {
/*
* 元素类型 数组名[数组长度] = {}
*
*/
int length = 5;
//int score[]={66,99,88,78,35};//方式一
int score[length];//方式二
score[0]=13;
printf("%d\n",score[0]);
}
多维数组
//多维数组
void test2(){
/*
* 二维数组
*/
//int row=3;
//int col=4;
//int score[row][col];
设置第一行第一列
//score[0][0]=55;
//score[2][3]=77;
/*
* 多维数组
*/
int score[3][4][5][6]={
{
{
{3,4,5,6,7,8},
{},
{},
{},
{}
},
{},
{},
{}
},
{},
{}
};
//score[0][0][0][0]=11;
}
字符数组
// 字符数组
test3(){
//char msg[]={'a','b','c','\0'};
//printf("%c\n",msg[0]);
//把字符当做字符串使用 字符数组的最后一个元素添加 \0(占一格字符位置)
//-->表示字符串结束
char *msg="abc";
printf("%s\n",msg);
}
循环
for循环
for 循环 :多次执行一个语句序列,简化管理循环变量的代码。
/**
* Created with CLion.
* Description:
* User: 87601
* Date: 2019-09-03
* Time: 15:26
*/
#pragma once
void run2();
//for 循环
void test4(){
/*
* 知道循环多少次时使用
*/
//遍历数组
int arr[]={66,48,3,88,9};
int index =0;
for(index =0;index<5;index++){
printf("索引:""index=%d\n value=%d\n",index,arr[index]);
}
}
//每隔5输出1个数 1--100
void test5(){
int index1 = 1;
for(index1;index1<=100;index1+=5){
printf("index1=%d\n",index1);
}
}
//遍历多维数组
void test6(){
int num[3][3] = {
{22,33,44},
{44,55,66},
{77,88,99}
};
int i = 0;
int p = 0;
for(i=0;i<3;i++){
for(p=0;p<3;p++){
printf("多维数组:%d\n",num[i][p]);
}
}
}
//四维数组
void test7(){
int n[1][1][1][1]={
{
{
{1}
}
}
};
int a = 0;
int b = 0;
int c = 0;
int d = 0;
for (a=0;a<1;a++){
for(b=0;b<1;b++){
for (c = 0; c < 1; c++) {
for(d=0;d<1;d++){
printf("四维数组:%d\n",n[a][b][c][d]);
}
}
}
}
}
while 循环
while 循环:当给定条件为真时,重复语句或语句组。它会在执行循环主体之前测试条件。
//while循环 不知道循环多少次
void test8(){
int n1 = 0;
while (n1<100){
//int m;
printf("输入数字:");
scanf("%d",&n1);
//
}
printf("完成\n");
}
//输入数字,大于100返回完成小于100继续输入
do…while
do…while 循环 :除了它是在循环主体结尾测试条件外,其他与 while 语句类似。
//do..while 循环
void test9(){
int n2 =0;
do{
printf("输入数字:");
scanf("%d",&n2);
}while(n2>100);
printf("完成...");
}
循环控制语句
Break
break 语句:终止循环或 switch 语句,程序流将继续执行紧接着循环或 switch 的下一条语句。
//break
void test10(){
int n1 = 0;
while (n1<100){
//int m;
printf("输入数字:");
scanf("%d",&n1);
if(n1==100){break;}
//
}
printf("完成\n");
}
Continue
continue 语句:告诉一个循环体立刻停止本次循环迭代,重新开始下次循环迭代。
//continue
void test11(){
int n3;
while(1){
scanf("%d",&n3);
if(n3==5) {continue;}
printf("%d\n", n3);
printf("继续");
if(n3==100){break;}
printf("zho");
}
}
Goto
goto 语句:将控制转移到被标记的语句。但是不建议在程序中使用 goto 语句。
//goto
void test12(){
int n4;
label:
scanf("%d",&n4);
printf("xxxx\n");
if(n4<10) {//小于10
goto label;
}
}
测试方法:
void run2(){
//test4();
//test5();
//test6();
//test7();
//test8();
//test9();
//test10();
test11();
//test12();
}
Main函数
手动传递参数
/*
* arg 传入到main函数里面的参数数量
* gcc main.c -o 0903
* ./0903
* args 传入到main函数的值
*/
/*
* clion执行时
* 会传入一个可执行文件的地址
*/
//printf("parNum:%d\n%s\n",arg,args[0]);
//int i ;
//for (i=0;i<arg;i++){
// printf("parmIndex:%d\nparm:%s\n",i,args[i]);
//}
/*
* 字符串对比 -> string.h -> strcmp
* 如果两个字符串相同 -> 0
* 如果第一个字符串 大于第二个字符串 -> 正数
* 如果小于 -> 负数
* */
if(arg>1) {
if (!strcmp(args[1],IS_UPDATE)){
int res = MessageBox(NULL, "是否立即更新", "", MB_OKCANCEL);
printf("res:%d\n",res);
if(res == IDOK){
printf("updating...");
}else{
printf("exiting...");
}
}
}
函数
/**
* Created with CLion.
* Description:
* User: 87601
* Date: 2019-09-03
* Time: 19:15
*/
//#ifndef INC_0903_FUNCTIONS_H
//#define INC_0903_FUNCTIONS_H
//#endif //INC_0903_FUNCTIONS_H
#pragma once
int sum(int, int);
int avg(int, int);
//可变参数的函数
void mutableFun(int arg,...);
/**
* Created with CLion.
* Description:
* User: 87601
* Date: 2019-09-03
* Time: 19:15
*/
#include "functions.h"
#include <stdio.h>
#include <stdarg.h>
int sum(int a, int b){
return a+b;
}
int avg(int c, int d){
return (c+d)/2;
}
void mutableFun(int arg,...){
printf("参数:%d\n",arg);
va_list list;
va_start(list,arg);
//printf("%d\n",va_arg(list,int));
//printf("%d\n",va_arg(list,int));
//printf("%d\n",va_arg(list,int));
//printf("%d\n",va_arg(list,int));
//printf("%d\n",va_arg(list,int));
int i;
for(i=0;i<arg;i++){
printf("%d:%d\n",i,va_arg(list,int));
}
va_end(list);
}
可变参数
//可变参数的函数
void mutableFun(int arg,...);
/*
* 可变参数
* 第一个参数是设置可变参数的数量
* 其余才是传入的参数
*/
void mutableFun(int arg,...){
printf("参数:%d\n",arg);
va_list list;
va_start(list,arg);
//printf("%d\n",va_arg(list,int));
//printf("%d\n",va_arg(list,int));
//printf("%d\n",va_arg(list,int));
//printf("%d\n",va_arg(list,int));
//printf("%d\n",va_arg(list,int));
int i;
for(i=0;i<arg;i++){
printf("%d:%d\n",i,va_arg(list,int));
}
va_end(list);
嵌套函数调用
int sum(int, int);
int avg(int, int);
/*
* 嵌套函数调用
*/
int sum(int a, int b){
return a+b;
}
int avg(int c, int d){
return (c+d)/2;
//printf("%d\n",avg(sum(10,5),3));
}
预编译–宏定义
/**
* Created with CLion.
* Description:
* User: 87601
* Date: 2019-09-03
* Time: 19:49
*/
//预编译处理
//处理常量
#pragma once
/*
* 通过宏定义 定义函数
*/
#define AA 10
#define BB 19980901
#define YS 19971208
#define AVG() (BB+YS)/2
#define SUM(a,b) a+b
#define LOW(attr) attr+32
#define UP(attr) attr-32
//宏定义多行代码
#define ENUMBER(len,body) \
int i; \
for(i=0;i<len;i++) \
body
printf("%d\n",AVG());
printf("%d\n",SUM(19980901,19971208));
printf("%c\n",UP('z'));
printf("%c\n",LOW('F'));
int array1[]={5,6,8,9,7};
//调用宏定义的函数
ENUMBER(5,{
printf("%d\n",array1[i]);
});
return 0;