C language programming detailed version (learning note 1) can't understand after reading, I can't help it.

computer language

Computer language is the language of communication between people and computers. It is composed of some instructions, including numbers, symbols, grammar and so on.
What is computer language πŸ™Œ
Computer languages include machine language, assembly language and high-level language

  • Machine language: This product is a language that can be directly recognized by the computer. It is a string of instruction sets composed of binary numbers 0 and 1, but for our programmers, machine language is not easy to remember and recognize

  • Assembly language: convert the incomprehensible and memorized machine language into assembly instructions according to the corresponding relationship. These assembly languages use English letters and or symbol strings to replace the machine language, but it is still very difficult for our programmers because the assembly language depends on hardware, that is, the portability of the program is very poor. When we use the computer, We need to learn new assembly instructions, which is very troublesome for us

  • High level language: it is the programming language we use now, which is easy to understand, remember and use (is that really the case 🀷‍β™‚οΈοΌŒ Why is it hai so difficult for me to learn 🀷‍♀️·······)

Programming and algorithm

Algorithm is the soul of programming
What is Farrell?

  • Algorithm: literally: problem solving method and process algorithm combine the actual problem to be solved with the computer program to solve the actual problem

The simplest C language program

Let's start with the code

#Include < stdio. H > / / header file
int main()  
{
	printf("Hello, world!\n"); //Main function
	return 0;
 } 

Simply put, a C program is composed of several header files and functions

  • #Include < stdio. H > is a preprocessing command. It is used to do some preprocessing operations before compiling C programs through the C language compilation system
  • A C program has only one main function, the main function, which is the only entry in C language
  • Before main is the type of the main function
  • printf() is a format output function. Here we just need to remember that its function is to output the specified information on the screen
  • \n is the newline character in the escape character
  • Return is the return value of a function. Depending on the type of function, the type of return value is also different

Secondly, we should develop good writing habits!!!!

Because your code is not only required to run, but also beautiful and easy to read. Readable code is valuable code. If you write the code, others can't understand it, or even you can't understand it, then the code can't be modified. If the program needs to be upgraded or vulnerabilities need to be modified, the efficiency will be very low!

Program interpretation - Notes
There are two annotation methods in C language:

/*This is a multiline comment*/
/*This is the difficult but immortal years of my life,
 I learned that everything is temporary. Opportunities. Feelings. People. Flowers
 I learned that love is giving,
 I learned that exposing weaknesses is the right choice,
 Because it's easy to see the warmth and coldness of the world*/
 //Single-Line Comments 
 //Life is constantly collapsing and healing

We need to remember
Notes are written for programmers, not for computers.

data type

The basic knowledge of C language includes keywords, identifiers, constants, variables, data types and operators

Keywords and identifiers

keyword

Keywords: words defined in advance in the programming language and given special meaning, also known as reserved words. They have special meaning and can not be used at will. Each keyword has a special role
features:

  • Special colors are displayed in the compiler

Keywords in C language (32): auto, double, int, struct, break, long, switch, else, case, enum, register, typedef, char, extern, return, union, construction, float, short, unsigned, continue, for, signed, void, default, goto, sizeof, volatile, do, if, while, static
Five more (restrict, inline, _Bool, _Complex, _Lmaginary) have been added in the latest edition

At this time, someone will ask, how can I remember so much 😣. Don't you embarrass me.
Don't worry, we just need to understand these now. When we meet and use them later, we are understanding and deepening our memory and application

identifier

Identifier: in our programming process, we need to define some symbols to mark some data or content, including variable name, method name, parameter name, array name, etc

C language regulations,

  • The identifier can be a string composed of letters (a ~ Z, a ~ z), numbers (0 ~ 9) and underscores
  • An identifier cannot have a number as the first character
  • Identifier cannot be a keyword in C language
  • Note:

The length of the identifier should not exceed 8 bits,

Identifiers are strictly case sensitive. For example, Name and Name are two different identifiers.

