Getting to know c language for the first time (on)

1. What is C language

C语言是一门通用计算机编程语言,广泛应用于底层开发。C语言的设计目标是提供一种能以简易的方式编译、处理低级存储器、产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言。

2. The first C language program

#include <stdio.h>//头文件
int main()
{
    
    
    printf("hello bit\n");
    return 0; }
//main函数是程序的入口
//一个程序中main函数有且仅有一个
//printf函数是格式化输出函数,是C语言标准库函数,定义于头文件

3. Data Type

(1) Type

char        //字符数据类型
short       //短整型
int         //整形
long        //长整型
long long   //更长的整形
float       //单精度浮点数
double      //双精度浮点数
.....

(2) Calculate the size of each type: use the sizeof function. sizeof() is a memory capacity measurement function that returns the size (in bytes) of a variable or type

#include <stdio.h>
int main()
{
    
    
    printf("%d\n", sizeof(char));
    printf("%d\n", sizeof(short));
    printf("%d\n", sizeof(int));
    printf("%d\n", sizeof(long));
    printf("%d\n", sizeof(long long));
    printf("%d\n", sizeof(float));
    printf("%d\n", sizeof(double));
    printf("%d\n", sizeof(long double));
    return 0; }

4. Variables, constants

Some values ​​in life are constant (eg: pi, gender, ID number, blood type, etc.) and
some values ​​are variable (eg: age, weight, salary).
Invariant values ​​are represented by the concept of constants in C language; variable values ​​are represented by variables in C language .

(1) How to define variables

int age = 45;
float weight = 50.2f;
char ch = 'x';

(2). Classification of variables

a. local variables b. global variables

#include <stdio.h>
int global = 2019;//全局变量
int main()
{
    
    
    int local = 2020;//局部变量
    int global = 2021;//局部变量
    printf("global = %d\n", global);
    return 0; 
 }//结果为global=2021

 *当局部变量和全局变量同名的时候,局部变量优先。

(3) Use of variables

#include <stdio.h>
int main()
{
    
    
    int num1 = 0;
   int num2 = 0;
    int sum = 0;
    printf("输入两个操作数:");
    scanf("%d%d", &num1, &num2);
    sum = num1 + num2;
    printf("sum = %d\n", sum);
    return 0;
  }
 //num1,num2,sum都是变量
//scanf是输入,printf是输出

(4) Scope and life cycle of variables
① Scope
Generally speaking, the name used in a piece of code is not always valid, and the scope of the code that limits the availability of this name is the scope of the name
a. The scope of local variables It is the local scope where the variable is located
b. The scope of the global variable is the entire project
② Life cycle
The life cycle of a variable refers to a period of time between the creation of the variable and the destruction of the variable
a. The life cycle of a local variable is the life cycle of entering the scope The cycle begins, and the out-of-scope life cycle ends
b. The life cycle of global variables is the life cycle of the entire program
(5). Constant
* literal constant
* const modified constant variable
* # define defined identifier constant
* enum constant

#include <stdio.h>
enum Sex
{
    
    
 MALE,//0
 FEMALE,//1
 SECRET//2
};
//括号中的MALE、FEMALE、 SECRET都是枚举常量
//枚举常量默认值是从0开始,往下依次递增1
//注意,最后一个常量后面没有( ,)号
int main()
{
    
    
    //字面常量演示
    3.1415;//字面常量
    1500;//字面常量
    
    //const 修饰的常变量是不能直接修改的!
    const float pai = 3.14f;   //这里的pai是const修饰的常变量
    pai = 5.14;//是不能直接修改的!依然还是3.14f
    //#define的标识符常量 演示
 #define MAX 100
    printf("max = %d\n", MAX);
    
    //枚举常量演示
    printf("%d\n", MALE);
    printf("%d\n", FEMALE);
    printf("%d\n", SECRET);

The constant variable modified by const in C language only restricts the variable pai from being changed directly at the syntax level, but pai is essentially a variable, so it is called a constant variable.

5. Strings, escape characters, comments

(1). String
A string of characters enclosed by double quotation marks (Double Quote) is called a string literal (String Literal), or a string for short. Its end marker is \0

"hello world.\n"//就是一个字符串

\0 is the end mark when calculating the length of the string (such as the strlen function), not the content of the string.

"hello.\n"//存放内容为{'h','e','l','l','o','\0'}所以长度为5
#include <stdio.h>
//下面代码,打印结果是什么?为什么?(突出'\0'的重要性)
int main()
{
    
    
    char arr1[] = "bit";//完整的字符串系统会在末尾自己加上\0
    char arr2[] = {
    
    'b', 'i', 't'};//没有\0
    char arr3[] = {
    
    'b', 'i', 't''\0'};//手动加上\0
    printf("%s\n", arr1);
    printf("%s\n", arr2);
    printf("%s\n", arr3);
    return 0; }

(2). Escape the string

转义字符 释义
\?       在书写连续多个问号时使用,防止他们被解析成三字母词
\'       用于表示字符常量'
\“       用于表示一个字符串内部的双引号
\\       用于表示一个反斜杠,防止它被解释为一个转义序列符。
\a       警告字符,蜂鸣
\b       退格符
\f       进纸符
\n       换行
\r       回车
\t       水平制表符
\v       垂直制表符
\ddd     ddd表示1~3个八进制的数字。 如: \130 X
\xdd     dd表示2个十六进制数字。如: \x30

E.g

#include <stdio.h>
int main()
{
    
    
    printf("%c\n", '\'');//输出为‘ ’
    printf("%s\n", "\"");//输出为“ ”
    return 0; 
 }

(3) Comments
Unnecessary code in the code can be deleted directly or commented out. Some code is more difficult to understand, you can add comment text

#include <stdio.h>
int Add(int x, int y) 
{
    
    
    return x+y; 
}
/*C语言风格注释,不能嵌套注释
int Sub(int x, int y)
{
    return x-y;
}
*/
int main()
{
    
    
    //C++注释风格,可注释一行或多行
    //int a = 10;
    //调用Add函数,完成加法
    printf("%d\n", Add(1, 2));
    return 0; }

6. Select Statement

if(表达式)    //如果满足表达式,就执行语句1
{
    
    
语句1}
else   //否则执行语句2
{
    
    
语句2
}

7. Loop Statement

(1) while loop

while(表达式)
{
    
    
语句1}
//当表达式成立时,一直执行语句1.

code demo

#include <stdio.h>
int main()
{
    
    
    int i = 0;
    while(i<5)
   {
    
    
        i++;
        printf("hello\n");
   }
    return 0; 
 }
 //运行结果为打印5行 hello 代码

(2) for loop

for(初始化变量;循环条件;变化变量)//中间用分号隔开
{
    
    
中间循环体;
}
//其中表达式皆可省略,但是分号不可以

code demo

#include <stdio.h>
int main()
{
    
    
  int i=0;
  for(i=0;i<5;i++)
  {
    
    
  printf("hello\n");
  }
    return 0;
 }
  //运行结果为打印5行 hello 代码

(3). Do...while loop

do
{
    
    
循环体;
}while(循环条件);
//意思为:做...,直到(条件不满足结束)

code demo

#include <stdio.h>
int main()
{
    
    
  int i=0;
  do
  {
    
    
  printf("hello\n");
  i++;
  }while(i<5);
    return 0;
 }
   //运行结果为打印5行 hello 代码

Guess you like

Origin blog.csdn.net/qq_62316056/article/details/124029537