Getting to know the C language for the first time -- the first chapter of a quick introduction to the C language

Hello everyone, here is the programmer learning diary

insert image description here


foreword

Hello everyone, welcome to the first part of "Elementary C Language", in this article we will give you a brief introductionWhat is C language, the first C language program, data types, constant variables, strings, escape characters, comments, let everyone understand the C language from scratch

1. What is C language

C language is a general-purpose computer programming language, widely used in low-level development. The design goal of the C language is to provide an easy way to compile, handle low-level memory, generate a small amount of machine code, andDoes not require any operating environment support to runprogramming language.
Although C language provides many low-level processing functions, it still maintains good cross-platform characteristics, written in a standard specificationC programs can be compiled on many computer platforms, and even include some embedded processors (microcontrollers or MCUs) and operating platforms such as supercomputers.
In the 1980s, in order to avoid differences in the C language syntax used by various developers, theThe American National Bureau of Standards has developed a complete set of American National Standard grammars for the C language, called ANSI C, as the original standard for the C language.
C language is a process-oriented computer programming language, which is different from object-oriented programming languages ​​such as C++, Java, etc. Its compilers mainly include Clang, GCC, WIN-TC, SUBLIME, MSVC, Turbo C, etc.
simply put:C language is the language that we humans communicate with computers. It can convert the code we write into binary instructions that the computer can recognize., the operating system uses these binary instructions to let the computer do all kinds of things for us humans.

2. The first C language program

#include<stdio.h>
int main()
{
    
    
	printf("hello world\n");
	return 0;
}

Analysis:
main: main function, the entry of the program, a program has and can only have one main function.
printf: library function, used to print data output to the screen.
include<stdio.h>: include means include, stdio.h is the header file of the printf library function, and the corresponding header file must be included at the beginning of the program when using printf.
return: The return value of the main function, where 1 is returned if the program ends normally.
insert image description here

3. Data type

char //Character data type
short //Short integer
int //
Integer long //Long integer
long long //Longer integer
float //Single-precision floating-point number
double //Double-precision floating-point number
Note: There is no such thing in C language String type

Size of each data type:

#include <stdio.h>
int main()
{
    
    
	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
	printf("%d\n", sizeof(long double)); //8
	return 0;
}
}

insert image description here
Why there are so many data types: In order to express the various values ​​in life more abundantly.
Use of data types:

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

Basic students can read my other article, which has a comprehensive introduction to data types: C language keyword detailed explanation (1) auto, register keywords

4. Constants and Variables

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

constant

Constants in C language are divided into the following categories:

  • literal constant
  • const-modified constant variable
  • Identifier constants defined by #define
  • enum constant
#include <stdio.h>
//举例
enum Sex
{
    
    
 MALE,
 FEMALE,
 SECRET
};
//括号中的MALE,FEMALE,SECRET是枚举常量
int main()
{
    
    
    //字面常量演示
    3.14;//字面常量
    1000;//字面常量
    
    //const 修饰的常变量
    const float pai = 3.14f;   //这里的pai是const修饰的常变量
    pai = 5.14;//是不能直接修改的!
    
    //#define的标识符常量 演示
 #define MAX 100
    printf("max = %d\n", MAX);
    
    //枚举常量演示
    printf("%d\n", MALE);
    printf("%d\n", FEMALE);
    printf("%d\n", SECRET);
    return 0;
 }

Notice:

  • By default, enumeration constants start at 0 and increment by 1.
  • The pai in the above example is called a const-modified constant variable. The const-modified constant variable 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.

For the detailed usage of const, students with basic knowledge can read my other article: Detailed Explanation of C Language Keywords (4) Take you to fully understand the const keyword

variable

How to define variables

int age = 150;
float weight = 45.5f;
char ch = 'w';

Classification of variables

Variables are divided into global variables and local variables

#include<stdio.h>
int g_val = 10;   //全局变量
int main()
{
    
    
	int a = 20;   //局部变量
	printf("%d\n", g_val);
	printf("%d\n", a);
	return 0;
}

Note: When two local variables have the same variable names, the compiler will report an error; when global variables and local variables have the same variable names, the local variable takes precedence.
insert image description here

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;
}

Analysis:
scanf: library function, formatted input, used to accept data entered from the keyboard. .
nums1, nums2, sum: local variables used to store data.

Scope and lifetime of variables

scope
Scope is a programming concept, in general, names used in a piece of program code are not always valid/available
. The scope of code that qualifies the availability of the name is the scope of the name.

  • The scope of a local variable is the local scope in which the variable resides.
  • The scope of global variables is the entire project.

