<> one , What is? C language

What is? C language ,C Language is a computer language , What is computer language ?

Just as there are many countries in the world , The language of each country may be different , Have Chinese , English , Russian , French, etc , Well, normally , Like the Chinese , Can communicate in Chinese , Americans communicate in English and so on , If you want to communicate between Chinese and Americans , Or the Chinese speak English , Or Americans speak Chinese , So that the other party can understand what they mean

This metaphor is like computer language , Computer language is : Human and computer communication , Then we need to learn computer language , Why? ? Because computers only know 0 and 1, If you're delving into this , Please Baidu by yourself

So what computer language do we have now , such as :C,C++,Java,Python,JavaScript,Go wait , These are computer languages

Next, let's talk about the history of computer language :
The language recognized by early computers was called binary language , How do you understand this , The computer is a hardware , But it needs electricity , Electricity is divided into positive and negative electricity , So that's what we said above 0 and 1
,0 Negative charge ,1 Positive , They are also called electrical signals

So we want computers to work now , Write a piece of code , That may be the case with binary language representation


Input so many electrical signals to the computer , Is it particularly complicated , Therefore, when writing such code, we had to check the manual to write the correct code

So then mnemonics appeared , Like this code


If this code means ADD, It's an additive mnemonic , Then we'll write the code directly in the future ADD Just fine , It's easy , And the language expressed by these mnemonics is assembly language

The discovery is still very cumbersome , Then there was B language , And in B On the basis of language, we invented what we know well C,C++ wait , from C From the beginning, we call these languages high-level languages

C Language is a process oriented computer programming language , And C++,Java And other object-oriented programming languages .

analysis C Language program code :
// This is us C A main function framework of language , That is, the code is executed from here int main() { return 0; }
Let's print a code first
The first in life C language Hello World
int main() { printf("Hello World\n"); return 0; }
Direct press F10 debug code , Take a look at the execution process

As you can see in page 8 The row has a small arrow , Press again F10, You can see the arrow to the third 9 that 's ok , So the code is from main Function starts execution

Let's create another one main Let's see what happens

So main Function has one and only one

So there's more in this function return 0, This means return 0,0 Is an integer , and main() Before function int It means integer
main() Put before function int express main() The function call returns an integer value

such return 0, Just like int Connect the front and back echoes

But did we see it before C Linguistic main The function is written like this :
void main() { }
you 're right , This is also us C Language writing , But this way of writing is out of date , So when we were writing , Don't write like that again

Then look at our print function printf() This function can actually be disassembled. It is divided into print and function, seeing the name of a thing one thinks of its function , Is the print function

Then this print function is also called library function , The library function is C The language itself encapsulates the functions provided to us , Then this is equivalent to using other people's functions

I'm going to use the functions provided by others “ say hello ” That is to call it :
//include It's called inclusion #include <stdio.h>
So this code means to include stdio.h File

Let's split it again stdio.h

* std: standard
* i:input input
* o:outut output
So header file means standard input / output header file
Then we should introduce this header file when writing input or input functions in the future

So next write a complete code to print the code :
#include <stdio.h> int manin() { printf(" Hello , world !\n"); printf(
" I'm a programmer , Is an engineer who weaves the world with code \n"); return 0; }
<> two , Character type

char Character data format
short Short
int integer
long Long integer
long long Longer integer
float Single-precision floating-point
double High precision floating point number
Let me pick a few examples of how these are used :

* char #include <stdio.h> int manin() { char ch = 'C'; printf("%c\n", ch);
return 0; }
character ‘C’ It can be understood as opening up a space in memory , and ch Is the name of this space
So printing is %c It can be understood as a placeholder , That is to print data in character format , That is, printing ch Corresponding character

* int #include <stdio.h> int manin() { int age = 21; printf("%d\n", age);
return 0; }
Also open up memory space named age, The placeholder for printing is %d , This represents integer decimal data

* float,double #include <stdio.h> int manin() { float f = 5.0; printf("%f\n",
f); return 0; }

#include<stdio.h> int manin() { double d = 3.1415926; printf("%lf\n", d);
return 0; }

These two comparisons can be seen that both output decimal points 6 digit ,double And rounded , This is also an explanation double More accurate

Then why are there so many character types ? Corresponding value type ? Reduce memory consumption ?
Next, we focus on the size of each character type

