C1数据类型
1.定义程序目标--2.设计程序--3.编写代码--4.编译--5.运行程序--6.测试和调试程序--7.维护和修改程序
//C编译器和链接器是将C语言代码转换成可执行代码的程序。
//编译器的作用是将源代码编译成目标代码,即机器语言代码
//链接器的作用是将目标代码、系统的标准启动代码和库代码结合在一起
//现代编译器的主要工作流程:源代码(source code)→预处理器(preprocessor)→编译器(compiler)→汇编程序(assembler)→目标代码(object code)→链接器(Linker)→可执行程序(executables)。
#include stdio.h int main(void)
int dogs;
printf("How many dogs do you have?\n");
scanf("%d",&dogs);
printf("So you have%d dog(s)!\n",dogs);
return 0;
getchar();//该行读取一次按键,如读取[Enter]键
getchar();//使程序暂停
在C语言中,所有变量都必须在使用之前定义。声明变量被认为是一个好的编程技术。变量名第一个字符必须是字母或者下划线,而且区分大小写。
#include stdio.h void butler void;
int main(void)
printf("the first line\n");
butler();
printf("the third line\n");
return 0;
void butler(void)
printf("the second line\n");
C语言的ISO/ANSI关键字列表
auto break case char const continue default do double else enum extern float for goto if inline int long register restrict return short signed sizeof static struct switch typedef union unsigned void volatile while _Bool _Complex _Imaginary
关键字是C语言的词汇
printf()函数用于输出语句和变量的值
基本数据类型使用11个关键字:unsigned int short long char float double signed _Bool(表示布尔值true和false)_Complex(表示复数)_Imaginary(表示虚数)void
运算符:sizeof()//圆括号对类型是必须的,而对于具体量是可选的
赋值函数:scanf()//为程序提供键盘输入
/*bases.c--以十进制、八进制和十六进制形式输出100*/
#include stdio.h int main(void)
int x=100;
printf("dec=%d;octal=%o;hex=%x\n",x,x,x);
printf("dec=%d;octal=%#o;hex=%#x\n",x,x,x);
isalpha 函数return 0;
输出结果为:
dec=100,octal=144;hex=64 dec=100;octal=0144;hex=0X64
/
/目前一般的情况是,long long类型为64位,long类型为32位,short类型为16位,int类型为16位或32位(依机器的自然字大小而定)
char类型是用于存储字母和标符号之类的字符。
char grade='A';//输出65 char grade="A"//输出A
单引号技术适用于字符、数字和标点符号,特殊符号用转义符
\a警告\b退格\f走纸\n换行\r回车\t水平制表符\v垂直制表符\反斜杠\'单引号\"双引号\?问号[message]oo八进制\xhh十六进制
/*charcode.c--显示一个字符的编码值*/
#include stdio.h int main(void)
char ch;
printf("Please enter acharacter.\n");
scanf("%c",&ch);
printf("The code for%c is%d.\n",ch,ch);
return 0;
浮点值的上溢和下溢
很多科学和工程计算需要复数和虚数。
/*escape.c--使用转义字符*/
#include stdio.h int main(viod)
float salary:
printf("\aEnter your desired monthly salary:");
printf("$_\b\b\b\b\b\b\b");
scanf("%f",&salary);
printf("\n\t$%.2f amonth is$%.2f ayear.",salary,salary*12.0);
printf("\rGee!\n");
return 0;
输出结果:
Enter your desired monthly salary:00.00 Gee!00.00 amonth is000.00 ayear.
/*printf("Enter your desired monthly salary:\n");
scanf("%f",&salary);*/
使用scanf()和printf()读取和显示字符串,使用strlen()函数获取字符串的长度
使用C预处理器的#define指令和ANSI C的const修饰符创建符号常量
//taldback.c--一个能为您提供一些信息的对话程序
#include stdio.h
#include string.h//提供strlen()函数的原型
#define DENSITY 62.4//人的密度(单位是:英磅/每立方英尺)
int main()
float weight,volume;
int size,letters;
char name[40];//name是一个有40个字符的数组
printf("Hi!What's your first name?\n");
scanf("%s",name);
printf("%s,what's your weight in pounds?\n",name);
scanf("%f",&weight);
size=sizeof name;
letters=strlen(name);
volume=weight/DENSITY;
printf("Well,%s,your volume is%2.2f cubic feet.\n",name,volume);
printf("Also,your first name has%d letters,\n",letters);
printf("and we have%d bytes to store it in.\n",size);
return 0;
//char数组类型和空字符
C用空字符[message]来标记字符串的结束,空字符不是数字0,是非打印字符,其ASCII码的值为(或者等同于)0。C的字符串存储时通常以这个空字符结束。该字符的存在意味着数组的单元数必须至少比要存储的字符数多1。
数组就是同一类型的数据元素的有序序列。
/
*praise.c--使用不同类型的字符串*/
#include stdio.h
#include string.h//提供strlen()函数原型
#define PRAISE"What asuper marvelous name!"
int main(void)
char name[40];
printf("What's your name?\n");
scanf("%s",name);
printf("Hello,%s.%s\n",name,PRAISE);
printf("Your name of%d letters occupies%d memory cells.\n",strlen(name),sizeof name);
printf("The phrase of praise has%d leters and occupies%d memory cells.\n",strlen(PRAISE),sizeof PRAISE);
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论