life cycle
The life cycle of a variable refers to the period of time between the creation of the variable and the destruction of the variable.

  • The life cycle of a local variable is: the life cycle of entering the scope begins, and the life cycle out of the scope ends.
  • The life cycle of global variables is: the life cycle of the entire program.

Students with basic knowledge can read my other article, which has a comprehensive introduction to the scope and life cycle of local variables, global variables, and variables: Detailed explanation of C language keywords (1) auto, register keywords

5. String + escape character + comment

string

"hello world"

This string of characters enclosed in double quotes is called a String Literal, or simply a
string.

Points to note with strings:

  • The end of the string is marked by a \0 escape character.
  • \0 is the end marker when printing the string and calculating the length of the string.
  • \0 does not count as string content.
  • The compiler will automatically add \0 at the end of the string.
  • Correctly understand that \0 is the sign of the end of the string:

How to understand that \0 is the end of the string:
Question 1: What is the print result of the following code?

#include <stdio.h>
int main()
{
    
    
    char arr1[] = "hello";
    char arr2[] = {
    
     'h','e','l','l','o' };
    char arr3[] = {
    
     'h','e','l','l','o', '\0' };
    printf("%s\n", arr1);
    printf("%s\n", arr2);
    printf("%s\n", arr3);
    return 0;
}

insert image description here

Analysis:
arr1 is a string, which is stored in memory: 'h' 'e' 'l' 'l' '0' '\0' six characters, the same as arr3, and '\0' is a character end of string, so when printing arr1 and arr3 with %s, it is hello, and there is no '\0' at the end of arr2, so when it prints arr1 and arr3When printing with %s, the compiler will not stop at the position of the character 'o', but will continue to print backward until it encounters '\0', but we don't know what data is stored in the memory space behind arr2, so we can't determine the number of characters printed by arr2 and what kind of characters it is.

Question 2: What is the print result of the following code?

#include <stdio.h>
#include<string.h>
//strlen:求字符串长度的库函数,返回'\0'以前的字符的个数,其头文件是 string.h
int main()
{
    
    
	char arr1[] = "hello";
	char arr2[] = {
    
     'h','e','l','l','o' };
	char arr3[] = {
    
     'h','e','l','l','o', '\0' };
	printf("%d\n", strlen(arr1));
	printf("%d\n", strlen(arr2));
	printf("%d\n", strlen(arr3));
	return 0;
}

insert image description here

Analysis:
Supplement: strlen: a library function that finds the length of a string, returns the number of characters before '\0', its header file is string.h
Here is the same as above, strlen finds the length of a string, and encounters '\0' stop, so arr1 and arr3 are both 5, and there is no '\o' at the end of arr2, and we don't know what the data after arr2 is, so arr2 prints a random value.

escape character

C language escape character table:

insert image description here

Use of common escape characters

#include<stdio.h>
int main()
{
    
    
	printf("hello\n");  // \n:换行,将光标移动到下一行

	printf("hello\t");  // \t:水平制表符,一次跳过四个或者八个字符
	printf("\n");

	printf("\"");       // \:将 " 的意思改变,让我们单独可以打印出 "
	printf("\n");

	printf("\'");       // \:同上,将 ’ 的意思改变,让我们单独可以打印出 ‘
	printf("\n");

	printf("%c\n", '\130');   // \:将八进制的130转化为十进制的88,再打印88对应的ASCII表上的字符 'X'

	printf("%c\n", '\x61');   // \:将十六进制的60转化为十进制的97,再打印96对应的ASCII表上的字符 'a'

	return 0;
}

insert image description here

ASCII encoding table:

insert image description here

Understanding of strings and escape characters:
Written test question: What is the output of the following code?

#include <stdio.h>
int main()
{
    
    
    printf("%d\n", strlen("abcdef"));
    printf("%d\n", strlen("c:\test\628\test.c"));
    return 0;
}

insert image description here

Analysis:
Let's not talk about the first one, just look at the second one: c:\test\628\test.c, where \t \62 \t is escaped by \ and is regarded as a character, so there are 14 characters in total , there may be many students who will regard \628 as a character to get the answer 13, but please note that,The maximum number of octal numbers is 7, and 8 does not belong to the category of octal, so \62 is one character and 8 is another character.

Notes

There are two styles
of comments: C-style comments: / xxxxxx / , which is convenient for comments, but cannot be nested.
C++ style comments: //xxxxxxxx , you can comment one line or multiple lines, you can also use ctrl+k+c and ctrl+k+u to quickly comment and uncomment, it is more popular at present.

The role of comments: You can explain the written program to enhance readability. You can also comment directly on the code you want to delete or useless or wrong.

#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;
}

Guess you like

Origin blog.csdn.net/m0_62391199/article/details/123936982