Initial C language (first class)

Table of contents

1. What is the c language?  

2. The first C language program

 3. Data type

1. The size of the type (example)

2. The role of type

4. Variables and constants

         1. Understanding of variables and constants

         2. Definition of variables

         3. Variable naming rules

         4. Classification of variables

         5. The life cycle and scope of variables

         6. Constants

1. What is the c language?

1. First of all, C language is a language as its name suggests, just like English is also a language.

2. English is the language used to communicate with foreigners (a general term, there is no need to engrave details here). At home, you communicate with your family members in your native dialect, and C language is similar. It is a computer programming language and is used to To communicate with a computer (computer).

3. For example, if you type something (such as code) on the keyboard, it is what you say to the computer, and then what is displayed on the computer screen is the computer answering you.

     First give a general framework: In summary, C language also has grammar, and some regulations are dead and can only be written in this way, so there is no need to be too entangled in future studies.

2. The first C language program

Note: We need to be in a specific place to communicate with the computer. The following content is implemented on Visual Studio 2022 (the specific installation process can be searched for tutorials on the Internet by yourself, it is too cumbersome to explain here)

The code is as follows (your communication to the computer):

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

The result of running the code (the black part on the right) (the computer's answer to you)

 Rough analysis

1. #include<stdio.h> : Header file (remember first, every C language program must have one and only one).

2. main: (main function, there is one and only one, the entry point of the c language program) , and "int" is modified "main", which means integer, and belongs to one of the data types (beginners are also requested to memorize first: int main), followed by {return 0;}, in general, 3, 4, 7, 8 form a whole

3. printf: This is a library function called print function, which is used to print data (for example: hello world here), which comes with the system. When using it, you need to quote "#include<stdio.h>" (like If I'm going to quote your original work, I'll need to cite you at the beginning).

4. Looking at the whole program, if you need to print data on the screen (including English, Chinese characters, symbols...), just change the hello word to what you want to display on the screen, copy other things, no Can be less. for example;

 3. Data type

We first give the classification of data types:

 Let's first list the commonly used data types at this stage: char, int, long, long long float, double;

To know the data type, we can divide it into these steps

1. Type size

2. The role of type

3. Types of

1. The size of the type (example)

(1). The smallest unit of computer storage, bite

(2). The way the computer stores data is binary storage

(3). Unit conversion

8 bit = 1 Byte one byte

1024 B = 1 KB (KiloByte) Kilobytes

1024 KB = 1 MB (MegaByte) megabyte

1024 MB = 1 GB (GigaByte) gigabytes

1024 GB = 1 TB (TeraByte) Terabyte

1024 TB = 1 PB (PetaByte) petabyte

1024 PB = 1 EB (ExaByte) Exabytes

1024 EB = 1 ZB (ZetaByte) Zetabytes

1024 ZB = 1 YB (YottaByte) YottaByte

(4). The size calculated by sizeof

 Interpretation of the picture: 1, 2, 4, 4... The unit represented is the byte, which means that the memory size occupied by the char type is 1 byte, and so on. (The code will not be interpreted for the time being)

2. The role of type

So many data types are left to us in the c language to better describe various things in life.

It can be seen from the above that different data types occupy different memory, so we need to make reasonable choices based on different things to avoid wasting space

Therefore, the role of the data type is to choose a reasonable type to create variables

4. Variables and constants

1. Understanding of variables and constants

Constant: a quantity whose value does not change

Variables: Quantities whose values ​​can vary

2. Definition of variables

Format: data type variable name; (the two need to be separated by a space, and a comma needs to be added at the end of the statement)

Such as: int a; -------- int is the type, a is the variable name. Generally, after creating a variable, it needs to be initialized, generally assigned a value of 0, such as: int a=0; if you are in a hurry to assign a value, you can also assign it directly when defining it, such as: int a=520;

char ch='w';-------Character type, used to create character variables to store characters.

float weight=520.13f-------floating point type, used to create variables to store decimals. (The following f is to indicate that the variable is of float type, and if it is not added, it means double type)

3. Variable naming rules

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

Such as int s*b; wrong variable name

(2) Cannot start with a number

int 2B; error

(3) The length cannot exceed 63 characters

(4) Case-sensitive in variable names

int a; and int A; are different variables

(5) Variable names cannot use keywords (eg, int, auto....)

 4. Classification of variables

Variables are divided into global variables and local variables

(1) Global variables

 (2) Local variables

 

 5. The life cycle and scope of variables

Scope: (1) Local variable: the local scope where the variable is located

     

 This local variable can only be used between lines 35-39, and it will fail if it is printed outside the braces (starting from the defined position)

              (2) Global variables: the entire project

1. Can be used inside and outside the function

#include<stdio.h>
int a = 520;

void text()
{
    printf("text-->%d",a);
}
int main()
{
    printf("%d\n",a);
    text();
    return 0;
}

 

2. Can be used in another project (need to declare with extern)

 life cycle:

Local variables: enter the scope life cycle start, exit the scope life cycle end (variable destruction)

Global variables: the life cycle of the entire program

6. Constants

Classification of constants: literal constants, constant variables modified by const, identifier constants defined by #define, enumeration constants

Literal constants ------- eg, 20 63.3, 888; these are literal constants

Constant variable modified by const : Modify the variable with const to make the variable have constant attributes, and the value of the variable after modification cannot be changed.

#include<stdio.h>
int main()
{     const int a = 520;//a is a constant variable modified by const, which is still a variable in essence , so it cannot represent the number of arrays

//Such as: arr[a]=0; (we will learn about arrays later)
    printf("%d\n", a);
    //a=100, it is not feasible to change the value of a here
    //printf( "%d",a);
}

Identifier constants defined by #define:

#include<stdio.h>
#define M 1314
int main()
{
    int arr[M] = 0;
    printf("%d\n",M);
}

Enumeration constant (custom type): constants (similar to arrays) used to enumerate one by one need to use the enumeration keyword neum

enum Color //enum Color is a type at this time, similar to a structure

{ RED, //The first one prints 0 by default, and so on

  GREED,//can only end with a comma, not a semicolon

  BLUE,

};

 It is also possible to change the starting value so that it does not start from 0

Ok, the article is over here, friends can consolidate and practice the above knowledge points.

Guess you like

Origin blog.csdn.net/2301_77053417/article/details/131609179