当前位置:网站首页>Day 7 of learning C language
Day 7 of learning C language
2022-07-27 09:17:00 【ruin987】
Compile preprocessing instructions
# The beginning is compiling preprocessing instructions
They are not C Elements of language , however C Language programs are inseparable from them
#define Used to define a macro
#define
#define< name >< value >
Notice the semicolon that doesn't end , Because not C The statement name of must be a word , Values can be all kinds of things
stay C Before the language compiler starts compiling , Compile preprocessor (cpp) Will change the name in the program to the value
Complete text replacement
gcc--save-temps
Like a function macro
#define cube(x) ((x)*(x)*(x))
Macros can have parameters
Wrong definition of macro
#define RADTODEG(x)(x*57.29568)
#define RADTODEG(x)(x)*57.29578
The principle of macros with parameters
Everything needs parentheses
The whole value needs brackets
Parentheses are required everywhere the parameter appears
#define RADTODEG(x)((x)*57.29578)
Macro with parameters
Can take multiple parameters
#define MIN(a,b)((a)>(b)?(b):(a))
You can also combine ( nesting ) Use other macros
It is very common in the code of large programs
It can be very complicated , Such as " produce " function
stay # and ## With the help of these two operators
There are cultural differences between China and the West
Some macros will be inline Function instead of
Multiple .C file
main() The code in is too long to be divided into several functions
A source code file is too long to be divided into several files
Two independent source code files cannot be compiled to form an executable program
.
project
stay Dev C++ New project in , Then add several source code files
For the project ,Dev C++ The compilation of will compile all the source code files in a project , link
yes , we have IDE There are separate compile and build buttons , The former is to compile a single source code file , The latter is a link to the whole project
Compilation unit
One .C File is a compilation unit
The compiler processes only one compilation unit at a time
The header file
Put the function prototype in a header file ( With ,h ending ) in , In the source code file that needs to call this function (.c file ) in #include This header file , You can let the compiler know the function prototype when compiling
Where you use and define this function, you should #include This header file
The general practice is any .c Have corresponding names .h, Put all publicly exposed function prototypes and global variable declarations in
Only declarations can be placed in header files
Otherwise, there will be entities with duplicate names in multiple compilation units in a project
* Some compilers allow functions with the same name to exist in several compilation units , Or use weak Modifier to emphasize this existence
"" still <>
#include There are two forms to indicate the file to insert
"" The compiler is required to first in the current directory (.c Directory of files ) Look for this file , without , Go to the directory specified by the compiler to find
<> Let the compiler look only in the specified directory
The compiler knows where the header file of its standard library is
Environment variables and compiler command line parameters can also specify the directory to find header files
#include Misunderstanding
#include It's not used to import and store
stdio.h Only in Li printf The prototype of the ,printf The code is in another place , Some .lib(Windows) or .a(Unix) in
current C The language compiler will introduce all standard libraries by default
#include<stdio.h> Just to let the compiler know printf Prototypes of functions , Make sure that the parameter values you give when calling are of the correct type
Functions that are not exposed to the public
Add before function static This makes it a function that can only be used in the compilation unit
Add... Before the global variable static This makes it a global variable that can only be used in the compilation unit
Declaration of variables
int i; Is the definition of variables
extern int i; It's the declaration of variables
Declaration and definition
Declarations are things that do not produce code
The function prototype
Variable declarations
Structure statement
Macro declaration
Enum Declarations
Type declaration
inline function
Definition is what generates code
Repeat statement
In the same compilation unit , A structure with the same name cannot be declared repeatedly
If you have a structural declaration in your header file , It's hard that this header file won't be in a compilation unit #include many times
So we need to " Standard header file structure "
Standard header file structure
#ifndef__list_head__
#define__list_head__
#include"node.h"
typedef struct_list
{
Node*head;
Node*tail;
}List;
#endifUsing conditional compilation and macros , Ensure that this header file will only be #include once
#pragma once Can also play the same role , But not all compilers support
Formatted input and output
%[flage][width][.prec][hlL]typ
| Flag | meaning |
| - | Align left |
| + | Put... In the front + or - |
| (space) | Leave a positive number blank |
| 0 | 0 fill |
| width or prec | meaning |
| number | Minimum number of characters |
| * | The next parameter is the number of characters |
| .number | The number of digits after the decimal point |
| .* | The next parameter is the number of digits after the decimal point |
| Type modifiers | meaning |
| hh | Single byte |
| h | short |
| l | long |
| ll | long long |
| L | long double |
| type | be used for | type | be used for |
| i or d | int | g | float |
| u | unsigned int | G | float |
| o | octal | a or A | Hexadecimal floating point |
| x | Hexadecimal | c | char |
| X | Hexadecimal capital letters | s | character string |
| f or F | float,6 | p | The pointer |
| e or E | Index | n | Read in / Write the number of |
scanf:%[flag]type
| flag | meaning | flag | meaning |
| * | skip | l | long,double |
| Numbers | Maximum characters | ll | long long |
| hh | char | L | long double |
| h | short |
| type | be used for | type | be used for |
| d | int | s | character string ( word ) |
| i | Integers , May be hexadecimal Or octal | [...] | Allowed characters |
| u | unsigned int | p | The pointer |
| o | octal | ||
| x | Hexadecimal | ||
| a,e,f,g | float | ||
| c | char |
FILE
FILE*fopen(const char*restrict path, const char* restrict mode);
int fclose (FILE*stream);
fscanf(FILE*,...)
fprintf(FILE*,...)
Standard code for opening a file
FILE*fp=fopen("file",'r');
if(fp){
fscanf(fp,...);
fclose(fp);
}else{
...
}fopen
| r | Open read only |
| r+ | Turn on read write , Start with the header |
| w | Open only write . If not, create a new one , If present, empty |
| w+ | Turn on read write . If not, create a new one , If present, empty |
| a | Open append . If not, create a new one , If it exists, start at the end of the file |
| ..x | Just new , Cannot open if the file already exists |
边栏推荐
猜你喜欢