Then let's write the code :
//sizeof() View size #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/8 printf("%d\n", sizeof(long long)); // 8
printf("%d\n", sizeof(float)); // 4 printf("%d\n", sizeof(double)); // 8 return
0; }
The numerical unit of the output like him is bytes , What is a byte ? I'll just list a few here
bit byte KB MB GB TB ...
The minimum memory unit of the computer is bit,1byte = 8bit,1KB = 1024 At the beginning, the following hexadecimal units are 1024

Let's see that the size of each character type is in bytes , What does this have to do with many data types ?

Because it opens up space in memory , It will involve the size of the development , Let's see int and short type , One is Zhan 4 Bytes , One is Zhan 2 Bytes

Imagine , We use int Defines a person's age as 21 year , Is anyone there 20000 Long live , Certainly not , So when we define age , use short int
Short integers are more appropriate , This is also more reasonable , most important of all , Save memory space

int Type definition , Will occupy 4 Bytes , that is 32 individual bit position , So if you define age , It will seem a waste of space , and short By definition , It's saving int Half space of type ,
long and long long It's more suitable for longer integers

Therefore, each character type has a more appropriate type , Reduces unnecessary memory consumption

We also saw long The length of a long integer is 4 perhaps 8, This is because it is related to the operating system and compiler ,long int Length at least 32 position , and 64 Bit class Unix System is 64 position

<> three , variable

Some values in our lives are constant , as : ID number , blood type , PI, etc , Then these values are called constants
And weight , Age , Salary, etc. will become , This is called a variable

<>1, local variable
#include<stdio.h> int manin() { int a = 10; // local variable return 0; }
<>2, global variable

Variables defined outside the code block
#include <stdio.h> int b = 20; // global variable int manin() { return 0; }
If local and global variables exist at the same time , Local variables will be output first , For example, the following code
#include<stdio.h> int a = 10; int manin() { int a = 100; printf("%d\n", a);
return 0; }

Same variable name , The value of the local variable will be output first

At this point, we have to mention the scope and life cycle

<>1. Scope

* local variable
Scope (scope), Programming concepts , Generally speaking , In general, the names used in a piece of code are not always valid or available

The scope of the code that limits the availability of the name is the scope of the name

Simply put , Where can I use this variable , Where is its scope
#include<stdio.h> int manin() { int a = 100; printf("%d\n", a) return 0; }
Let's look at the code above , In this way, it can be printed directly , If so :
#include<stdio.h> int a = 10; int main() { { int a = 100; } printf("%d\n", a);
return 0; }

Then it is not available , This variable is in the innermost bracket , That is, the scope of this variable

* global variable
We will create a new source file in the source code of this project , Look at the scope of global variables
int c_a = 21;
This can then be used in another source file
#include<stdio.h> int main() { // extern Declare external symbols , Is the variable we created in another source file , Only in this way can it be used extern
int c_a; printf("%d\n", c_a); return 0; }

Therefore, the scope of the global variable is the whole project

<>2. life cycle

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

local variable
The life cycle of a local variable is : Enter scope lifecycle start , Out of scope lifecycle end

global variable
The lifecycle of a global variable is : Entire program life cycle

It's hard for us to explain this declaration cycle in code , So this can only be understood literally

<> four , constant

C The definition forms of constants and variables in language are different
C Constants in languages are divided into the following categories :

* Literal constant
* const Modified constant
* #define Defined identifier constant
* enumeration constant
Let's look at it separately :
// Literal constant #include<stdio.h> int main() { 3; 5; 100; return 0; } //const #include
<stdio.h> int main() { const int a = 8; printf("a=%d\n", a); a = 10; printf(
"a=%d\n", a); return 0; }

Precede the defined variable with a const, If you want to modify variables later, you can't , If removed const, Then it is possible to modify variables

Although by const Modified variables cannot be changed , But variables are still variables in nature , It just can't be modified , So it's called const Modified constant

#define It is also convenient to define constants
// #define #include<stdio.h> #define MIN 22 int main() { int arr[MIN] = {0};
printf("%d\n", MIN) return 0; }
Enumeration is the value that can be enumerated one by one
// enumeration constant enum Color { RED, YELLOW, BLUE }; int main() { enum Color c = BLUE;
// Use in enumeration constants BLUE Assign to variable c return 0; }
Enumeration constants are RED,YELLOW,BLUE This three , These three are also valuable , And it can't be changed
int main() { printf("%d\n", RED); printf("%d\n", YELLOW); printf("%d\n", BLUE);
return 0; }