C语言入门

1、基本环境

2、学习流程图

 3、HelloWord程序

 4、更多基础示例

例1:输入a,b,将a,b进行相加得到c,最后输出c

#include <stdio.h>
int main() { int a,b,c; printf("Please input a=?"); scanf("%d",&a); printf("Please input b=?"); scanf("%d",&b); c=a+b; printf("a+b=%d",c); return 0; }

例2:数据类型的定义与输出

#include <stdio.h>

int main() {
    int myint1=1;                      //定义一个整数类型
    double mydouble1=1.34;            //定义一个含小数的数据类型
    char mychar1='a';                //定义一个字符类型
    char* myString1="Hello,World!"; //定义一个字符串数据类型
    char myString1array[20]="Hello,World!array";  //自定义一个字符数组,即可以表示字符串类型,20是字符串的长度
    const int myconstInt1=1.11;   //定义一个int常量,所以下面注释的这行不能再对它进行赋值
    //myconstInt1=2.22;

    /*请记住下面的输出类型对应的表示符号,比如整数是%d*/
    printf("%d\n",myint1);    
    printf("%f\n",mydouble1);
    printf("%d %c\n",mychar1,mychar1);
    printf("%s\n",myString1);
    printf("%s\n",myString1array);
    printf("%d\n",myconstInt1);
    
    return 0;
}

例3:类型转换

#include <stdio.h>
#include <stdlib.h>
int main() {
    /*转为int*/
    char* myString1="123";
    int toint = atoi(myString1); //1、string转int
    printf("%d\n",toint);

    /*转为String*/
    int a=123;
    char s[20];
    itoa(a,s,10);   //2、int转为 Strigng  其中int是10进制
    printf("%s\n",s);

    /**/
    int b=123;
    double bb=b; //3、int 转为 double
    printf("%f\n",bb);
    b=(int)bb;  //4、double 需强转为double
    printf("%d\n",b);
    return 0;
}

 例4:变量运算

#include <stdio.h>
#include <string.h>
#include <math.h>
int main() {
    /*数值运算及基本逻辑运算*/
    int a=0,b=1;
    a=a+b+1;  //0+1+1=2
    a+=1;    //2+1=3
    a*=2;    //2*3=6
    printf("%d",a);
    a==6? printf("算对了\n"): printf("算错了~\n");
    a=pow(a,2); //6^2
    printf("平方后:%d\n",a);

    /*字符运算*/
    char* str="Hello";
    strcat(str,",World!");
    printf("%s",str);
    
    return 0;
}

例5:逻辑运算

#include <stdio.h>
int main() {
    /*逻辑运算, 0为假,1为值*/
    int a = 1;
    int b = 0;
    int c = a && b; //与运算 (找假)
    int d = a || b; //或运算 (找真)
    int e = !a;     //非运算(取反)
    printf("请调试~\n");
    printf("%d-%d-%d", c,d,e);
    return 0;
}

例6:数学运算

#include <stdio.h>
#include <math.h>
#include <stdlib.h>

int main() {
    /*数学运算*/
    int myDouble;
    const double PI= acos(-1.0);
    myDouble=PI;
    myDouble= exp(1);
    myDouble= sqrt(64);        //开方
    myDouble= pow(4,2);       //4^2
    myDouble= ceil(18.54);    //上取敕
    myDouble= floor(18.54);  //下取整
    myDouble= sin(30*PI/180);
    myDouble= fabs(-101);     //取绝对值
    myDouble= fmod(19,2);  //取余
    myDouble= rand()%100;     //生成0~99的随机数

    printf("请自行调试~");


    return 0;
}

例7:if 语句

#include <stdio.h>
int main() {
    /*数学运算*/
    int age = 18;
    if (age == 18) {
        printf("刚刚成年~");
    } else if(age > 18){
        printf("你已经成年了~");
    }else {
        printf("年经真好~");
    }
    return 0;
}

例8:switch 语句(与if相近)

#include <stdio.h>
int main() {
    /*数学运算*/
    int n = 3;
    switch (n) {
        case 1:
            printf("一级");
            break;
        case 2:
            printf("二级");
            break;
        default:
            printf("其它级别");
    }
    return 0;
}

例9:循环语句(当知道循环的次数时使用)

#include <stdio.h>
int main() {
    /*for循环*/
    for (int i=0; i < 100; i++) {  // ..(循环变量初始化; 循环条件; 循环变量更新规则)..
        printf("循环次数:%d\n",i+1);
    }
    return 0;
}

例10:while与do while  循环结构

#include <stdio.h>
int main() {
    /* while循环  经常使用*/
    int i = 0;
    while (i < 100) {
        printf("循环次数: %d\n",i+1);
        i++;
    }

    /* do-while  非常少用*/
    printf("===do-while==-\n");
    do{
        printf("至少执行一次:%d",i);
    } while (i<100);
    return 0;
}

 例11:函数

