C语言基本语法

数据变量

数据是存放在内存里的,内存是很庞大的空间,所有存放的数据都是010001000的方式存放,而视觉能识别的数据存放就需要不同的数据类型

数据需要有的数据类型,整型数(int)、浮点型(float)、字符(char)long、double

数据要有变量类型、变量名、变量的值(即存放的真时数据)

基本写法:
int a = 1;
char b = ‘b’;
float c = 3.1415926;

通俗来说就是有多个盒子,每个盒子分配好放不同类型的东西,比如,一个盒子就只能放杯子,一个盒子只能放筷子

杯子,筷子就是不同是数据类型,但这两个盒子里,却可以放不同的杯子,不同是筷子

#include <stdio.h>
#include <stdlib.h>
int main()
{
	int a = 12;
    int b = 33;
    int num = 0;
    
    num = a + b;
    
	printf("hello world!\n");
    printf("num = %d\n",num);

	system("pause");
	return 0;
}

变量里的数据输出是通过占位符来完成

占位符:%d
%d代表在输出的地方,占上一个位置,至于输出什么,是根据逗号后面的变量名里面的变量值决定

变量使用注意

变量需要先声明在使用
变量名不要以数字开头

输入输出

输入
scanf("%d",&num);
1、双引号中除了占位符,不要写任何其它东西,否则输入时会产生错误
2、占位符只是代表从键盘输入一个数,需要跟上输入数据存放的变量名,错误写法:scanf("%d",num);存放数据的变量名需要取地址
3、当连续输入多个变量时,分开写

#include <stdio.h>
#include <stdlib.h>
int main()
{
	int a;
    int b;
    int num;
    printf("输入第一个加数:\n");
    scanf("%d",&a);
    printf("输入第二个加数:\n");
    scanf("%d",&b);
    //scanf("%d%d",&a,&b);
    num = a + b;
    
    printf("计算结果:%d+%d=%d\n",a,b,num);

	system("pause");
	return 0;
}

scanf("%d%d",&a,&b); 通过这样的写法可以同时输入两个数据,但切记不能在占位符中间加上了其他字符。

scanf("%d,%d",&a,&b); 不恰当的写法
在输入的时候,就需要在输入一个数据后,要再加上占位符中间的字符,后面在输入数据才能有效的存入变量

输出

printf(“hello world!\n”);
printf(“计算结果:%d+%d=%d\n”,&a,&b,&c);

输出时,逗号后面的变量要根据占位符的顺序存放

编写程序思路

1、先用汉语把需要编写的步骤功能写下来

  • 变量声明
  • 提示用户
  • 获取输入
  • 计算数据
  • 打印结果

2、要记住基本的编写框架

  • 头文件
  • 程序入口
#include <stdio.h>
int main()
{
	return 0;
}

3、实际编写操作

#include <stdio.h>
#include <stdlib.h>
int main()
{
	//0、声明变量
	int a;
    int b;
    int c;
    //int a,b,c;
    //1、提示用户输入第一个数
    printf("输入第一个加数:\n");
    //2、获取用户输入的第一个数
    scanf("%d",&a);
    //3、提示用户输入第二个数
    printf("输入第二个加数:\n");
    //4、获取用户输入的第二个数
    scanf("%d",&b);
    //5、计算数据
    c = a + b;
    //6、打印计算的数据结果
    printf("计算结果:%d+%d=%d\n",a,b,c);

	system("pause");
	return 0;
}

同类型的变量声明可以这样写
int a,b,c;

选择语句if else

通过条件控制程序选择执行不同的功能,不同的计算

当(条件){
做什么
}当(条件){
做什么
}否则{
做什么
}

条件有

  • 相等 ==
  • 大于 >
  • 小于 <
  • 与 &
  • 或 |
  • 非 !=

if(a == 0){
	printf("hello world!\n");
}else if(a == 1){
	printf("a=1\n");
}else{
	printf("no flie\n");
}

选择语句分支嵌套

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int num = 0;
    int num2 = 0;
    int num3 = 0;
    printf("input a num:\n");
    scanf("%d",&num);
    if(num >= 10){
		printf("num=%d\n",num);
        printf("input a num2:\n");
        scanf("%d",&num2);
        if(num2 == 18){
			num3 = num + num2;
			printf("%d+%d=%d\n",num,num2,num3);
        }else{
			printf("no is 18\n");
        }
    }else{
		num = 0;
		printf("num<10\n");
    }
    printf("result:\nnum=%d\nnum2=%d\nnum+num2=%d\n",num,num2,num3);

	system("pause");
	return 0;
}

成绩划分案例

#include <stdio.h>

int main()
{
	int score;
    printf("请输入成绩:\n");
    scanf("%d",&score);
    if(score >90){
		printf("优秀!\n");
    }else if(75 < score && score <= 90){//需要同时满足大于75,小于等于90的条件
		printf("良好!\n");
    }else if(60 < score && score <= 75){
		printf("及格!\n");
    }else if(40 < score && score <= 60){
		printf("不及格!\n");
    }else{
		printf("很差劲,退学!\n");
    }
    
    system("pause");
    return 0;
}