The identifier is best composed of meaningful English words, so as to "see the name and know the meaning", and do not use Chinese. Otherwise, other programmers can't understand the code and want to beat people 😑

Constant and Variable

constant

Constant: an amount that is fixed during program operation.
Classification of constants:

  • String constant: the part enclosed in double quotation marks is called string constant. For example: "abc", "2b"
  • Integer constant: (% d) constant of integer type. It is a number written directly without decimal point. For example, 100.
  • Floating point constant: (% f) a number written directly with a decimal point. For example: - 2.5.
  • Character constant: (% c) any single character enclosed in single quotation marks is called a character constant. For example, 'a' and 'medium'.
  • Boolean constant: there are only two values: true and false.
  • Null constant: null. Represents no data
#define identifier constant value
#include<stdio.h>
#define MONEY 3000 / / define constants and constant values 
int main()
{
	printf("Your flower quota is%d element\n",MONEY) ;//Do you think Alipay will give you more money? 
	return 0; 
	
 } 
 //Operation results
 Your flower quota is 3000

variable

Variable: it is a variable quantity, and each variable will have a name (identifier). Variables occupy a certain storage unit in memory. Variables must be defined before using variables. To distinguish between variable name and variable value are two different concepts.

The general form of variable definition is: data type variable name
Multiple variables of the same type: data type, variable name, variable name

Next, let's go directly to the code and explain it with code

#include<stdio.h> 
int main()
{
int love;    //Define an integer variable called love
love=520;    //Assign the lov variable 520
int a,b,c,d; //Declare multiple variables at the same time, and then assign values respectively
a=1;
b=2;
c=3;
d=4;
printf("%d\n%d\n%d\n%d\n%d\n",love,a,b,c,d);//Print integer variables num,a,b,c,d
//Operation results
520
1
2
3
4
}

Note: continuous assignment is not allowed in the definition, such as int=a=b=c=d; this is illegal

data type

Data types are used to declare different types of variables or functions, and the type of variables determines the space occupied by variable storage and storage mode
Data types in C language are divided into four types: basic type, construction type, pointer type and null type

Basic type

Basic types: integer, character, floating point
I've only listed what we often use 🐱 ‍ 🏍🐱 ‍ πŸš€

data typeexplainbyteapplicationexample
charcharacter1Used to store a single characterchar sex='m'
intinteger2Used to store integersint height=170
folatSingle precision floating point4Used to store decimalsfloat price=12.5
doubleDouble precision floating point8Used to store more decimal placesdouble pi=3.1415926

Here we talk about formatting output statements, which is also more important

Formatted output statement is to output various types of data to the computer screen and display it to us according to the formatted type and specified position

printf("Output format characters",Output target)//The target variable name is output here
#include<stdio.h>
int main()
{
	int a;   //Define integer variable a
	a=1;   //Assign a a value of 1
	printf("%d\n",a) ; //Output a
	char b;  
	b='a'; 
	printf("%c\n",b);
	float c=3.14;
	printf("%f\n",c);
	printf("%s","Hello world");
	//It's OK
	printf("Integer:%d Decimal:%f Character:%c",a,c,b);
	return 0; 
	
 } 
 Operation results:
1
a
3.140000
 Hello world
 Integer: 1 decimal: 3.140000 Character: a

Construction type

The basic data types (character type, integer type and floating point type) provided by C language can not meet the design requirements of complex programs, so
C language allows users to customize data types according to their own needs. These customized data types are called construction types
Construction type: array, enumeration; Common structure
1. Array
Array: it is a set of variables with the same data type. These variables are called array elements. The type of array is determined by the type of elements stored in the array

When defining an array, specify the array type and size
int arr[5]; Define an array of type int with a size of 5
char str[5]; Define an array of char type with a size of 5
float ff[5]; Define an array of float type with a size of 5

2. Enumeration type
In actual programming, the value of some data is often limited and can only be a very small number of integers, and it is best to take a name for each value to facilitate its use in subsequent codes. For example, there are only seven days a week, only twelve months a year, six courses a Class A week, etc.

