【C/C++】编程基础

2022/3/28 1:23:03

本文主要是介绍【C/C++】编程基础,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

基本结构

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

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

简单C程序的结构。

书写C程序的基本原则。

代码设计

#include<stdio.h>
void main(void)
{
    printf("This is C!");
}

结果显示

格式化输出

格式化输出

回车

代码设计

#include<stdio.h>
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 <stdio.h>
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 <stdio.h>
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 <stdio.h>
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 <stdio.h>
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);
}

结果显示



这篇关于【C/C++】编程基础的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程