Playing with C language with zero foundation - getting to know C language for the first time (Part 1)

Hello everyone, I am deep fish~

Table of contents

Foreword:

1. How to learn C language well

1.1 Encourage you and applaud you.

1.2 Squeeze time to study

1.3 Learning programming well is not just learning C language well

Second, the initial C language (on)

focus:

1. What is C language

2. The first C language program

3. Data type (used to create variables)

4. Variables, constants

5. String + escape character + comment

6. Selection statement (here only briefly introduces the if else statement)

7. Loop language

8. Function

Three, error-prone exercises

Four. Conclusion


Foreword:

This chapter is just a basic understanding of the basic knowledge of the C language. I have a general understanding of the C language and can basically understand the code. It is suitable for students who have zero basic learning of the C language. Each knowledge point is a simple understanding without detailed explanation. Detailed knowledge will also be explained.

1. How to learn C language well

1.1 Encourage you and applaud you.

C is the foundation of
programming in all things C language is the mother language of choice
for long-term IT career development , and it is a bridge for human-computer interaction to approach the bottom . A revolution In the past 50 years, C/C++ has occupied the top three positions in the IIOBE rankings for a long time, without any shake. It can be said that the classics will never go out of date!





1.2 Squeeze time to study

·Wearing a crown, must bear its weight

·If you always go the same way as others, how can you guarantee to surpass others, then you have to make different efforts

· To truly become an expert in a field requires at least 10,000 hours of training

1.3 Learning programming well is not just learning C language well

Must learn:

Computer language (C/C++, Java, etc.), algorithm and data structure, operating system, computer network, project practice, database

Second, the initial C language (on)

focus:

1. What is C language

2. The first C language program

3. Data type

4. Variables, constants

5. String + escape character + comment

6. Select statement

7. Loop language

8. Function

1. What is C language

Natural language: Chinese, Japanese, English...etc, these are the languages ​​of communication between people

Computer language: the language for communication between people and computers, eg: C/C++/Java/python/go/php, etc. There are thousands of computer languages, and C language is a computer language that is widely used in underlying development

C language is a process-oriented programming language, while C++/Java, etc. are object-oriented programming languages

The United States National Bureau of Standards has customized a complete set of standard syntax for the C language, called ANSI C , and the computer processes binary information (100010101), but this is too troublesome, and assembly language (such as 00010000 means Add, this kind called mnemonics), and then produced the B language, followed by the C language (high-level language), but due to inconsistency, a set of standards was produced

Its compilers mainly include: Clang, GCC, MSVC (compiler for VS integrated development environment)

Integrated development environment : It is the application software developed by programmers, such as VS2019, VS2022, Devc++, CodeBlocks, which integrates many sub-functions: editing, compiling, linking, running, debugging

 

2. The first C language program

Print on screen: Hello World

#include<stdio.h>
int main()
{
	printf("Hello World\n");  // \n是换行符(类似于Enter回车键)
	return 0;
}

explain:

#include<stdio.h> is the inclusion of the header file, std in stdio is the standard, i is the input, o is the output

The main function is the entry point of the program, and there is only one main function in a program

printf is a library function, which is used to print output. The use of library functions must include header files, and its header file is stdio.h

return 0 is the return value at the end of the main function is 0

Except for the fourth line, the other parts are fixed parts of the C language program, which is an indispensable part of typing code

Of course, good tools are definitely needed to learn C language. Vs2019 and vs2022 are both available. You can watch this for the specific installation tutorial. I strongly recommend Brother Peng’s C language video. The explanation is very detailed. I personally think it is the best C language learning course.

https://www.bilibili.com/video/BV11R4y1s7jz/?spm_id_from=333.999.0.0&vd_source=ca7656038bc9f10870c8b9fb0a82e2cf

3. Data type (used to create variables)

char //string data type

short //short integer

int //integer

long // long integer

long long //longer integer

float //single-precision floating-point type (floating-point numbers are decimals)

double // double precision floating point type

Question 1: Does the C language have a string type?

Answer: No, strings are usually placed in character arrays

Question 2: Why are there so many types?

Answer: In fact, it is to express and describe various values ​​in life in a richer way

Question 3: What is the size of each type?

Answer: Each type is followed by its size (%d here is to print an integer in decimal form)

#include<stdio.h>
int main()
{
	//sizeof是用来计算操作数的类型长度(以字节为单位)

	printf("%d\n", sizeof(char));      //1字节
	printf("%d\n", sizeof(short));     //2
	printf("%d\n", sizeof(int));       //4
	printf("%d\n", sizeof(long));      //4
	printf("%d\n", sizeof(long long)); //8
	printf("%d\n", sizeof(float));     //4
	printf("%d\n", sizeof(double));    //8
	return 0;
}

