内存存储:
Char 有符号 范围(-128 --- 127)
Unsigned char 无符号 范围 (0 --- 255)
#include <stdio.h>
#include <stdlib.h>
int main()
{
char a = -1;
signed char b = -1;
unsigned char c = -1;
printf("a=%d,b=%d,c=%d", a, b, c);
system("pause");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
char a = 258;
printf("%d\n", a);
char b = -128;
printf("%u\n", b);
system("pause");
return 0;
}
Sizeof : 计算占内存空间的长度 (编译期间做,不在运行时做 , 不运行sizeof ()括号中的结果 )
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a = 6; // sizeof;
int n = sizeof (a++);
printf("n = %d,a = %d", n, a);
system("pause");
return 0;
}
Strlen : 计算字符串的长度 (\0 不算)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int i;
char str[1000];
for (i = 0; i < 1000; i++)
{
str[i] = -1 - i;
}
printf("%d\n", strlen(str)); //255 '\0' -> 0;
system("pause");
return 0;
}
使用函数,交换两个数的内容,需要先找到存储数的地址,在通过地址对数原本的内容进行交换操作,而不是对拷贝的书进行操作。
#include <stdio.h>
#include <stdlib.h>
void swap1(int a, int b)
{
int t;
t = a;
a = b;
b = t;
}
void swap2(int* x, int* y)
{
int *t = x;
x = y;
y = *t;
}
void swap3(int* x, int* y)
{
int t;
t = *x;
*x = *y;
*y = t;
}
int main()
{
int a = 5,b = 7;
swap1(a, b);
printf("%d %d\n", a, b);
swap2(&a, &b);
printf("%d %d\n", a, b);
swap3(&a, &b);
printf("%d %d\n", a, b);
system("pause");
return 0;
}