ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

【C/C++】编程基础

2022-03-27 23:03:05  阅读:185  来源: 互联网

标签:void 编程 基础 C++ month income printf 4d expense


基本结构

写一个简单但完整的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);
}

结果显示

标签:void,编程,基础,C++,month,income,printf,4d,expense
来源: https://www.cnblogs.com/yyyyfly1/p/16065050.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有