C language standard: sizeof (long long) > sizeof (long) >=sizeof (int) >sizeof (short) >sizeof (char)

Supplement: Common units and conversions in computers:

bit - bits

byte - byte

KB                          1byte=8bit

MB                         1KB=1024byte

GB                          1MB=1024KB

TB                          1GB=1024MB

PB                          1TB=1024GB

4. Variables, constants

4.1 Method of defining variables

Variable data type + variable name (type+name)

char ch='w';
int weight=120;
int salary=20000;

 4.2 Classification of variables

Local variables: defined variables defined inside { }

Global variables: variables defined outside { }

#include<stdio.h>

int a=100; //全局变量
int main()
{
    int a=1000;   //局部变量
    printf("a=%d",a);
    return 0;
}

Note: When the local variable and the global variable have the same name, the local variable takes precedence , so the result of the above code is 1000

4.3 Use of variables

#include<stdio.h>
int main()
{

  int a=0;
  int b=0;
  int sum=0;
  scanf("%d %d",&a,&b);
  sum=a+b;
  printf("sum=%d\n",sum);
  return 0;
}

The input function scanf appears here. Note that \n is not added after the scanf format . Similarly, %d here is input in decimal form, & is an address character, and scanf ("%d", &a) means to input a value and Put in the address of a, let a be equal to the input value

4.4 Naming of variables

· Can only be composed of letters (including uppercase and lowercase), numbers and underscores (_)

· Cannot begin with a number

· The length cannot exceed 63 characters

The case of the variable name is different , count as two different variables

Variable names cannot use keywords (keywords will be discussed in the next section)

· Variable names should be as meaningful as possible

4.5 Scope and Lifecycle of Variables

Scope: the scope of the variable

              The scope of a local variable is the local scope of the variable

             The scope of global variables is the entire project

Life cycle: the period of time between a variable's creation and its destruction

                  The life cycle of local variables: the life cycle of entering the scope begins, and the life cycle of the out of scope ends

                  The life cycle of global variables: the life cycle of the entire program

4.6 Constants

The constants in C language are divided into the following categories:

·  Literal constants

Constant variables modified by const (variables are still variables after modification  , but have constant attributes)

·  Identifier constants defined by #define

·  Enumerated constants

#include<stdio.h>

//枚举常量(枚举:就是一一列举)
//三原色:红,绿,蓝
//性别:男,女,未知
//星期:1,2,3,4,5,6,天

//自定义类型
enum Color   //enum是枚举关键字
{
    //枚举常量
  RED,  //这里用,而不是;
  GREEN,  //默认这几个常量为0,1,2;如果自己定义为7,那这三个就是7,8,9
  BLUE,
};   //注意这里有逗号

int main()
{
   enum Color c=RED;
   printf("%d\n",RED); //打印的结果为0
   return 0;
}


int main()//这里只是做个示范,记住一个程序只能有一个main函数
{
//字面常量演示
    3.14; //字面常量
    1000; //字面常量

//const(中文意思:常属性的),用来修饰变量
   const int a=10;  //a就是const修饰的常变量,但是本质上a还是变量
   
   printf("a=%d\n",a);  //a=10
   a=100;               
   printf("a=%d\n",a);  //a=10;因为const修饰的变量具有常属性,值不会改变
   
   int arr[10]={0};
   const int n=10;
   int arr2[n]={0};  //这是错误的,const并没有使变量变成常量(数字的个数[ ]里不能用变量表示,只能用常量)

//#define定义的标识符常量
   #define M 100  //(没有逗号,而且这条语句要写在主函数之前)
   int arr[M]={0}; //这里的M是常量而不是变量
   int a=M;
   printf("%d\n",a);
   
   return 0;
}

5. String + escape character + comment

5.1 Strings

"hello world.\n"

This string of characters enclosed by double quotes is a string

Note: The end mark of the string is \0 (although it cannot be seen, but it always exists), but when calculating the length of the string, \0 is not counted as the length of the string

#include<stdio.h>

int main()
{

   char arr1[]="bit";   //这个字符串的结尾自动带有'\0'作为字符串的结束标志
   char arr2[]={'b','i','t'}; //这个结尾没有\0,打印出来就不知道什么时候结束,就会乱码
   char arr3[]={'b','i','t','\0'}; //这个才是正确的写法,结果和第一个一样
   printf("%s\n",arr1);
   printf("%s\n",arr2);
   printf("%s\n",arr3);

   return 0;
}

5.2 Escape characters

Question: If you want to print a directory on the plane: c:\code\test.c

#include<stdio.h>

