注意事项

注意事项

learn/c

printf

当类型向上转型时,如果不强制转型,会出现内存溢出,他输出时默认输出 0 .

C
1
2
3
4
5
6
7
8
#include <stdio.h>

int main() {
    short s = 32767;
    int i = s; // 向上转型
    printf("%d\n", i); 
    return 0;
}

putchar

输出字符类型 putchar(字符变量); putchar(‘A’);//A

C
1
2
3
4
5
6
7
8
9
#include <stdio.h>

int main() {
    char c = 'A';
    putchar(c); // 输出字符 A
    putchar('\n'); 
    putchar('B'); // 直接输出字符 B
    return 0;
}

getchar

输入一个字符 char c ; //接收键盘输入的字符 c = getchar(); //输出字符 putchar(c);

C
1
2
3
4
5
6
7
8
9
10
#include <stdio.h>

int main() {
    char c;
    // 接收键盘输入的字符
    c = getchar();
    // 输出字符
    putchar(c);
    return 0;
}

int x 定义多个值时

当 int j ; int y ; int k ; //实际取值为最后一项,就是 int x = 32 int x = (int j = 4 , int y = 16 , int k = 32 );

C
1
2
3
4
5
6
7
8
#include <stdio.h>

int main() {
    int j = 4, y = 16, k = 32;
    int x = (j, y, k); // 实际取值为 k 的值
    printf("%d\n", x); 
    return 0;
}

用户标识符

标识符由数字、字母和下划线组成,但标识符的开头必须是字母或者下划线。例如,_var、abc123 是合法的标识符,而 123abc 是不合法的。

定义字符串

1.第一种:

C
1
2
char str[] = "hello";
char str[] = {'h','e','l','l','o','\0'};

特别注意用这种方式定义字符串时必须加上\0来结束否则会有好事发生。

2.第二种:

C
1
2
3
4
5
6
7
char*str = "hello";
**注意**
两种方式的区别:第一种是将字符串定义为字符串变量(可读可写
)而第二种将字符串定义为字符串常量(只读)
char*str  [] = "hello";
    printf("str = %p\n" , str);//打印str字符串
    printf("str = %p\n" , sizeof(str));//打印str所占的字节长度

其他

要求参加算数运算符的数必须是整数的运算符是 % 符号位 ——0 正 1 负

continue 语句

contune :用于循环中的,用来跳过某一次循环

C
1
2
3
4
5
6
7
8
9
10
11
#include <stdio.h>

int main() {
   for (int i = 0; i < 5; i++) {
       if (i == 2) {
           continue; 
       }
       printf("%d ", i); 
   }
   return 0;
}

define 预处理指令

define是预处理指令,不是关键词

C
1
2
3
4
5
6
7
8
9
10
11
#include <stdio.h>

#define NUM 100
#define ADD(X, Y) ((X) + (Y))

int main() {
    int a = 10, b = 20;
    int c = ADD(a, b); 
    printf("%d\n", c); 
    return 0;
}