sizeof运算符、size_t和typedef声明

一、sizeof运算符

C语言中将表示字符的char型长度定义为1。

通过是用sizeof运算符(sizeof operator),写作sizeof(数据类型名称)可以判断出包括char型在内的所有数据类型的长度。

 

该运算符不仅仅是上面这种形式,还可以像这样使用:sizeof (表达式)

要判断数据类型的长度时使用前者,要判断变量或表达式的长度时使用后者。

 

#include <stdio.h>

int main(int argc, char *argv[])
{
	int a,b;
	char x,y;

	printf("sizeof(int) = %d\n", sizeof(int));
	printf("sizeof(char) = %d\n", sizeof(char));

	printf("sizeof(a) = %d\n", sizeof(a));
	printf("sizeof(x) = %d\n", sizeof(x));

	printf("sizeof(a + b) = %d\n", sizeof(a + b));
	printf("sizeof(x + y) = %d\n", sizeof(x + y));
	printf("sizeof(a + x) = %d\n", sizeof(a + x));

	return 0;
}

 

sizeof亦可求数组长度以及数组元素个数

#include <stdio.h>

int main(int argc, char *argv[])
{
	int str[5];

	printf("sizeof(str) = %d\n", sizeof(str));
	printf("sizeof(str) / sizeof(str[0]) = %d\n", sizeof(str) / sizeof(str[0]));

	return 0;
}

 

 

二、sizeof_t和typedef声明

由sizeof运算符生成的值的数据类型是在<stddef.h>头文件中定义的size_t型,在许多编译器中用typedef声明来定义size_t型。

typedef unsigned size_t;

typedef声明是数据类型的同义词,也就是为现有的数据类型创建别名(并非创建新的数据类型)。

通过这个声明,size_t就成了unsigned型的同义词。

这个同义词在语法上可以作为一种“数据类型名称”来使用。

sizeof运算符是不会生成负值的,所以将它定义为无符号整型。

 

printf函数中的“%lu”转换说明表示unsigned long型数值,“%u”转换说明则是表示unsigned型数值。

格式控制字符串中的转换说明必须和要显示的值的数据类型保持一致。

整型和字符型的取值范围

一、字符型和整型

字符型和整型是用来表示“一定的数值范围”的整数数据类型的。

char 0 – 255
signed char -128 – 127
unsigned char 0 – 255
signed int -2147483648 ~ 2147483647
signed long int -2147483648 ~ 2147483647
unsigned int 0 – 4294967295
unsigned long int 0 – 4294967295

 

二、字符型

char型是用来保存“字符”的数据类型。

基本数据类型和数

一、基本数据类型

整型,浮点型,字符型,枚举型。

 

二、基数

二进制,八进制,十进制,十六进制。

 

三、基数转换

1.将二进制数101转换为十进制数的步骤:

101 = 1 x 2² + 0 x 2² + 1 x 2²

= 5

2.将十六进制数1FD转换为十进制的步骤:

1FD = 1 x 16² + 15 x 16² + 13 x 16²

= 509

3.将八进制数123转换为十进制数的步骤:

123 = 1 x 8² + 2 x 8² + 3 x 8²

= 83

 

由十进制数向二进制数转换

二进制数有以下规律:

偶数的末尾数字为0,

奇数的末尾数字为1。

即用要转换的十进制数除以2所得的余数就是末位数字的值。

 

Windows——将一个文件附加到另一个文件

#include <stdio.h>
#include <Windows.h>									//调用Windows API需要加此头文件

