• hello world
    编译gcc -o hello main.c
    #include <stdio.h>
    int main() {
    printf("Hello World!");
    return 0;
    }
  • 基础知识
    c语言句末必须有分号
    区分大小写,变量申请可以不使用
    // 这是一行注释
    /*
    这是多行注释
    */
    打印用printf("")
    printf("%5d\n", 123); // 输出为 "  123" d是十进制
    printf("%+d\n", 123); 输出+123
    %f小数  %s字符串 %o八进制 %x十六进制 %p指针 %%显示%
    printf("%6.2f\n", 0.5); 输出0.50
    printf("%.5s", "hello world"); 输出hello
  • 变量,常量,数据类型
int height, width;
height=12;
int a=1;
main函数之外变量当前文件有效
const float PI = 3.14; 不可修改
short a = 0b1010110;  // 2 进制数字 0b
int b = 02713;        // 8 进制数字0开头
long c = 0X1DAB83;    // 16 进制数字0x或0X
字符串,必须双引号
char s1[]="我是中国的";不可修改
char *s2="我是太空人";可修改
字符类型,必须单引号
char c = 'B';char c = 66;两个等价
unsigned int a;unsigned; 无符号类型>=0
unsigned char c;表示 0-255
signed char c; -128-127
short int;long int;long long int;可以省略int
long int      x = 1234L;等价
long long int x = 1234LL;
unsigned int           x = 1234U;
unsigned long int      x = 1234UL;
unsigned long long int x = 1234ULL;
float x       = 3.14f;
double x      = 3.14;
long double x = 3.14L;
float c = 10.5;
double x = 123.456e+3;浮点数
_Bool b=0; 10伪
类型显示转换
int b=(int)3.14;
  • 流程控制
    ### if 
    多条语句用{}
    int x = 10;
    if (x == 10) printf("x is 10");
    if (x >1){
          printf("x >1\n");
      }else if(x<1){
          printf("x <1\n");
      }else{
          printf("x =1\n");
      }
    ### 三元运算
      int x = -10;
      char *y=(x>1)?"大于1":"小于1";
      printf("%s",y);
    ### switch
      int day = 4;
    switch (day) {
    case 3: printf("周三"); break;
    case 4: printf("周四"); break;
    default:
      printf("期待周末");
    }
    ### while
      int i = 0;
    while (i < 5) {
    printf("%d\n", i);
    i++;
    }
    ### for
    int i;
    for (i = 0; i < 5; i++) {
    printf("%d\n", i);
    }
    ### break,continue
    跳出循环用在switch,for
    终止本轮循环进入下一轮continue
  • 数组
    int arr[]={1,2,3,4,5}; int arr1[2]={1,-1};
    int a[2];a[0]=1;a[1]=0;
    int a[2][5] = {
    {0, 1, 2, 3, 4},
    {5, 6, 7, 8, 9}
    }; 多维数组
    printf("%zd\n",sizeof(arr)); 长度 %ud 是size_t数据类型
    char* weekdays[] = {
    "Monday",
    "Tuesday",
    "Wednesday",
    "Thursday",
    "Friday",
    "Saturday",
    "Sunday"
    };
  • 指针
    int arr[2]={1,2};
     int * a=&arr[0];
     int* b=&arr[1];
     int c=*a+*b;
     printf("%d\n",c); //3
     printf("%d\n",*a+1); //2
     printf("%p\n",&c); //000000e3b65ffb64
     printf("%d\n",*a+*b);//3
     printf("%d",*a-*b);//-1
     int a=2;
      printf("%d\n",*(&a));//2
  • 函数
    int inx(int a){ //定义在main之前或者申明在前 int inx(int a);
      a++;
      return a;
    }
    int (*p)(int) = &inx;//函数指针调用等价于inx(1)
    printf("%d\n",(*p)(1));
    void swap(int* x, int* y) {//交换xy值,函数只能返回一个值,多个使用指针和地址传递
    int temp;
    temp = *x;
    *x = *y;
    *y = temp;
    }
    int a=1,b=2;
      swap(&a,&b);
      printf("%d %d\n",a,b);//2,1
    exit(0); 运行成功退出
    exit(1); 异常终止
    static int count = 1; 只初始化一次,赋值只能是常量
    static int fn(int num);只能当前文件使用的函数
    double average(int i, ...) 可变参数
  • 结构体
    typedef struct car {  //设置别名
    char* name;
    float price;
    int speed;
    }st;
    st saturn ={"测试", 16000.99, 175};
    printf("车名:%s,价格:%f,速度:%d\n", saturn.name, saturn.price, saturn.speed);
    typedef unsigned char BYTE;//类型别名
    BYTE c = 'z';
    union foo {
    int a;
    float b;
    } x;
    x.a = 12;
    enum { ONE = 1, TWO = 2 }; 枚举
  • 预处理器
    #define MAX 100
    #define SQUARE(X) X*X
    printf("%d\n", SQUARE(3 + 4));//19
  • 输入
    int i;
    scanf("%d", &i); 键盘输入
    printf("%d\n", i);
    处理文件
    FILE* fp;char c;
    fp = fopen("hello.txt", "r");
    if (fp == NULL) {
      return -1;
    }
    c = fgetc(fp);
    printf("%c\n", c);
    fclose(fp);
作者:Yoby  创建时间:2025-06-08 17:08
最后编辑:Yoby  更新时间:2025-06-13 12:46
上一篇:
下一篇: