定义变量。

a、变量的基本介绍(c语言是一种有类型的语言,使用之前必须先定义)

eg:int price=0(其中变量的名字是price,类型是int变量,初始值为零) 。 变量就是一个储存数据的地方,用一个变量保存了数据,它才能参加到后面的计算中。

b、变量定义

定义变量的一般形式就是: <类型名称><变量名称> eg:int price;int amount;int price,amout;

c、变量的名字

变量的名字就是一种标识符。

d、赋值和初始化

eg:int price=0 “=”就是一个赋值的运算符。就是将右边的值赋值给左边的变量。在定义变量的时候就开始赋值则称之为初始化。 定义初始值时应注意:eg:int price=0;int amount=100;int price=0,amout=100;组合定义时应单独定义;(注:c99标准时变量在何处都可以定义,而在ansi c则只能在开头统一定义)

e、 读整数

scanf(“%d“,&printf) scanf(输入),printf(输出) ,printf前需要加&

f、常量

写法一(不定义常量):

int change =100 -price    //写法二(先初始化常量): const int AMOUNT(注:全大写是为了强调const) =100(推荐使用) <定义100这个常量>                 
int change =AMOUNT -price;//当const放               在int前用于给这个变量加一个const(不变的)属性。
const int AMOUNT = 100;//(注:全大写是为了强调const,固定票面为100)
int price = 0;
printf("请输入金额(元): \n");
scanf("%d",&price);
int change = AMOUNT - price;
printf("找您%d元。\n", change);
return 0;
}
 

g、如何让用户输入变量AMOUT的值,而不是一个固定的初始值。

int amount= 100; //(去掉const,使票面可变) 
int price = 0;                
	printf("请输入金额(元): ");  
	scanf("%d",&price); 
	printf("请输入票面"); 
	scanf("%d",&amount);                
	int change = amount - price; 
	printf("找您%d元。\n", change); 
	return 0;            
printf("请输入金额(元): "); 
	scanf("%d",&price);                
	printf("请输入票面");  
	scanf("%d",&amount);                
		int change = amount - price; 
		printf("找您%d元。\n", change);  
		return 0;           
h、浮点数:(就是带小数点的数)

1、double:双精度浮点数

2、float:单精度浮点数。


printf("请输入身高的英尺和英寸,"~~                    
			"如输入\”5  7\“,表示5英尺7英寸");~~
int foot;//(定义英尺)~~                
int inch;//(定义英寸)~~                
scanf("%d %d",&foot, &inch);~~                
printf("身高为%f米。\n",~~                      
((foot + inch / 12) * 0.3048));~~                                
return 0;  //此程序会报错~~

改法1、

改法1、
printf("请输入身高的英尺和英寸,"                      
       		"如输入\"5  7\",表示5英尺7英寸");              
int foot;//(定义英尺)    
int inch;//(定义英寸)                
scanf("%d %d",&foot, &inch);                
printf("身高为%f米。\n",                      
((foot + inch / 12.0) * 0.3048));//注:将12改为12.0变为浮点数                               
return 0;

改法2、

printf("请输入身高的英尺和英寸,"                     
       					"如输入\"5  7\",表示5英尺7英寸");         
double foot;//(定义英尺)             
double inch;//(定义英寸)(注:此时已经将英尺和英寸改为double)
scanf("%lf %lf",&foot, &inch);                
printf("身高为%f米。\n",                      
((foot + inch / 12) * 0.3048));                                
return 0;     
· 数据类型

整数型: *int

*printf("%d",...)

*scanf("%d",...)

带小数点的数

*double(双精度)

*printf(“%f”,...)

*scanf("%lf",...)

i、表达式
I、运算符:

eg: *运算符:amount = x (1+0.033)(1+0.033);//这一行都是表达式 a=b+5;//"=,+"都是运算符''a,b,5"则为算

​ sides=sides+5,sides和5为算子

​ %:取余():括号

int hour1,minute1;                 

int hour2,minute2;                 

scanf("%d %d",&hour1, &minute1);                  

scanf("%d %d",&hour2, &minute2);                    

int t1=hour1 * 60 + minute1;                  	

int t2=hour2 * 60 + minute2;//将小时全部化为分钟来计算 

int t =t2-t1;                  

printf("时间差是%d小时%d分钟。",t/60, t%60);//"t/的单位是分钟,因为计算机的整数运算原理,只会保留前面的整数,而"是取余数只会保留余数"//			
II**、运算符的优先级

第一优先级:“-”和“+” +:单目不变。 -:单目取负 。“ a+b"先算+b 然后用a*+b 算子肯定在运算符右边。 *

第二优先级:”,/,%,“,与数学算法差不多。

第三优先级:”+,-“

第四优先级:”=“,赋值号。 eg:”a=b+5“,因为+的优先级比=高 III、复合的赋值运算符 eg:total += 5; 等价于 total =total + 5;(运算符间不能有空格,需连接在一起。)

递增/递减运算符 (必须和变量配合使用) 规律:a++ 先算+1 ,a++后算+1

相关文章:

  • 2022-12-23
  • 2021-09-21
  • 2022-12-23
  • 2021-10-09
  • 2021-12-05
  • 2022-01-29
  • 2022-02-08
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-08-23
  • 2021-12-19
  • 2022-03-03
  • 2021-12-08
  • 2022-01-22
  • 2022-12-23
相关资源
相似解决方案