当前位置:网站首页>Overview of basic knowledge of C language
Overview of basic knowledge of C language
2022-07-03 05:04:00 【The little moon who loves to knock code】
List of articles
ps: Basic understanding C Basic knowledge of language , Yes C There is a general understanding of language .
Every knowledge point is a simple understanding , Don't explain it in detail , Subsequent complement .
1. What is? C Language
C Language is a general computer programming language , Widely used in the underlying development .C The goal of language design is to provide a way to make it easy
The way to compile 、 Processing low-level memory 、 Generate a small amount of machine code and programming language that can run without any running environment support
said .
Even though C Language provides many low-level processing functions , But it still has good cross platform characteristics , Written in a standard specification
C Language programs can be compiled on many computer platforms , It even includes some embedded processors ( Single chip computer MCU) And super
Level computer and other operating platforms .
The 1980s , In order to avoid the use of C There are differences in language grammar , By the National Bureau of standards C Language system
A complete set of American national standard grammar , be called ANSI C, As C The original standard of language . [1] at present 2011 year 12 month 8
Japan , International Organization for Standardization (ISO) And the International Electrotechnical Commission (IEC) released C11 The standard is C The third official mark of language
accurate , It's also C The latest standard of language , The standard better supports Chinese character function name and Chinese character identifier , To a certain extent, it has realized Han
Word programming .
C Language is a process oriented computer programming language , And C++,Java Object oriented programming languages such as .
Its compiler mainly includes Clang、GCC、WIN-TC、SUBLIME、MSVC、Turbo C etc. .
2. first C Language program
#include <stdio.h>
int main()
{
printf("hello bit\n");
printf("he he\n");
return 0; }
// explain :
//main Function is the entry of a program
// In a project main There is and only one function
3. data type
char // Character data type
short // Short
int // plastic
long // Long integer
long long // Longer plastic surgery
float // Single-precision floating-point
double // Double precision floating point
ps: The size of the data type is bytes (byte)
ps: There are so many types , In fact, it is to express various values in life more abundantly .
4. Variable , Constant
Some values in life are constant ( such as : PI , Gender , Blood type and so on )
Some values are variable ( such as : Age , weight , Salary ).
Constant value ,C Used in language Constant The concept of , Become value C Used in language Variable To express .
4.1 How to define variables
char name[] = "eleven";
int height = 180;
4.2 Classification of variables
- local variable
- Global variables
#include <stdio.h>
int year = 2021;// Global variables
int main()
{
// As defined below year Will there be a problem ?
int year = 2022;// local variable
printf("year = %d\n", year);
return 0;
}
ps: The local variable above year There is no problem with the definition of variables !
When a local variable has the same name as a global variable , Local variables take precedence .
4.3 Use of variables
#include <stdio.h>
int main()
{
int num1 = 0;
int num2 = 0;
int sum = 0;
printf(" Enter two operands :>");
scanf("%d %d", &num1, &num2);
sum = num1 + num2;
printf("sum = %d\n", sum);
return 0; }
//scanf Input function
//printf Output function
4.4 The scope and life cycle of variables
Scope
Scope (scope) It's a programming concept , Generally speaking , The names used in a program are not always valid / Usable , The scope of the code that defines the usability of the name is the scope of the name .
- The scope of a local variable is the local scope of the variable .
- The scope of global variables is the whole project .
Life cycle
The life cycle of a variable is the period between the creation of a variable and its destruction
- The life cycle of a local variable is : Enter the scope lifecycle begins , Out of scope life cycle ends .
- The life cycle of global variables is : The whole life cycle of the program .
4.5 Constant
C There are differences in the form of definitions between constants and variables in languages .
C Constants in a language can be divided into the following categories :
- Literal constants
- const Modified constant variable
- #define Defined identifier constant
- Enumeration constants
#include <stdio.h>
// give an example
enum Sex
{
MALE,
FEMALE,
SECRET
};
// In brackets MALE,FEMALE,SECRET It's an enumeration constant
int main()
{
// Literal constants demonstrate
3.14;// Literal constants
1000;// Literal constants
//const Modified constant variable
const float pai = 3.14f; // there pai yes const Modified constant variable
pai = 5.14;// It can't be modified directly !
//#define The identifier constant for demonstration
#define MAX 100
printf("max = %d\n", MAX);
// Enumeration constants demonstrate
printf("%d\n", MALE);//0
printf("%d\n", FEMALE);//1
printf("%d\n", SECRET);//2
// notes : The default of enumeration constants is from 0 Start , Increase in descending order 1 Of
return 0;
ps: In the above example pai go by the name of const Modified constant variable , const The modified constant is C In language, variables are limited only at the grammatical level pai Can't be changed directly , however pai Is essentially a variable , So it's called a constant variable .
5. character string + Escape character + notes
5.1 character string
"hello world"
This is made up of double quotation marks (Double Quote) A string of characters is called a string literal (String Literal), Or string for short .
ps: The end of a string is a \0 The escape character of . When calculating the length of a string \0 It's the end sign , It doesn't count as string content .
#include <stdio.h>
// The following code , What is the result of printing ? Why? ?( prominent '\0' Importance )
int main()
{
char arr1[] = "hello";
char arr2[] = {
'h', 'e', 'l', 'l', 'o'};
char arr3[] = {
'h', 'e', 'l', 'l', 'o', '\0'};
printf("%s\n", arr1);//5
printf("%s\n", arr2);// Random value
printf("%s\n", arr3);//5
return 0;
}
5.2 Escape character
If we want to print a directory on the screen : c:\code\test.c
How do we write code ?
#include <stdio.h>
int main()
{
printf("c:\code\test.c\n");
return 0;
}
In fact, the result of the program is :
I have to mention the escape character here . Escape characters, as the name suggests, change meaning .
Let's look at some escape characters :
| Escape character | paraphrase |
|---|---|
| \? | Use... When writing multiple consecutive question marks , Prevent them from being parsed into three letter words |
| \’ | Used to represent character constants ’ |
| \" | Double quotation marks used to represent the inside of a string |
| \\ | Used to indicate a backslash , Prevent it from being interpreted as an escape sequence character |
| \a | Warning characters , Beep |
| \b | Back space |
| \f | Change the page |
| \n | Line break |
| \r | enter |
| \t | Horizontal tabs |
| \v | Vertical tabs |
| \ddd | ddd Express 1~3 Eight octal numbers . Such as : \130 X |
| \xdd | dd Express 2 Hexadecimal numbers . Such as : \x30 0 |
Small test
1
#include <stdio.h>
int main()
{
// problem 1: Print a single quote on the screen ', How do you do it? ?
// problem 2: Print a string on the screen , The content of the string is a double quote “, How do you do it? ?
return 0;
}
2
// What does the program output ?
#include <stdio.h>
int main()
{
printf("%d\n", strlen("abcdef"));
printf("%d\n", strlen("c:\test\628\test.c"));
return 0;
}
Refer to the answer
1.
printf("%c\n", '\'');
printf("%s\n", "\"");
2.
6
14// /t,\62 Parsed into an escape character
Hee hee , Did you do it right
5.3 notes
- There are unnecessary codes in the code that can be deleted directly , You can also comment out
- Some of the code is hard to understand , You can add the annotation text
such as :
#include <stdio.h>
int Add(int x, int y)
{
return x+y;
}
/*C Language style notes int Sub(int x, int y) { return x-y; } */
int main()
{
//C++ Annotation style
//int a = 10;
// call Add function , Complete the addition
printf("%d\n", Add(1, 2));
return 0;
}
There are two styles of annotation :
- C Notes on language style /* xxxxxxx*/
defects : You can't nest comments - C++ Style notes //xxxxxxxx
You can comment on one line or multiple lines
6. Select statement

#include <stdio.h>
int main()
{
int coding = 0;
printf(" Would you type the code ?( choice 1 or 0):>");
scanf("%d", &coding);
if(coding == 1)
{
printf(" come on. , Bai Fumi is waving to you \n");
}
else
{
printf(" Go home and farm \n");
}
return 0; }
Click on this blog , That's the choice !
give the thumbs-up , Comment on , Collection , Focus on , It's also a choice !
7. Loop statement
C How to implement a loop in a language ?
- while sentence
- for( Subsequent complement )
- do…while( Subsequent complement )
Code up
//while Examples of cycles
#include <stdio.h>
int main()
{
int row = 0;
while(row<=30000)
{
printf(" I'm going to keep typing \n");
row++;
}
if(line>30000)
{
printf(" good offer\n");
}
return 0;
}
8. function
The feature of the function is to simplify the code , Code reuse .
Go straight to the code :
#include <stdio.h>
int main()
{
int num1 = 0;
int num2 = 0;
int sum = 0;
printf(" Enter two operands :>");
scanf("%d %d", &num1, &num2);
sum = num1 + num2;
printf("sum = %d\n", sum);
return 0;
}
The above code , Write the function as follows :
#include <stdio.h>
int Add(int x, int y)
{
int z = x+y;
return z;
}
int main()
{
int num1 = 0;
int num2 = 0;
int sum = 0;
printf(" Enter two operands :>");
scanf("%d %d", &num1, &num2);
sum = Add(num1, num2);
printf("sum = %d\n", sum);
return 0;
}
9. Array
To store 1-10 The number of , How to store ?
C The definition of array is given in the language : A collection of elements of the same type
9.1 To define an array
| int arr[10] = {1,2,3,4,5,6,7,8,9,10};// Define an integer array , Put at most 10 Elements |
|---|
9.2 Index of the array
C Language policy : Each element of an array has a subscript , The subscript is from 0 At the beginning .
Array can be accessed by subscript .
| int arr[10] = {0}; |
|---|
| // If the array 10 Elements , The scope of the subscript is 0-9 |

9.3 Use of arrays
#include <stdio.h>
int main()
{
int i = 0;
int arr[10] = {
1,2,3,4,5,6,7,8,9,10};
for(i=0; i<10; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
11. The operator
First, a brief introduction , Make up later
- arithmetic operator
+ - * / %
- Shift operator
>> <<
- Bit operators
& ^ |
- Assignment operator
= += -= *= /= &= ^= |= >>= <<=
- Monocular operators
! Logical anti operation
- negative
+ Comes at a time
& Address fetch
sizeof The type length of the operands ( In bytes )
~ To reverse the binary of a number
-- In front of 、 After --
++ In front of 、 After ++
* Indirect access operators ( Dereference operator )
( type ) Cast
- Relational operator
>
>=
<
<=
!= It's not equal
== equal
- Logical operators
&& Logic and
|| Logic or
- Conditional operators
exp1 ? exp2 : exp3
- Comma expression
- exp1, exp2, exp3, ...expN
- Subscript reference 、 Function calls and structure members
[] () . ->
12. Common keywords

C The language provides a wealth of keywords , These keywords are preset by the language itself , Users cannot create keywords themselves .
13.define Define constants and macros
Code up. , Easy to understand
//define Define identifier constants
#define MAX 1000
//define Defining macro
#define ADD(x, y) ((x)+(y))
#include <stdio.h>
int main()
{
int sum = ADD(2, 3);
printf("sum = %d\n", sum);
sum = 10*ADD(2, 3);
printf("sum = %d\n", sum);
return 0; }
14. The pointer
14.1 Memory
Memory is a very important memory on a computer , All programs in a computer run in memory .
So in order to use memory efficiently , Divide the memory into small memory units , The size of each memory unit is 1 Bytes .
In order to effectively access every unit of memory , The memory units are numbered , These numbers are called the The address of the memory unit .
| Memory | Address |
|---|---|
| A byte | 0x00000002 |
| A byte | 0x00000001 |
| A byte | 0x00000000 |
Variables are created in memory ( To allocate space in memory ), Each memory cell has an address , So variables also have addresses .
Take out the variable address as follows :
#include <stdio.h>
int main()
{
int num = 10;
#// Take out num The address of
// notes : here num Of 4 Bytes , Every byte has an address , What is taken out is the address of the first byte ( Smaller address )
printf("%p\n", &num);// Print address ,%p Is printed as an address
return 0;
}

How to store the address , Need to define pointer variables .
int num = 10;
int *p;//p For an integer pointer variable
p = #
The use of pointers :
#include <stdio.h>
int main()
{
int num = 10;
int *p = #
*p = 20;
return 0;
}
Take the example of a reshaped pointer , It can be extended to other types , Such as :
#include <stdio.h>
int main()
{
char ch = 'w';
char* pc = &ch;
*pc = 'q';
printf("%c\n", ch);
return 0;
}
14.2 The size of the pointer variable
#include <stdio.h>
// The size of the pointer variable depends on the size of the address
//32 The address under the bit platform is 32 individual bit position ( namely 4 Bytes )
//64 The address under the bit platform is 64 individual bit position ( namely 8 Bytes )
int main()
{
printf("%d\n", sizeof(char *));
printf("%d\n", sizeof(short *));
printf("%d\n", sizeof(int *));
printf("%d\n", sizeof(double *));
return 0; }
ps: The size of the pointer is 32 Bit platform is 4 Bytes ,64 Bit platform is 8 Bytes .
15. Structure
The structure is C Especially important knowledge points in language , The structure makes C Language has the ability to describe complex types .
For example, describe students , Students include : name + Age + Gender + Student number These information .
Only structures can be used to describe .
for example :
struct Stu
{
char name[20];// name
int age; // Age
char sex[5]; // Gender
char id[15]; // Student number
};
Initialization of structure :
// Print structure information
struct Stu s = {
" Zhang San ", 20, " male ", "20180101"};
//. Access operators for structure members
printf("name = %s age = %d sex = %s id = %s\n", s.name, s.age, s.sex, s.id);
//-> The operator
struct Stu *ps = &s;
printf("name = %s age = %d sex = %s id = %s\n", ps->name, ps->age, ps->sex, ps- >id);
Summary :
Come here C I have probably passed the elementary knowledge of language , Thank you for watching , Later, bloggers will send out detailed versions after learning new knowledge , The first time I write a blog, there may be some things I haven't done well , Please forgive me , If there are mistakes, you are welcome to point them out .
Cough , You see this , I'll go if I like it ~

边栏推荐
- 1106 lowest price in supply chain (25 points)
- Market status and development prospects of the global IOT active infrared sensor industry in 2022
- What is UUID
- MySQL master-slave configuration
- [research materials] 2021 annual report on mergers and acquisitions in the property management industry - Download attached
- Promise
- [Yu Yue education] basic reference materials of interchangeability and measurement technology of Zhongyuan Institute of Technology
- Esp32-c3 learning and testing WiFi (II. Wi Fi distribution - smart_config mode and BlueIf mode)
- "Hands on deep learning" pytorch edition Chapter II exercise
- ZABBIX monitoring of lamp architecture (3): zabbix+mysql (to be continued)
猜你喜欢

"Hands on deep learning" pytorch edition Chapter II exercise
![[research materials] the fourth quarter report of the survey of Chinese small and micro entrepreneurs in 2021 - Download attached](/img/01/052928e7f20ca671cdc4c30ae55258.jpg)
[research materials] the fourth quarter report of the survey of Chinese small and micro entrepreneurs in 2021 - Download attached

Thesis reading_ Chinese NLP_ ELECTRA
![[research materials] 2022q1 game preferred casual game distribution circular - Download attached](/img/13/5a67c5d08131745759fdc70a71cf0f.jpg)
[research materials] 2022q1 game preferred casual game distribution circular - Download attached

Basic knowledge of reflection (detailed explanation)

Burp suite plug-in based on actual combat uses tips

Review the configuration of vscode to develop golang

JQ style, element operation, effect, filtering method and transformation, event object

Source insight garbled code solution

JS dynamic table creation
随机推荐
[research materials] 2022q1 game preferred casual game distribution circular - Download attached
Mobile terminal - uniapp development record (public request encapsulation)
The consumption of Internet of things users is only 76 cents, and the price has become the biggest obstacle to the promotion of 5g industrial interconnection
Use posture of sudo right raising vulnerability in actual combat (cve-2021-3156)
论文阅读_清华ERNIE
Keepalived热备与HAProxy
[set theory] relation properties (transitivity | transitivity examples | transitivity related theorems)
Go language interface learning notes Continued
Notes | numpy-10 Iterative array
leetcode452. Detonate the balloon with the minimum number of arrows
Realize file download through the tag of < a > and customize the file name
1118 birds in forest (25 points)
On typescript and grammar
leetcode860. Lemonade change
How to connect the network: Chapter 2 (Part 1): a life cycle of TCP connection | CSDN creation punch in
1099 build a binary search tree (30 points)
Market status and development prospect prediction of the global fire hose industry in 2022
Huawei personally ended up developing 5g RF chips, breaking the monopoly of Japan and the United States
Market status and development prospect prediction of global colorimetric cup cover industry in 2022
String matching: find a substring in a string