As a VC, the auction house invested Web3 for the first time

Mangodb简单使用

Music experience ceiling! Emotional design details of 14 Netease cloud music

500 error reporting

Data interaction based on restful pages

一些实用、常用、效率越来越高的 Kubernetes 别名

巴比特 | 元宇宙每日必读:广州南沙发布“元宇宙九条”措施,平台最高可获得2亿元资金支持...
![[C language _ review _ learn Lesson 2] what is base system? How to convert between hexadecimals](/img/ef/b7a9214e69150c069e352af758f614.png)
[C language _ review _ learn Lesson 2] what is base system? How to convert between hexadecimals

Five kinds of 2D attention finishing (non local, criss cross, Se, CBAM, dual attention)
![[C language _ study _ exam _ review lesson 3] overview of ASCII code and C language](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)
[C language _ study _ exam _ review lesson 3] overview of ASCII code and C language
随机推荐
Svg drawing curve
ES6 new - Operator extension
函数防抖节流
HUAWEI 机试题:字符串变换最小字符串 js
全排列递归思路整理
08_ Service fusing hystrix
qt中使用sqlite同时打开多个数据库文件
Software testing function testing a full set of common interview questions [function testing - zero foundation] essential 4-1
Rewrite the tensorrt version deployment code of yolox
CUDA programming-01: build CUDA Programming Environment
Analog library function
ES6 new - deconstruction assignment of array / object
B tree
[leetcode -- the second day of introduction to programming ability] operator (the number of bit 1 / the difference between the sum of the products of integers)
易语言编程: 让读屏软件可获取标签控件的文本
5g failed to stimulate the development of the industry, which disappointed not only operators, but also mobile phone enterprises
Explanation of common basic controls for C # form application (suitable for Mengxin)
Antdesign a-modal自定义指令实现拖拽放大缩小
ES6 new - array part
The difference between computed and watch