多分支选择

#include <stdio.h>

int main()
{
	int data;
    
    printf("please input a interager:\n");
    scanf("%d",&data);
    
    switch(data){
		case 0:
        case 1:
		case 2:
		case 3:
        case 4:
        	printf("data=0~4\n");
			break;
		case 5:
        	printf("data=%d\n",data);
			break;
		case 6:
        	printf("data=%d\n",data);
			break;
		case 7:
        	printf("data=%d\n",data);
			break;
		case 8:
        	printf("data=%d\n",data);
			break;
        default:
			printf("input one a 0~8 num!\n");
			break;
    }
    system("pause");
    return 0;
}

成绩划分案例优化

#include <stdio.h>

int main()
{
	int data;
    
    printf("请输入成绩:\n");
    scanf("%d",&data);
    if(data == 100){
		printf("恭喜考了%d\n",data);
    }else{
		switch(data/10){
			case 0:
			case 1:
			case 2:
			case 3:
				printf("很差劲,退学!\n");
				break;
			case 4:
			case 5:
				printf("不及格!\n");
				break;
			case 6:
			case 7:
				printf("及格!\n");
				break;
			case 8:
				printf("良好!\n");
				break;
			case 9:
			//case 10:
				printf("优秀!\n");
				break;
			default:
				printf("非法成绩!\n");
				break;
		}
    }
    
    system("pause");
    return 0;
}

循环控制语句

while循环语句:

第一种循环语句
死循环

#include <stdio.h>

int main()
{
	int score;
    while(1){
        printf("请输入成绩:\n");
		scanf("%d",&score);
		if(score >90){
			printf("优秀!\n");
		}else if(75 < score && score <= 90){
			printf("良好!\n");
		}else if(60 < score && score <= 75){
			printf("及格!\n");
		}else if(40 < score && score <= 60){
			printf("不及格!\n");
		}else{
			printf("很差劲,退学!\n");
		}
    }
    
    system("pause");
    return 0;
}

第二种循环语句
循环可控次数

#include <stdio.h>

int main()
{
	int score;
	int cat = 0;//循环初始条件
    while(1){
		cat = cat + 1;//条件发生改变
        printf("请输入%d次成绩:\n",cat);
		scanf("%d",&score);
		if(score >90){
			printf("优秀!\n");
		}else if(75 < score && score <= 90){
			printf("良好!\n");
		}else if(60 < score && score <= 75){
			printf("及格!\n");
		}else if(40 < score && score <= 60){
			printf("不及格!\n");
		}else{
			printf("很差劲,退学!\n");
		}
        if(cat == 6){
			break;//终止,跳出循环
        }
    }
    
    system("pause");
    return 0;
}

成绩划分案例优化

#include <stdio.h>

int main()
{
	int data;
    int a;
    while(a){
		printf("请输入成绩:\n");
		scanf("%d",&data);
		if(data == 100){
			printf("恭喜考了%d\n",data);
		}else{
			switch(data/10){
				case 0:
				case 1:
				case 2:
				case 3:
					printf("很差劲,退学!\n");
					break;
				case 4:
				case 5:
					printf("不及格!\n");
					break;
				case 6:
				case 7:
					printf("及格!\n");
					break;
				case 8:
					printf("良好!\n");
					break;
				case 9:
				//case 10:
					printf("优秀!\n");
					break;
				default:
					printf("非法成绩!\n");
					break;
			}
		}
        printf("继续测试输入1\n");
        scanf("%d",&a);
        if(a != 1){
			break;
        }
    }
    
    system("pause");
    return 0;
}

for循环语句:

基本语句

#include <stdio.h>

int main()
{
	int i;
    for(i = 0;i < 5;i++){
        printf("hello world!\n");
    }
    system("pause");
    return 0;
}

实际案例

#include <stdio.h>

int main()
{
	int score;
	int cat;
    for(cat = 0;cat < 5;cat = cat + 1){
        printf("请输入%d次成绩:\n",cat+1);
		scanf("%d",&score);
		if(score >90){
			printf("优秀!\n");
		}else if(75 < score && score <= 90){
			printf("良好!\n");
		}else if(60 <= score && score <= 75){
			printf("及格!\n");
		}else if(40 < score && score < 60){
			printf("不及格!\n");
		}else{
			printf("很差劲,退学!\n");
		}
    }
    
    system("pause");
    return 0;
}

do…while循环

会在while的条件为0时也执行一次

#include <stdio.h>

int main()
{
    do{
        printf("hello world!\n");
    }while(0);
    system("pause");
    return 0;
}

数量控制
计算控制变量初始值要赋值为0

#include <stdio.h>

int main()
{
	int i = 0;
    do{
		i++;
        printf("hello world!\n");
        if(i == 3){
			break;
        }
    }while(1);
    system("pause");
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值