【C/C++】编程基础


基本结构

写一个简单但完整的C程序。

利用printf 函数在屏幕上显示输出。

简单C程序的结构。

书写C程序的基本原则。

代码设计

#include
void main(void)
{
    printf("This is C!");
}

结果显示

格式化输出

格式化输出

回车

代码设计

#include
void main(void)
{
	printf("Welcome to");
	printf("China!");
	printf("\nHow do we\njump\n\ntwo lines?\n");
	printf("\n");
	printf("It will rain\ntomorrow\n");
}

结果显示

其他转义字符

显示转义字符

代码展示

#include 
void main(void)
{
	printf("Listen to the beep now.\a");
	printf("\nWhere is the 't' in cat \b?\n\n");
	printf("I earned $50 \rWhere is the money?\n");
	printf("The rabbit jumps \t\t two tabs.\n\n");
	printf("Welcome to\
			New York!\n\n");
	printf("From "			"Russia \
			with "			"Love.\n");
	printf("Print 3 double quotes	-\" \" \" \n");
}

结果显示

变量:命名、声明、赋值和打印值

命名变量

声明数据类型

使用赋值语句

显示变量的值

基本的赋值语句

代码设计

#include 
void main(void)
{
	int month;
	float expense,income;
	month=12;
	expense=111.1;
	income=100.;
	printf("Month=%2d,Expense=$%.2f\n",month,expense);
	month=11;
	expense=82.1;
	printf("For the %2dth month of the year\n"
			"the expenses were $%5.2f \n"
			"and the income was $%6.2f\n\n",month ,expense,income);
}

结果显示

算数运算符和表达式

运算数

算数运算符和他们的特点

算数表达式

代码设计

#include 
void main(void)
{
	int i,j,k,p,m,n;
	float a,b,c,d,e,f,g,x,y;
	
	i=5; j=5;
	k=11;p=3;
	x=3.0;y=4.0;
	printf("......Initial values ......\n");
	printf("i=%4d,j=%4d\nk=%4d,p=%4d\nx=%4.2f,y=%4.2f\n\n",i,j,k,p,x,y);
	a=x+y;
	b=x-y;
	c=x*y;
	d=x/y;
	e=d+3.0;
	f=d+3;
	i=i+1;
	j=j+1;
	printf(".....Section 1 output ......\n");
	printf("a=%5.2f,v=%5.2f\nc=%5.2f,d=%5.2f\ne=%5.2f f==%5.2f\ni==%5.d,%5d\n\n",a,b,c,d,e,f,i,j); 
	
	m=k%p;
	n=p%k;
	i++;
	++j;
	e--;
	--f;
	
	printf(".....Section 2 output ......\n");
	printf("m=%4d,n=%4d\ni=%4d,j=%4d\ne=%4.2f,f=%4.2f\n",m,n,i,j,e,f);

	
}

结果显示

从键盘输入数据

使用scanf()函数

从键盘输人数据

地址操作符&

double数据类型

代码设计

#include 
void main(void)
{
	float income;
	double expense;
	int month,hour,minute;
	
	printf("What month is it?\n");
	scanf("%d",&month);
	printf("You have entered month=%5d\n",month);
	printf("Please enter your income and expenses\n");
	scanf("%f %1f",&income,&expense);
	printf("Entered income=%8.2f,expenses=%8.2lf\n",income,expense);
	printf("Please enter the time, e.g.,12:45\n");
	scanf("%d : %d",&hour,&minute);
	printf("Entered Time = %2d:%2d\n",hour,minute);
}

结果显示

常量宏及打印变量值的进一步讨论

用define指令来定义常量

更多的转换限定符及组成

科学计数法

转换限定符中的标志

代码设计

#include 
#define DAYS_IN_YEAR 365
#define PI 3.14159

void main(void)
{	
	float income =1234567890.12;
	
	printf("CONVERSION SPECIFICATIONS FOR INTEGERS \n\n");
	printf("Days in year = \n"
			"[[%1d]] \t(field width less than actual)\n" 
			"[[%9d]] \t(field width greater than actual)\n"
			"[[%d]]  \t(no field width specified)  \n\n\n",
			DAYS_IN_YEAR, DAYS_IN_YEAR,DAYS_IN_YEAR);
	printf("CONVERSION SPECIFICATIONS FOR REAL NUMBERS\n\n");
	printf("Cases for precision being specified correctly \n");
	printf("PI = \n"
		   "[[%1.5f]] \t\t(field width less than actual) \n"
		   "[[%15.5f]] \t(field width greater than acutal) \n"
		   "[[%.5f]] \t\t(no field width specified) \n\n",
		   PI,PI,PI);
	
	printf("Cases for field width being specified correctly \n");
	printf("PI = \n"
		   "[[%7.2f]] \t\t(precision less than actual) \n"
		   "[[%7.8f]] \t\t(precision greater than actual) \n"
		   "[[%7.f]] \t\t(no precision specified) \n\n",
		   PI,PI,PI);
	
	printf("PRINTING SCIENTIFIC NOTATION \n\n");
	printf("income = \n"
		   "[[%18.2e]] \t(field width large,precision small) \n"
		   "[[%8.5e]] \t(field width and precision medium size) \n"
		   "[[%4.1e]] \t\t(field width and precision small) \n"
		   "[[%e]]    \t(no specifications) \n\n",
		   income, income,income,income   
		   );
	
	printf("USING A FLAG IN CONVERSION SPECIFICATIONS \n\n");
	printf("Days in year= \n"
		   "[[%-9d]] \t\t(field width large,flag included)\n",
		   DAYS_IN_YEAR
	);
	
}

结果显示

混合类型的运算、复合赋值、运算符优先级和类型转换

算术运算符的优先级

初始化变量

算术运算中的陷阱

在算术表达式中混合使用整型数和实型数

类型转换

副作用

代码显示

#include
void main(void)
{
	int i=1,j=1,
		k1=10,k2=20,k3=30,k4=40,k5=50,
		k,h,m,n;
	float a=7,b=6,c=5,d=4,
		  e,p,q,x,y,z;
	printf("Before increment, i=%2d,j=%2d\n",i,j);
	
	k=i++;
	h=++j;
	
	printf("After increment, i=%2d,j=%2d\n"
		   "				 k=%2d,h=%2d\n\n",i,j,k,h);
	m=6/4;
	p=6/4;
	n=6/4.0;
	q=6/4.0;
	
	printf("m=%2d,p=%3.1f\nn=%2d,q=%3.f\n\n",m,p,n,q);
	printf("Original k1=%2d, k2=%2d, k3=%2d, k4=%2d,k5=%2d\n",k1,k2,k3,k4,k5);
	
	k1 += 2;
	k2 -= i;
	k3 *= (8/4);
	k4 /= 2.0;
	k5 %= 2;
	
	printf("New k1=%2d, k2=%2d,k3=%2d,k4=%2d,k5=%2d\n\n",
		  k1,k2,k3,k4,k5);
	e=3;
	x=a+b-c/d*e;
	y=a+(b-c)/d*e;
	z=((a+b)-c/d)*e;
	
	printf("a=%3.0f,b=%3.0f,c=%3.0f\nd=%3.1f,e=%3.1f\n\n",
		   a,b,c,d,e);
	
	printf("x= a + b -c  /d *e = %10.3f \n"
		   "y= a +(b -c) /d *e = %10.3f \n"
		   "z=((a + b)-c /d)*e = %10.3f\n", x,y,z); 
}

结果显示

相关