当前位置:网站首页>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 |
边栏推荐
- [C language - zero foundation _ study _ review _ lesson 4] data types and operations
- Mangodb simple to use
- 【云驻共创】华为云:全栈技术创新,深耕数字化,引领云原生
- Apple cut its price by 600 yuan, which was almost a devastating blow to the collapse of its domestic flagship mobile phone
- Software testing function testing a full set of common interview questions [function testing - zero foundation] essential 4-1
- Save Xiaoyi from Netease written test -- a typical application of Manhattan distance
- DNS domain name space
- Qdoublevalidator does not take effect solution
- [cloud co creation] Huawei cloud: full stack technology innovation, deep digitalization, leading cloud native
- Analog library function
猜你喜欢

Cross domain and processing cross domain

MySQL transaction

500 error reporting

Data interaction based on restful pages
![Software testing function testing a full set of common interview questions [function testing - zero foundation] essential 4-1](/img/1c/c1c1b15e502ee901a396840c01e84d.png)
Software testing function testing a full set of common interview questions [function testing - zero foundation] essential 4-1

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

【ACL2020】一种新颖的成分句法树序列化方法

8 kinds of visual transformer finishing (Part 2)

NPM install error forced installation

pollFirst(),pollLast(),peekFirst(),peekLast()
随机推荐
易语言编程: 让读屏软件可获取标签控件的文本
NPM install error forced installation
Tensorflow package tf.keras module construction and training deep learning model
STL container -- Application of set set
Is the operation of assigning values to int variables atomic?
Deep understanding of Kalman filter (3): multidimensional Kalman filter
Detailed explanation of two methods of Sqlalchemy
DNS domain name space
Principle of flex:1
QDoubleValidator不生效问题解决办法
ES6 new - deconstruction assignment of array / object
Replace restricts the text box to regular expressions of numbers, numbers, letters, etc
Flex layout (actual Xiaomi official website)
D3.v3.js data visualization -- pictures and tips of force oriented diagram
[C language - zero foundation lesson 10] adventure of array Kingdom
ArkUI框架中的两个小技巧
js call和apply
音乐体验天花板!14个网易云音乐的情感化设计细节
Hard core structure, violent interpretation
5g failed to stimulate the development of the industry, which disappointed not only operators, but also mobile phone enterprises