int main(int argc, char *argv[])						
{
	HANDLE hFile;												
	HANDLE hAppend;										//声明句柄变量
	DWORD  dwBytesRead, dwBytesWritten, dwPos;			//DWORD等同于无符号的int
	BYTE   buff[4096];									//声明数组,该数组申请的内存空间为4096个字节

	hFile = CreateFile(TEXT("E:\\file1\\file1_1.txt"),	//注意Visual环境下路径的单个\为转义字符
		GENERIC_READ,											
		0,
		NULL,
		OPEN_EXISTING,
		FILE_ATTRIBUTE_NORMAL,
		NULL);											//注意被调用函数的参数个数不能少
	
	if (hFile == INVALID_HANDLE_VALUE)
	{
		printf("Could not open file1_1.txt.");

		return 0;										//返回值的类型要与调用其函数的类型相同
	}

	hAppend = CreateFile(TEXT("E:\\file1\\file1_2.txt"),
		FILE_APPEND_DATA,
		FILE_SHARE_READ,
		NULL,
		OPEN_ALWAYS,
		FILE_ATTRIBUTE_NORMAL,
		NULL);

	if (hAppend == INVALID_HANDLE_VALUE)
	{
		printf("Could not open file1_2.txt.");

		return 0;
	}

	while (ReadFile(hFile, buff, sizeof(buff), &dwBytesRead, NULL)
		&& dwBytesRead > 0)
	{
		dwPos = SetFilePointer(hAppend, 0, NULL, FILE_END);
		LockFile(hAppend, dwPos, 0, dwBytesRead, 0);
		WriteFile(hAppend, buff, dwBytesRead, &dwBytesWritten, NULL);
		UnlockFile(hAppend, dwPos, 0, dwBytesRead, 0);
	}

	CloseHandle(hFile);
	CloseHandle(hAppend);

	return 0;
}

 

Windows——遍历进程

#include <stdio.h>
#include <Windows.h>
#include <TlHelp32.h>

int main(int argc, char *argv[])
{
	HANDLE SnapshotHandle;
	PROCESSENTRY32 ProcessEntry;
	BOOL Result;
	
	SnapshotHandle = CreateToolhelp32Snapshot (TH32CS_SNAPPROCESS,0);

	if (INVALID_HANDLE_VALUE == SnapshotHandle)
	{
		return 0;
	}
	
	ProcessEntry.dwSize = sizeof(PROCESSENTRY32);
	Result = Process32First(SnapshotHandle, &ProcessEntry);
	while (Result)
	{
		printf("ProcessID = %d, ProcessName = %s\r\n", ProcessEntry.th32ParentProcessID, ProcessEntry.szExeFile);
		Result = Process32Next(SnapshotHandle, &ProcessEntry);
	}

	return 0;
}

 

函数调用及指针练习

练习:输入一个整数。如果是该整数为奇数则乘以2;为偶数则除以2。

#include <stdio.h>

void mul(int x, int *y)
{
	*y = x * 2;
}

void div(int x, int *y)
{
	*y = x / 2;
}

int main(int argc, char *argv[])
{
	int a;
	int b;
	printf("请输入一个整数:");
	scanf("%d", &a);

	if (a % 2 != 0)
	{
		mul(a, &b);
		printf("乘以2等于%d。\n", b);
	}
	else
	{
		div(a, &b);
		printf("除以2等于%d。\n", b);
	}

	return 0;

 

数组相关

一、一维数组

1.无初始值

int arr[100];

其中int表示整形,arr是数组的名字,[100]表示这是一个有100个int型变量的数组。即,内存为该数组分配了400个字节的空间。

2.赋予初始值

int arr[5] = {1,2,3,4,5};

其等同于

int arr[5];
arr[0] = 1;
arr[1] = 2;
arr[2] = 3;
arr[3] = 4;
arr[4] = 5;

3.声明一维数组清零并访问

#include <stdio.h>

int main(int argc, char *argv[])
{
	int len = 10;
	int arr[10];
	for (int a = 0; a < 10; a++)
	{
		arr[a] = 0;
	}
	for (int i = 0; i < len; i++)
	{
		printf("index %d,and value is %d\n", i, arr[i]);
	}
	
	return 0;
}

 

二、二维数组

声明二维数组,清零并访问。

#include <stdio.h>

int main(int argc, char *argv[])
{
	int arr[3][4];
	//clear arr
	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 4; j++)
		{
			arr[i][j] = 0;
		}			
	}
	//access arr
	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 4; j++)
		{
			printf("index(%d,%d),%d\n",i,j,arr[i][j]);
		}
	}

	return 0;
}

 

声明二维数组并赋初始值。

int arr[3][4] = 
{
	{1,2,3,4},
	{5,6,7,8},
	{9,10,11,12}
};

 

