当前位置:文档之家› 数据类型

数据类型

%f浮点数、十进制记数法
%g根据数值不同自动选择%f或%e
%G根据数值不同自动选择%f或%E
%i有符号十进制整数
%o无符号八进制整数
%p指针
%s字符串
%u无符号十进制整数
%x使用十六进制数字0f的无符号十六进制整数
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);
printf("dec=%d;octal=%#o;hex=%#x\n",x,x,x);
return 0;
}
输出结果为:
dec=100,octal=144;hex=64 dec=100;octal=0144;hex=0X64
//目前一般的情况是,long long类型为64位,long类型为32位,short类型为16位,int类型为16位或32位(依机器的自然字大小而定)
/*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);
//现代编译器的主要工作流程:源代码(source code)→预处理器(preprocessor)→编译器(compiler)→汇编程序(assembler)→目标代码(object code)→链接器(Linker)→可执行程序(executables)。
#include stdio.h int main(void)
Your naem of 6letters occupies 40 memory cells.
The phrase of praise has 28 letters and occupies 29 memory cells.
//scanf()函数开始读取输入,会在遇到第一个空白字符空格(blank)、制表符(tab)或者换行符(newline)处停止读取。
运算符: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);
getchar();//使程序暂停
--
在C语言中,所有变量都必须在使用之前定义。声明变量被认为是一个好的编程技术。变量名第一个字符必须是字母或者下划线,而且区分大小写。
--
#include stdio.h void butler void;
int main(void)
{
printf("the first line\n");
{
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]键
printf("and we have%d bytes to store it in.\n",size);
return 0;
}
//char数组类型和空字符
C用空字符[message]来标记字符串的结束,空字符不是数字0,是非打印字符,其ASCII码的值为(或者等同于)0。C的字符串存储时通常以这个空字符结束。该字符的存在意味着数组的单元数必须至少比要存储的字符数多1。
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");
printf("Biggest int:%d\n",INT_MAX);
printf("Smallest unsigned long:%lld\n",LLONG_MIN);
printf("One byte=%d bits on this system.\n",CHAR_BIT);
printf("Largest double:%e\n",DBL_MAX);
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);
//sizeof运算符以字节为单位给出数据的大小;strlen()函数以字符为单位给出字符串的长度。
---
(符号)常量和C预处理器
#define NAME value定义的常量通常被称为明显常量(manifest constant)
const int NAME=value修饰符const把一个变量声明转换成常量声明
enum
//C头文件limits.h和float.h分别提供有关整数类型和浮点类型的大小限制的详细信息。每个文件都定义了一系列应用于实现的明显常量。
limits.h中的一些符号常量:CHAR_BIT CHAR_MAX CHAR_MIN SCHAR_MAX SCHAR_MIN UCHAR_MAX SHRT_MAX SHRT_MIN USHRT_MAX INT_MAX INT_MIN UINT_MAX LONG_MAX LONG_MIN ULONG_MIN LLONG_MAX LLONG_MIN ULLONG_MAX float.h中的一些符号常量:FLT_MANT_DIG FLT_DIG FLT_MIN_10_EXP FLT_MAX_10_EXP FLT_MIN FLT_MAX FLT_EPSILON
printf("The phrase of praise has%d leters and occupies%d memory cells.\n",strlen(PRAISE),sizeof PRAISE);
return 0;
}
输出结果:
What's your name?
Morgan Buttercup Hello,Morgan.What asuper marvelous name!
关键字是C语言的词汇
---
printf()函数用于输出语句和变量的值
基本数据类型使用11个关键字:unsigned int short long char float double signed _Bool(表示布尔值true和false)_Complex(表示复数)_Imaginary(表示虚数)void
数组就是同一类型的数据元素的有序序列。
/*praise.c--使用不同类型的字符串*/
#include stdio.h
#include string.h//提供strlen()函数原型
#define PRAISE"What asuper marvelous name!"
int main(void)
{
//defines.c--使用limits.h和float.h中定义的常量
#include stdio.h
#include limits.h//整数限制
#include float.h//浮点数限制
int main(void)
{
printf("Some number limits for this system:\n");
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);
char类型是用于存储字母和标符号之类的字符。
char grade='A';//输出65 char grade="A"//输出A
单引号技术适用于字符、数字和标点符号,特殊符号用转义符
相关主题