当前位置:网站首页>First acquaintance with C language (1)
First acquaintance with C language (1)
2022-07-28 05:18:00 【It Xiaoting】
List of articles
Preface
c Language is almost the basis of programming , As a foundation , It must be handled carefully , Now take a look at this article ---- To introduce the c The basis of , Very friendly to Xiaobai , Clear and easy to understand .
This article mainly introduces the commonly used functions and data types , Constant variable , Escape character , String, etc.
One 、c What is it? ?
For many people who have not been exposed to programming, and even some people who have been exposed to it c The concept of may not be understood so clearly , So let's start from here c.
c Language is a computer language , And computer language is the language that people communicate with computers (c It's just one of them , Computer language and python,java etc. ), meanwhile c Language is a process oriented language ( Pay more attention to process ),c++,java And so on are object-oriented languages ( Object oriented ). In the face of c When programming , I'm using vs2019 edition , Note that at this time .c At the end is the source file ,.h The end is the header file , When compiling, we need to use the source file !
Two 、main function
Let me first introduce main function , because main Function is the entry of a program , Especially important ,main The general format of the function is as follows :return 0 Means to return normally , When return When the following number is non-zero, it means abnormal return , therefore main In the function is return 0.
int main()
{
return 0;
}
3、 ... and 、printf function
To learn about the second common function —printf function ( It is also a library function ), When using it, you need to import header files ( The use of library functions requires the introduction of header files )------stdio.h, This function indicates that you want to print data on the screen ( Present to us on the console ), The format is as follows ( The output is as follows ):
#include <stdio.h>
int main(){
printf("hello c");
return 0;
}

Add : When printing :
If it is a decimal integer , The front is %d
If decimal , The front is %f
If character , The front is %c
If it's a string , The front is %s
Four 、scanf function
Enter data from the keyboard , At this time, pay attention to vs Use... Directly scanf Will report a mistake , It will prompt you to use scanf_s This function , Although the program can work normally , but scanf_s This function is vs Provided , Therefore, it does not have cross platform , So it's not recommended , A better solution is : In the first line of all code in the source file , first line , first line ( Important things are to be repeated for 3 times ) Add a piece of code :#define _CRT_SECURE_NO_WARNINGS 1 It's working scanf Function will not be wrong , The specific code is as follows ( give the result as follows ):
Input format ( Looking at the type in front, it changes , Take integer as an example ):scanf(“%d”,& The variable where you want to put the input number );
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
int main()
{
int n;
// Make a statement
printf(" Please enter a number from the keyboard :");
// Input
scanf("%d", &n);
// Can output n See if it is entered
printf("%d", n);
return 0;
}

Four 、 data type
1.char // Character type ( It can also be understood as describing integer , Its ASCALL The code is an integer )
2.int // integer ( For integers )
3.long // Long integer ( For integers )
4.long long // Longer integers ( For integers )
5.short // Short ( For integers )
6.float // Single precision floating point number ( For decimals )
7.double // Double precision floating point ( For decimals )( If it is a decimal , The compiler defaults to double type )
Why are there so many types of integers and decimals ?
The allowable range of each type is different ( Data in the computer is stored in binary form , A binary representation unit is one bit (1b) and 1B( byte )=8b), We can use sizeof Function to see how wide the type is ( It's in bytes ), The code is as follows :( Notice I'm here 64bit Operation of , If 32bit The result is different from this )
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main() {
printf("%d\n", sizeof(char));
printf("%d\n", sizeof(int));
printf("%d\n", sizeof(long));
printf("%d\n", sizeof(long long));
printf("%d\n", sizeof(short));
printf("%d\n", sizeof(float));
printf("%d\n", sizeof(double));
return 0;
}

5、 ... and 、 Variables and constants
You can know from the name , Variables represent variable quantities , A constant represents an immutable quantity .
1. Variable
A. Variable definition method : type + Variable name
eg: int age;( uninitialized )
int n=18;( Initialize the )
Be careful : Variable names are not given casually , There is a naming rule :
a. Only letters ( Uppercase or lowercase ), Numbers and underscores (_) form .
b. The number can't start
c. Cannot use keyword
d. Variable names are case sensitive
e. The length cannot exceed 63 Characters (64bit In the system );
B. Variables are divided into local variables and global variables
You can simply distinguish : Inside the braces are local variables , The external variables are global variables .
( When local variables and global variables have the same name , Local variables are preferred .)
eg:
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int m = 100;// Global variables
int main() {
int n = 20;// local variable
return 0;
}
Add a point of knowledge : Life cycle concept : The time period between the creation of variables and the destruction of variables .
Global variables can be applied to the entire c project ( self-created , Below I will have code examples ), Local variables can only be applied to their own local range .
The life cycle of global variables is the life cycle of the whole program .
The declaration cycle of a local variable begins with the range it enters , Out of that range, it ends .
Application examples of global variables :
a. The most basic application
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int m = 100;// Global variables
int main() {
printf("%d\n", m);
return 0;
}

b. Second usage ( This is mainly to show that it can be applied to the whole c project )
In other .c Use another in the file .c You only need to declare the global variables of the file , You can use it .(eg:extern int m;)
test01.c in :
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int m = 100;// Global variables
test02.c in :
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
int main() {
extern int m;
printf("%d\n", m);
return 0;
}

2. Constant
a. Literal constants ( A constant value written directly )
eg:30,“abcdefg”,2.45
b.const Modified constant variable ( But it is not allowed to be modified , It is still a variable )
eg:const int m=88;
c.#define Defined identifier constant ( Cannot be modified , Constant in nature )
The format is :#define name The number
eg:
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#define max 100
int main() {
int a = max;
printf("%d\n", a);
return 0;
}
d. Enumeration constants
The format is as follows :
enum name {
(eg:)male,// Can be initialized ,eg:male=4,( It is not allowed to change after initialization )
(eg:)female,
(eg:)age
};
male,female,age Are enumeration constants , When not initialized , The default value of the enumeration constant at the beginning is 0, Add one next .
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
enum sex
{
male,
female,
age
};
int main() {
printf("%d\n", male);
printf("%d\n", female);
printf("%d\n", age);
return 0;
}

6、 ... and 、 character string
Someone can't distinguish between characters and strings , Briefly , The output on the keyboard is all characters , Enclosed in single quotation marks are characters , Enclosed in double quotation marks is a string . stay c in
eg:“asdfgg”-----> character string
‘g’------> character
String end flag \0, When calculating the length of a string ,\0 Not included in the length
It can be used strlen To calculate the string length , but strlen For a library function , You need to import header files when using
#include<string.h>
A. First, distinguish whether there is \0
char arr1[]=“abcdef”;// At the end \0
char arr2[]={‘a’,‘b’,‘c’,‘d’,‘e’,‘f’};// There is no \0
The output result after printing is :( Because the second defined array is not followed by \0, So I will always look back , Until I met \0 Will stop , So the second print will show the results shown in the figure )
The second solution :
char arr2[]={‘a’,‘b’,‘c’,‘d’,‘e’,‘f’,‘\0’};
B. Use strlen Calculate string length
\0 Previous characters , not \0 In itself
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>
int main() {
char arr1[] = "abcdef";
char arr2[] = {
'a','b','c','d','e','f'};
char arr3[] = {
'a','b','c','d','e','f','\0' };
printf("%d\n", strlen(arr1));
printf("%d\n", strlen(arr2));
printf("%d\n", strlen(arr3));
return 0;
}

7、 ... and 、 Escape character
Change the meaning of the original characters .
The approximate table is as follows :
Three letter words ----- A three letter word is a sequence of several characters , Together, it represents another character .
This escape character is often used for paths :
The output path :
Output as shown in the figure , The result is not quite right , Pictured :
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main() {
char arr[] = "c:\test\code";
printf("%s\n", arr);
return 0;
}

After improvement :
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main() {
char arr[] = "c:\\test\\code";
printf("%s\n", arr);
return 0;
}

8、 ... and 、 notes
- //--------> This comment can be used for multiple lines or single lines
- /…/ Comment on multiple statements
summary
This article is mainly about c Part of the basic knowledge of the language is explained in detail , For example, common data types , String, etc. ,c See the following notes for other basic knowledge in , Now it is c The basis of , You must understand thoroughly , In this way, I can understand later c It's helpful in the deep layer of .
边栏推荐
- 测试开发---自动化测试中的UI测试
- 阿里怎么用DDD来拆分微服务?
- Bean的作用域、执行流程、生命周期
- [internal mental skill] - creation and destruction of function stack frame (C implementation)
- Pipe /createpipe
- 7.<tag-字符串和API的取舍>补充: 剑指 Offer 05. 替换空格
- Dell remote control card uses ipmitools to set IPMI
- Simulink automatically generates STM32 code details
- 基于MPLS构建虚拟专网的配置实验
- POJ 3417 network (lca+ differential on tree)
猜你喜欢

Visual studio 2019 new OpenGL project does not need to reconfigure the environment

【内功心法】——函数栈帧的创建和销毁(C实现)

FreeRTOS startup process, coding style and debugging method

MySQL(5)

Flink mind map

How does Alibaba use DDD to split microservices?

Mysql基本查询

The go zero singleton service uses generics to simplify the registration of handler routes

如何在 FastReport VCL 中通过 Outlook 发送和接收报告?

Configuration experiment of building virtual private network based on MPLS
随机推荐
【CVPR2022】Lite Vision Transformer with Enhanced Self-Attention
PC side bug record
[internal mental skill] - creation and destruction of function stack frame (C implementation)
【ARXIV2204】Vision Transformers for Single Image Dehazing
数据库日期类型全部为0
Flink mind map
Implementation of simple upload function in PHP development
The default isolation level of MySQL is RR. Why does Alibaba and other large manufacturers change to RC?
Have you ever seen this kind of dynamic programming -- the stock problem of state machine dynamic programming (Part 2)
FreeRTOS startup process, coding style and debugging method
PC端-bug记录
类和对象【中】
FreeRTOS learning (I)
驾驭EVM和XCM的强大功能,SubWallet如何赋能波卡和Moonbeam
【ARXIV2203】SepViT: Separable Vision Transformer
Making RPM packages with nfpm
阿里怎么用DDD来拆分微服务?
Introduction to testcafe
【CVPR2022 oral】Balanced Multimodal Learning via On-the-fly Gradient Modulation
7. < tag string and API trade-offs> supplement: Sword finger offer 05. replace spaces