1. Basic concepts
1. Data: The object processed by the computer is data;
2. Data type: a type of data with the same characteristics;
3. Data structure: There are often some connections between different data, forming a certain data structure;
4. C language data includes constants and variables, both constants and variables have types;
2. Data types
1. Data types: basic types, structure types, pointer types, empty types;
2. Basic types: integer, floating point, Character
type ; 3. Structure type: array type, structure type, union type, enumeration type;
3. Value range
char _______________字节数: 1,值范围:-128―127
short ______________字节数: 2,值范围:-32768―32767
int ________________字节数: 4,值范围:-2147483648―2147483647
long _______________字节数: 4,值范围:-2147483648―2147483647
long long __________字节数: 8,值范围:-9223372036854775808―9223372036854775807
unsigned char ______字节数: 1,值范围:0―255
unsigned short _____字节数: 2,值范围:0―65535
unsigned int _______字节数: 4,值范围:0―4294967295
unsigned long ______字节数: 4,值范围:0―4294967295
unsigned long long _字节数: 8,值范围:0―18446744073709551615
Four. Format control
1. Integer data
有符号十进制整型数据i=1234输出:
%d=1234 %hd=1234 %ld=1234 %lld=1234
无符号十进制整型数据i=1234输出:
%u=1234 %hu=1234 %lu=1234 %llu=1234
无符号八进制整型数据i=1234输出:
%o=2322 %ho=2322 %lo=2322 %llo=2322 %#o=02322
无符号16进制整型数据i=1234输出:
%x=4d2 %hx=4d2 %lx=4d2 %llx=4d2 %X=4D2 %#x=0x4d2 %#X=0X4D2
其它控制printf输出i=1234格式:
% d= 1234
%+d=+1234
%+d=-1234
%8d= 1234
%-8d=1234
%08d=00001234
%+08d=-0001234
% 08d=-0001234
2. Floating point data
浮点型数据常用格式:
%f = 12345678.123457
%e = 1.234568e+07
%E = 1.234568E+07
%g = 1.23457e+07
%G = 1.23457E+07
%g = 1234.12
%G = 1234.12
浮点型数据控制格式:
% f = 12345678.123457
%+f = +12345678.123457
%20f = 12345678.123457
%-20f = 12345678.123457
%020f = 0000012345678.123457
%.10f = 12345678.1234567892
%+025.10f = +0000012345678.1234567892
%*.*f = 12345678.123 //(%*.*f \n", 30, 3, i);
3. Character/string/pointer data
字符"A"输出的各种形式:
%c = A
%hhd = 65
%-10c = A
%10c = A
%010c = 000000000A
字符串"ABCD"输出的各种形式:
%s = ABCD
%-10s = ABCD
%10s = ABCD
%010s = 000000ABCD
%.2s = AB
%5.2s = AB
%05.2s = 000AB
%-05.2s = AB
%*.*s = ABC //参数5,3;
指针输出的三种形式:
%p = 00057D14
%-10p = 00057D14
%10p = 00057D14
V. Integer constants The
data type can be seen by writing:
123 304 25278 1 0 906 //Decimal int type data
123L 304L 25278L 1L 0l 906l //The last digit plus l/L defaults to long type, and the
data is expressed in different hexadecimals:
0236 0527 06254 //Add the number 0 at the beginning to indicate the octal number
0531L 0765432L //Add the letter l/L at the end to indicate the long type
0x2073 0xA3B5 //Add 0x/0X at the beginning to indicate hexadecimal number;
0XABCD 0XF0F00000L /L/L can be added at the end to indicate long type
6. Floating-point constants
The representation of floating- point data
3.2 3. 0.038 .05 2E-3 2.45e17 105.4E-10 304.2E8 //Double type, scientific notation can be used Represents
float and long double constants
13.2f -1.7853E-2F 24.68700f .32F 0.33f //float type, add F or f after the number
12.89L 3.47E34L .05L 1.L //long double type, after the number Add L or l
Seven. Symbolic constants
1. Use symbolic constants for easy writing and unified modification
#include <stdio.h>
#define PI 3.1415926f
int main()
{
float 圆半径, 圆周长, 圆面积;
printf("请输入圆的半径: ");
scanf_s("%f", &圆半径);
圆周长 = 2 * PI * 圆半径;
圆面积 = PI * 圆半径 * 圆半径; //圆周率用PI表示后,书写不容易出错,且方便修改;
printf("\n圆的周长 = %.2f\n圆的面积 = %.2f\n", 圆周长, 圆面积);
}
2. Using symbolic constants, it is not easy to confuse the same data
#include <stdio.h>
#define 商品单价 30
#define 限定年龄 30 //使用符号常量后,不容易混淆;
int main()
{
int 购买数量, 实际年龄, 购买价格;
printf("请输入您的实际年龄和购买商品的数量: ");
scanf_s("%d %d", &实际年龄, &购买数量);
if (实际年龄 < 限定年龄)
{
购买价格 = 购买数量 * 商品单价;
printf("购买价格 = %d元\n", 购买价格);
}
else
{
printf("您年龄超过30岁,不能购买!\n");
}
return 0;
}