int main()
{

   printf("c:\code\test.c\n");
   return 0;
}

The result of running this code is this:

c:code  est.c

 This is because of the escape character. If you want to print, you have to cancel the escape with \\ (backslash) . Let's introduce the escape character:

\t: Horizontal tab character: It is equivalent to the Tab key (every 8 characters can be regarded as a horizontal tab character, if there are less than 8 characters before \t, then \t will fill in spaces until it is full 8)

\ddd: can connect one to three, not necessarily three

\xdd:x is fixed, hex has: 0 1 2 3 4 5 6 7 8 9 ABCDEF

 Question 1: How to print a single quote ' ?

printf("%c\n",'\'');  //%c是用来打印字符的

Question 2: How to print a double quote " ?

printf("%s\n","\"");  //%s是用来打印字符串的

The difference between single quotes and double quotes:

  • 'A' represents a single character capital letter A, occupying 1 byte
  • "A" represents a string, which consists of only one capital letter A, occupying 2 bytes of space, and a null character '\ 0' is automatically added at the end of each string

 Question 3: Calculate the length of the following string

The strlen function is used here. This function is to calculate the number of strings before '\0' . If there is no \0, it will not end. A value is randomly given. Before using this library function, the header file is required.

#include <string.h>
The third one is why \181 is not counted as one, because there is no 8 in octal, so it is not considered an escape character
#include<stdio.h>
int main()
{
	printf("%d\n", strlen("abc"));
	printf("%d\n", strlen("c:\test\111\test.c"));//13  \t \111 各算一个字符
	printf("%d\n", strlen("c:\test\181\test.c"));//15     \1 \t  各算一个字符
	return 0;
}

5.3 Notes

(1) Some unnecessary codes can be deleted directly or commented out

(2) Some codes in the code are difficult to understand, you can add a comment text

Two styles of comments:

【1】/*xxxxxxx*/ Defect: cannot nest comments, only comment from beginning to end

【2】//xxxxxxxxxx recommends this, you can comment multiple lines or one line

6. Selection statement (here only briefly introduces the if else statement)

C language - a structured programming language, including sequence structure, selection structure (there are two types: if and switch statements), and loop structure (there are three types: while, for, and do while statements)

choose:

If you study hard, get a good offer during school recruitment, and reach the pinnacle of life.

If you don't study, graduation is equivalent to unemployment, go home and sell sweet potatoes.

code show as below:

#include<stdio.h>

int main()
{
    int input=0;
    printf("你打算好好学习吗(1\0)\n");
    scanf("%d",input);
    if(input==1)
    {
      printf("好offer\n");
    }
    else
    {
       printf("回家卖红薯\n");
    }
    retrun 0;
}

Note: == in C language is equivalent to the equal sign in mathematics , and = means assignment

7. Loop language

There are three types of loop statements in C language

· while statement

` for statement (later)

· do...while statement (later)

 The code realizes the flow chart of the rookie constantly typing code to become a big cow cycle:

#include<stdio.h>
int main()
{
int line=0;
while(line<30000)
{
  line++;
  printf("我要继续努力敲代码\n");

}
if(line==30000)
   printf("代码敲到30000行,成为大牛拿到好offer,迎娶白富美\n");
return 0;
}

8. Function

Analogy raw material --> factory --> product

Function: input parameters --> function --> return value (functions include these parts)

eg: y=f(x)=2*x+5 Give x a value (input parameter), enter the function, and return a y value (return value)

The following uses a custom function to realize the addition of two numbers:

#include<stdio.h>

//自定义函数也可以写在后面但是要在主函数之前进行函数声明
//int Add(int x,int y);(自定义函数主体在后面这样写)

int Add(int x,int y)
{
   int z=x+y;
   return z;   //自定义函数的返回值
}

int main()
{
  int a=0;
  int b=0;
  int sum=0;
  scanf("%d %d",&a,&b);
  //  求和:sum=a+b
  sum=Add(a,b);
  printf("%d\n",sum);   //自定义函数的调用

   return 0;
}

Three, error-prone exercises

Think about what the result of this code prints?

#include <stdio.h>
#include <string.h>

int main()
{
    char arr[] = {'b', 'i', 't'};
    printf("%d\n", strlen(arr));
	return 0;
}

 answer: The result is a random value, because strlen did not find '\0'

4. Conclusion: There is still glory in the other side, and young people are not afraid of the long years

The initial C language part is temporarily introduced here, and some of the content is being sorted out later...

I feel that the author's writing is not bad, or when I have gained a little, I would like to trouble you to move your little hands and give me a one-click three-link, thank you very much

Guess you like

Origin blog.csdn.net/qq_73017178/article/details/131195030