三、字符数组

char arr[] = "gudako";

其等同于

char arr[] = { 'g','u','d','a','k','o','\0'};

表示内存为该数组分配了7个字节的空间。

scanf——格式化输入说明

scanf格式说明符遵循以下原则:

%[*][width][length]specifier

specifier Description Characters extracted
i Integer Any number of digits, optionally preceded by a sign (+ or -).
Decimal digits assumed by default (0-9), but a 0 prefix introduces octal digits (0-7), and0x hexadecimal digits (0-f).
Signed argument.
d or u Decimal integer Any number of decimal digits (0-9), optionally preceded by a sign (+ or -).
d is for a signed argument, and u for an unsigned.
o Octal integer Any number of octal digits (0-7), optionally preceded by a sign (+ or -).
Unsigned argument.
x Hexadecimal integer Any number of hexadecimal digits (0-9, a-f, A-F), optionally preceded by 0x or 0X, and all optionally preceded by a sign (+ or -).
Unsigned argument.
f, e, g Floating point number A series of decimal digits, optionally containing a decimal point, optionally preceeded by a sign (+ or -) and optionally followed by the e or E character and a decimal integer (or some of the other sequences supported by strtod).
Implementations complying with C99 also support hexadecimal floating-point format when preceded by 0x or 0X.
a
c Character The next character. If a width other than 1 is specified, the function reads exactly widthcharacters and stores them in the successive locations of the array passed as argument. No null character is appended at the end.
s String of characters Any number of non-whitespace characters, stopping at the first whitespace character found. A terminating null character is automatically added at the end of the stored sequence.
p Pointer address A sequence of characters representing a pointer. The particular format used depends on the system and library implementation, but it is the same as the one used to format %pin fprintf.
[characters] Scanset Any number of the characters specified between the brackets.
A dash (-) that is not the first character may produce non-portable behavior in some library implementations.
[^characters] Negated scanset Any number of characters none of them specified as characters between the brackets.
n Count No input is consumed.
The number of characters read so far from stdin is stored in the pointed location.
% % A % followed by another % matches a single %.

http://www.cplusplus.com/reference/cstdio/scanf/

printf——格式化输出说明

printf格式说明符遵循以下原则:

%[flags][width][.precision][length]specifier

%[标志][宽度][.精度][长度]说明符

specifier Output Example
d or i 带符号的十进制整数 392
u 无符号的十进制整数 7235
o 无符号八进制数字 610
x 无符号十六进制整数 7fa
X 无符号十六进制整型(大写) 7FA
f 十进制浮点数,小写 392.65
F 十进制浮点数,大写 392.65
e Scientific notation (mantissa/exponent), lowercase 3.9265e+2
E Scientific notation (mantissa/exponent), uppercase 3.9265E+2
g Use the shortest representation: %e or %f 392.65
G Use the shortest representation: %E or %F 392.65
a Hexadecimal floating point, lowercase -0xc.90fep-2
A Hexadecimal floating point, uppercase -0XC.90FEP-2
c 字符 a
s 字符串 sample
p 指针地址 b8000000
n Nothing printed.
The corresponding argument must be a pointer to a signed int.
The number of characters written so far is stored in the pointed location.
% A % followed by another % character will write a single % to the stream. %

http://www.cplusplus.com/reference/cstdio/printf/

二进制与十六进制相关问题

bit(二进制位)是计算机中的最小单位。

1字节=8bit

8个二进制数占1字节。

2个十六进制数占1字节。

int声明的变量占4个字节。

 

二进制最高位表示正负。其中,0表示正、1表示负。

十六进制数最高位大于等于8时,必然是负数。

使用unsigned int声明变量时,不考虑符号,只取0和正数。

 

argc:arg为参数,c为计数器。

argv:arg为参数,v为值(value)

char:字符。占用1字节,8个位。

 

#include <stdio.h>

int main(int argc, char *argv[])
{
	int a = 0xFFFFFFFF;
	int b = 0x0FFFFFFF;

	printf("%d\t%u\n", a,a);
	printf("%d", b);

	return 0;
}