#include <stdio.h>
/*定义一个函数*/
int _max(int a,int b) {
    if (a>b) {
        return a;
    }else {
        return b;
    }

}
/*入口函数调用*/
int main() {
    /* while循环  经常使用*/
    int a=22,b=11,c;
    c=_max(a,b);
    printf("%d\n",c);
    return 0;
}

例12:一维数组的定义

#include <stdio.h>

int main() {
    /*定义一个char数组,第一种方式*/
    char arr1[3]={'a','b','c'};
    /*定义一个char数组,第二种方式*/
    char arr2[3];
    arr2[0]='a';
    arr2[1]='b';
    arr2[2]='c';

    /*定义一个字符串数组*/
    char *sarr[3]={"小庄","大聪明","小聪明"};
    printf("请调试~");
    
    return 0;
}

例13:二维数组的定义

#include <stdio.h>

int main() {
    //定义一个二行二列的二维数组,即[行数][列数]
    char arr2[2][2]={{'a','b'},{'c','d'}};
    for (int i = 0; i < 2 ; i++) {
        for (int j = 0; j < 2; j++) {
            printf("%d-%d:%c\n",i,j,arr2[i][j]);
        }
    }
    printf("--------分割线-----\n");
    //定义一个二行二列二维字符串数组
    char *srr2[2][2]={{"aa","bb"},{"cc","dd"}};
    for (int i = 0; i < 2 ; i++) {
        for (int j = 0; j < 2; j++) {
            printf("%d-%d:%s\n",i,j,srr2[i][j]);
        }
    }


    return 0;
}

例14:数据结构体

#include <stdio.h>
#include <string.h>
int main() {
    /*声明数据结构体*/
    struct Student{
        int id;
        char name[20];
    } ;
    /*创建结构体的模版补例*/
    struct Student student1;
    /*向实例中存储数据*/
    student1.id=12901;
    strcpy(student1.name,"大聪明");

    /*输出数据*/
    printf("%d-%s",student1.id,student1.name);

    return 0;
}

 例15:指针(重要)

#include <stdio.h>
#include <string.h>
int main() {
    /*指针基本示例内容*/
    int a=1;
    int *p;  //声明时*p存的是变量地址
    p=&a;   //此时的p代表的是地址,取a变量的地址赋值给p , 而*p就是空间即1,这是最基本也是最常用的指针知识点
    printf("%d,%p,%d\n",a,p,*p);

    /*特别地,对数组中的元素进行“前后”运算时*/
    int array[3]={1,2,3};
    int *p2;
    p2=&array[0];
    printf("%d,%p,%d,%d\n",array[0],p2,*p2,*(p2+1));

    return 0;
}

例17:文件读写

#include <stdio.h>
int main() {
    FILE *fp;
    /*文件写*/
    char *myfile="d:\\cRead.txt";
    char *new_content="写入的内容~";
    fp= fopen(myfile,"w");
    fputs(new_content,fp);
    /*文件读*/
    fclose(fp);
    fp= fopen(myfile,"r");
    char ts[200];
    fgets(ts,50,fp);
    printf("%s",ts);
    fclose(fp);
    return 0;
}

知识1: 函数指针 

//函数接收函数指针作为参数
void pi(int (*my_max)(int , int), int num1,int num2) {
    printf("利用传入的函数与数据,得到的max:%d\n",my_max(num1, num2));
}
int main() {
    //声明一个函数指针
    int (* p)(int, int) = &max;
    //传入一个函数指针到其它函数中调用
    pi(p,123,22);
    return 0;
}

知识2: 直接创建数据存储结构 & 创建数据类型

//直接创建存储结构  不用 typedef 

#include <stdio.h>
#include <stdlib.h>
#include "_4_Tree/tree.h"
struct {
    char *name;
    char *author;
}book;
int main() {

    book.author = "小产";
    book.name = "仙人志";
    printf("%s的新书叫《%s》\n",book.author,book.name);
    return 0;
}

//创建数据类型,应用了 typedef
//============================

typedef struct {
    char *name;
    char *author;
}book;
int main() {

    book *bk = (book *)malloc(sizeof(book));
    bk->author = "小产";
    bk->name = "仙人志";
    printf("%s的新书叫《%s》\n",bk->author,bk->name);
    return 0;
}

 知识2: 宏定义

#define  标识符  常量   //注意, 最后没有分号

#C语言中,可以用 #define 定义一个标识符来表示一个常量。其特点是:定义的标识符不占内存,只是一个临时的符号,预编译后这个符号就不存在了。
#一经定义,程序中就可以直接用标识符来表示这个常量。是不是与定义变量类似?但是要区分开!变量名表示的是一个变量,但宏名表示的是一个常量。可以给变量赋值,但绝不能给常量赋值。
#宏所表示的常量可以是数字、字符、字符串、表达式。其中最常用的是数字。
原文地址:https://www.cnblogs.com/zjazn/p/15666539.html