Tip: This article is a summary of the basic knowledge of C language, so it does not quote too much code. It is suitable for friends with zero basic knowledge to learn and watch. If you want to learn more knowledge, you can like it, collect it and follow it. More will be updated in the future. C language notes sharing.
- If you reprint, please indicate the originality, thank you.
Article directory
Preface
提示:此篇博客为C语言基础常识:
Hello everyone, I am Xiao Ming. The article updated today and the entire blog content are the common sense knowledge of C language summarized by Xiao Ming. You can understand more and lay a good foundation for learning C language.
提示:以下是本篇文章正文内容,下面案例可供参考
1. What is a programming language?
Before learning a programming language, you must first understandProgramming languageThis concept:
- When we were very young, our parents taught us to speak and communicate with others. Although there are many languages, such as Chinese, English, French, Korean, etc., and the vocabulary and formats used in communication are different, they can achieve the same purpose. . The language used for communication between people is called " natural language ".
- Similarly, we can also communicate with computers through "language" and let computers do things for us. The language for communication between humans and computers is calledProgramming language. Programming languages also have fixed formats and vocabulary. There are currently thousands of known programming languages. Commonly used languages include C language, C++, Java, C#, Python, JavaScript, Go language, etc.
The development of programming languages has probably gone through the following stages:
Among them, not long after the C language appeared, the computer industry began to explode. Computer hardware became smaller and cheaper, gradually entering government agencies and ordinary households. C language became the main force in programming, operating systems, commonly used software, Hardware drivers, core algorithms, databases, mini-games, etc. are all developed using C language.
2. Introduction to C language
1. Introduction to C language
- The C language is a general-purpose, high-level language originally designed by Dennis Rich at Bell Laboratories for the development of the UNIX operating system. The C language was first implemented in 1972 on the DEC PDP-11 computer.
- C/C++, Java and Python are the most commonly used programming languages in recent years, accounting for almost half of the programming languages!
2.C program structure
Conversion of C language program:
1. After writing the code, save it as a file with the suffix .c
2. Compile the .c program into the target program .obj through the compiler
3. Finally, the linker combines multiple modules and libraries Together generate the executable file .exe
3. The first C program example
#include <stdio.h>
int main()
{
//第一个C程序实例
printf("Hello, World! \n");
return 0;
}
1. The first line of the program
#include <stdio.h>
is a preprocessor directive that tells the C compiler to includestdio.h
the file before actually compiling it.
2. The next line
int main()
is the main function, and the program execution starts from here.
3. The next line is used in pairs with the seventh line. Curly braces can be used to gather the statements in the function into a code block. The code between the two curly braces is the function to be implemented by the function
{
.}
4. The next line
//
is a comment, which will be ignored by the compiler, and the comment content of the program is placed here.
5. The next line
printf(...)
is a function that comes with the C language, and its function is to print a message on the screen"Hello, World!"
.
6. The next line
return 0;
terminates the main() function and returns a value of 0.
3. Basic syntax of C language
1.Header file
#include<stdio.h>
This is the first line of the program. Its function is to import the entire contents of the stdio.h file at the location of this line in the code, introduce the header file using the command, and
#include
put the file name< >
in,#include
and< >
there can be a space between Or not. The files in the following angle brackets<>
can also be other header files, which will be covered later.
2. Main function
int main()
This line of code declares a main function.
main函数
It is a function that comes with C language. The function that comes with C language is calledLibrary Functions. In addition to library functions, we can also write our own functions to expand the functionality of the program.
Characteristics of custom functions and main functions:
1. A C program consists of one or more functions
2. A C program has one and only one main functionmain
3. Program executionmain
starts from the main function and endsmain
in the main function
The function is a C language programbasic unit
An obvious feature of functions is that they must be used with parentheses( )
3. Parentheses, braces and semicolons
int main()
{
//一个简单的例子
/*一个简单的例子*/
}
1. main is the name of the function, and the brackets
( )
indicate that this is a function definition.
2. The code between the curly braces{ }
is the function to be implemented, and only curly braces { } can play this role. Neither parentheses ( ) nor square brackets [ ] will work. Curly braces can also be used to enclose functions in statements are grouped into a code block.
In C programs, the semicolon
;
is the statement terminator. That is, each statement must end with a semicolon. It indicates the end of a logical entity.
Parentheses, curly braces, and semicolons all
必需
exist and cannot be discarded.
4. Comments
//单行注释
/*
多行注释
多行注释
多行注释
*/
The comment formats of C language are single-line comments
//
and multi-line comments/* */
.
//
Single line comments can be made using/* */
Multi-line comments can be used, /* and */ must be used in pairs, and multi-line comments are不可以
nested
effect:
单行注释
later和多行注释
middle的所有内容都会被编译器忽略掉,不产生编译代码,所以可以在一些容易忘记和混淆的代码后面加以备注。
5.Identifier
In layman's terms, an identifier is a name. The name used to identify variables, functions, macro names, and structure names.
Naming rules for identifiers:
1. Can only consist of letters [AZ or az ], numbers [0~9], underscores [ _ ], and the first character不能
isnumber
2. Identifiers in C language are case -sensitive
3. Cannot be usedKeywords
demonstration:
correct | Incorrect |
---|---|
Sum | @day |
sum | 3sum |
_above | a>b |
student_name | M.D.John |
Among them:
1.correctThis column: the first row Sum and the second row sum are different identifiers, because identifiers are case-sensitive, and _above and student_name are identifiers that follow naming rules.
2.mistakeThis column: the first row cannot use the "@" symbol, the second row cannot start with a number, the third row cannot use the ">" symbol, and the fourth row cannot use the "." symbol.
6.Keywords
In C language, in order to define variables , express statement functions and preprocess some files , some characters with special meaning must also be used . This isKeywords, keywordcannotAs a constant name, variable name, or other identifier name.
- Control statement
12
keywords
data type keyword | illustrate |
---|---|
short | Declare short integer variable |
int | Declare integer variable |
long | Declare long integer variable |
float | Declare floating point variables |
double | Declare a double-precision floating-point variable |
char | Declare character variables |
struct | Declare structure type |
union | Declare a union type |
enum | Declare enumeration type |
signed | Declare signed type variables |
unsigned | Declare unsigned type variables |
void | Declare a function with no return value or parameters, declare an untyped pointer |
- Data type
12
keywords
control statement keyword | illustrate |
---|---|
if | Conditional statements |
else | Negative branch of conditional statement (used with if) |
switch | for multi-branch statements |
case | branch of multi-branch statement |
default | Multi-branch statement default |
for | a loop statement |
do | The loop body of the loop statement |
while | Loop condition of loop statement |
goto | unconditional jump statement |
continue | End the current cycle and start the next cycle |
break | Break out of current loop |
return | Subprogram return statement (can take parameters or not) |
- Storage type keywords
4
and other4
keywords
storage types and other types | illustrate |
---|---|
auto | Declare automatic variables |
extern | expand its scope |
register | Declare register variables |
static | Declare static variables (variables with immortal properties) |
volatile | Specifies that variables can be changed implicitly during program execution |
sizeof | Calculate the data type or variable length (i.e. the number of bytes occupied) |
typedef | Used to alias data types |
const | Declare read-only variables |
Summarize
Okay, after reading this, you have read the entire content of this blog. The content is not much and it is very basic. After understanding the content of this chapter, everyone will be basically familiar with some of the knowledge required to learn C language.
[Final and final]: This blog is about the basic knowledge of C language. It is not only Xiao Ming’s own notes, but also the knowledge points that Xiao Ming thinks are shared with everyone. If there are any errors or deficiencies in the content, you are welcome to point them out. Your feedback is Xiao Ming’s. motivation! ! !