Enumeration type: used to define variables whose values can be enumerated one by one. The specific format is as follows:

enum Enumeration name{Identifier 1=Integer constant 1,Β·Β·Β·Β·Β·Β·Β·}

Let's look directly at the code πŸ‘©‍🏫

#include <stdio.h>
#define Mon 1 / / are you familiar with it, a little strange? Go up and have a look 🀬😑
#define Tues 2
#define Wed 3
#define Thurs 4
#define Fri 5
#define Sat 6
#define Sun 7
int main(){
int day;
scanf("%d", &day);
switch(day){
case Mon: puts("Monday"); break;
case Tues: puts("Tuesday"); break;
case Wed: puts("Wednesday"); break;
case Thurs: puts("Thursday"); break;
case Fri: puts("Friday"); break;
case Sat: puts("Saturday"); break;
case Sun: puts("Sunday"); break;
default: puts("Error!");
}
return 0;
}
Operation results:
5
Friday

But the side effect is that there are many macro names and loose code. It always looks a little uncomfortable. Let's optimize it

int main(){

enum week{ Mon = 1, Tues, Wed, Thurs, Fri, Sat, Sun } day;

This is called code optimization πŸ˜‚πŸ˜‚πŸ˜‚πŸ˜‚πŸ˜‚πŸ˜‚
This is still very useful for us to write projects in the future
3. Common body
A common body is also called a consortium, which can integrate variables of different data types.
The common body data type is declared using the union keyword. Find out if the above keyword is used. At this time, we are memorizing and understanding the usage of this keyword
The format is as follows:

union Common body type name
{
   Data type member name 1;
   Data type member name 2;
   Β·Β·Β·Β·Β·Β·Β·Β·Β·
}

Upper code (* ~ 3)( ε οΏ£ *)

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

union Data
{
   int i;
   float f;
   char  str[20];
};

int main( )
{
   union Data data;

   data.i = 10;
   data.f = 220.5;
   strcpy( data.str, "C Programming");

   printf( "data.i : %d\n", data.i);
   printf( "data.f : %f\n", data.f);
   printf( "data.str : %s\n", data.str);

   return 0;
}
//Operation results
data.i : 1917853763
data.f : 4122360580327794860452759994368.000000
data.str : C Programming

Why? Only the last value is displayed,????????
Because the value later assigned to the variable takes up memory location

After defining a common body variable, you need to initialize the common body variable. When initializing a common body variable, only one member can be initialized. Only one member can be used at a time

union Common body type name common body variable={The type value of one of the members}

Less nonsense and code

//example
#include <stdio.h>
#include <string.h>
union Data
{
   int i;
   float f;
   char  str[20];
};
int main( )
{
   union Data data;
   
   data.i = 5;
   printf( "data.i : %d\n", data.i);

   data.f = 250.2;
   printf( "data.f : %f\n", data.f);

   strcpy( data.str, "C Programming");
   printf( "data.str : %s\n", data.str);

   return 0;
}
//Operation results
data.i : 5
data.f : 250.200000
data.str : C Programming
//Here, all members can be output intact, because only one member is used at the same time.

Do you understand
structural morphology
Above, we explained the common body, which is a set of data with the same type. In the actual programming process, we need a group of data with different types. For example, for the student information registration form, the name is a string, the student number is an integer, the age is an integer, the learning group is an integer, and the score is a decimal. Obviously, it can not be stored in an array because of different data types.

full nameStudent numberAgeStudy Groupachievement
charintintintfloat

In C language, structure can be used to store a set of different types of data. The definition form of structure is:

struct Structure name{
    A variable or array contained in a structure
};

A structure is a collection that contains multiple variables or arrays. Their types can be the same or different. Each such variable or array is called a Member of the structure.
Stop talking nonsense and write the code 🐱 ‍ 🏍🐱 ‍ 🏍🐱 ‍ 🏍🐱 ‍ 🏍

struct stu{
char *name; //full name
int num; //Student number
int age; //Age
int group; //Study Group
float score; //achievement
};

stu here is the structure name, which contains five members: name, num, age, group and score. Structure members are defined in the same way as variables and arrays, but cannot be initialized.

Notice the semicolon after the braces; No less, this is a complete statement.

Structure is also a data type, which is defined by the programmer and can contain multiple other types of data.

int, float, char, etc. are data types provided by the C language itself and cannot be split. We call them basic data types; The structure can contain multiple basic types of data or other structures. We call it complex data type or construction data type.

Structure variable

Since a structure is a data type, it can be used to define variables.
For example: struct stu stu1, stu2;
Two variables stu1 and stu2 are defined, both of which are stu types and are composed of five members.
Note that the keyword struct cannot be less

stu is like a "template", and the defined variables have the same properties. You can also compare the structure to "drawing" and the structure variable to "part". The characteristics of parts produced according to the same drawing are the same.

You can also define structure variables while defining structures:

struct stu{
char *name; //full name
int num; //Student number
int age; //Age
int group; //Study Group
float score; //achievement
} stu1, stu2;

Place the variable at the end of the structure definition.

If only stu1 and stu2 variables are required, and other variables do not need to be defined with the structure name later, the structure name may not be given during the definition, as shown below:

struct{ //Did not write stu
char *name; //full name
int num; //Student number
int age; //Age
char group; //Study Group
float score; //achievement
} stu1, stu2;

This is easy to write, but because there is no structure name, it is impossible to define a new variable with this structure later.

Acquisition and assignment of members

Structure is similar to array. It is also a set of data. It doesn't make much sense to use it as a whole. The array uses the subscript [] to get a single element, and the structure uses the dot. To get a single member. The general format for obtaining structure members is:

Structure variable name.member name;

In this way, you can get the value of the member or assign a value to the member:

#include <stdio.h>
int main(){
struct{
char *name; //full name
int num; //Student number
int age; //Age
int group; //Group
float score; //achievement
} stu1;
//Assign values to structure members
stu1.name = "Jay Chou";
stu1.num = 1001;
stu1.age = 18;
stu1.group = 1;
stu1.score = 100;
//Reads the value of a structure member
printf("%s My student number is%d,Age is%d,stay%d Group, this year's result is%.1f!\n", stu1.name, stu1.num, stu1.age, stu1.group, stu1.score);
return 0;
}
//Operation results
 Jay Chou's student number is 1001 and his age is 18. In group 1, his score this year is 100.0!

In addition to assigning values to members one by one, they can also be assigned as a whole during definition, for example:

struct{
char *name; //full name
int num; //Student number
int age; //Age
char group; //Group
float score; //achievement
} stu1 = { "Jay Chou", 1001, 18, 1, 100 };
//Operation results
 Jay Chou's student number is 1001 and his age is 18. In group 1, his score this year is 100.0!

However, the overall assignment is limited to the definition of structural variables. In the process of use, only members can be assigned one by one, which is very similar to the assignment of arrays.

It should be noted that structure is a user-defined data type and a template for creating variables, which does not occupy memory space; Structure variables contain real data and need memory space to store.


Did you say there was a problem? What's the difference between this and the above common body? What did you say??
This is the difference between a common body and a structure:

Community: make several variables of different types occupy a section of memory (covering each other). The memory length occupied is the memory length occupied by the longest member.

Structure: combine different types of data into a whole. The memory length occupied is the sum of the memory length occupied by each member.

Structure, let's talk about it here, πŸ˜‚πŸ˜‚πŸ˜‚πŸ˜‚ I'll meet you later (are you probably...)

From now on, stick to it and make progress a little bit a day. In the near future, you will thank you for your efforts!

If something goes wrong again, please correct it.

Newcomers check in, friends, give me a triple (praise, attention, collection)

Tags: C C++

Posted on Tue, 19 Oct 2021 16:34:26 -0400 by Ken2k7