Article^ Parent
C 笔记
Date: | 2018-12-18 08:00:00 |
Description: | C 语言笔记。 |
Keywords: | C, 指针, 编译, GCC |
Category: | engineering_technology/computer/code_interpreter |
Tag: | c |
Link: | https://www.diewuxi.com/blog/article/63.html |
Get thing done
simple example
编写
File: hello_world.c
#include <stdio.h>
int main()
{
printf("Hello World\n");
return 0;
}
编译
gcc -o hello_world hello_world.c
/<somepath>/
hello_world.c
hello_world
运行
./hello_world
advance example
编写
directory structure:
/<somepath>/
hello_world.c
/<headerpath1>/
stdio.h
/<headerpath2>/
tool.h
/<libpath1>/
stdio.so
/<libpath2>/
tool.so
File stdio.h
:
...
void printf(char * [] string);
...
File tool.h
:
...
int extrude(int time, int speed);
int retract(int time, int speed);
...
File hello_world.c
:
#include <stdio.h>
#include "tool.h"
int main()
{
int time = 3;
int speed = 60;
printf("Hello World\n");
extrude(time, speed);
retract(time, speed);
return 0;
}
编译
gcc -I /<headerpath2> -l <libpath2>/tool.so -o hello_world -x c hello_world.c
/<somepath>/
hello_world.c
hello_world
运行
./hello_world
编译过程
| Stage | file | command |
| ----------- | ------------------- | -------------------- |
| preprocessing | `<file>.c -> <file>.i` | `gcc -E -o <file>.i -x c <file>.c` |
| compilation | `<file>.i -> <file>.s` | `gcc -S -o <file>.s -x cpp-outpt <file>.i` |
| assembly | `<file>.s -> <file>.o` | `gcc -c -o <file>.s -x assembler <file>.s` |
| linking | `<file>.o -> <file> exe` | `gcc -o <file> <file>.o |
Shortcut command:
| process | file | command |
| ---------- | ------------------------- | --------------------------------- |
| .c to exe | `<file>.c -> <file> exe` | `gcc -o <file> -x c <file>.c` |
| .c to .s | `<file>.c -> <file>.s` | `gcc -o <file>.s -x c <file>.c` |
| .c to .o | `<file>.c -> <file> exe` | `gcc -o <file>.o -x c <file>.c` |
Option explanation:
-x <language>
source language type
-o <file>
output file name
-c
Compile to object file. .c/.i/.s -> .o
-S
Compile to assembler code. .c/.i -> .s
-E
Compile to preprocessed code. .c -> .i
使用库
源文件可分成多个。
其中一个源文件包含 main 函数,是主程序。
其它每个源文件各包含一些函数,把这些函数的声明部分从源文件移动出来放到 <file>.h 文件中。
主程序源文件中使用 #include "<file>.h",这样可以在主源文件中直接使用那些函数。
#include <<file>.h> // 引用系统头文件
#include "<file>.h" // 引用用户头文件
-l <library>
Add search library which is file .a or .so.
The linker searches a standard list of directories for the library.
-s
Remove all symbol table and relocation information from the executable
-I <dir>
Add the directory dir to the list of directories to be searched for header files dur-ing preprocessing.
语言特点
* #include 使用函数库
* #define 符号定义
* 变量,流程控制结构,函数
* 变量声明,先声明后使用
* 变量作用域
* 函数声明,函数参数值传递
* 从 main 函数开始运行
* 注释 // 和 /* */
* 空白
数据类型
基本类型
| type | size | value |
| ---- | -------- | ------------------------------------ |
| char | 1 B | -128~127 / 0~255 |
| int | 2 B / 4 B | -32768~32767 / -2147483648~2147483647 |
| float | 4 B | 1.2e-38~3.4e+38 6 bits |
| double | 8 B | 1.3e-308~1.7e+308 15 bits |
修饰符限定类型
| type | size | value |
| --------------- | -------- | ----------------------------- |
| signed char | 1 B | -128~127 |
| unsigned char | 1 B | 0~255 |
| short int | 2 B | -32768~32767 |
| long int | 4 B | -2147483648~2147483647 |
| signed int | | |
| signed short int | | |
| signed long int | | |
| unsigned int | 2 B / 4 B | 0~65535 / 0~4294976295 |
| unsigned short int | 2 B | 0~65535 |
| unsigned long int | 4 B | 0~4294976295 |
| long double | 16 B | 3.4e-4932~1.1e+4932 19 bits |
常量
| type | example |
| ------------------------- | --------- |
| int type | 123 |
| long int type | 123L |
| unsigned int type | 123U |
| unsigned long int type | 123UL |
| float | 123.4F, 1e-2F |
| double | 123.4, 1e-2 |
| long double | 123.4L, 1e-2L |
| char | 'a', '0' |
| string | "hello world!" |
| enum | |
指针
声明指向数组的指针:
char * a[10]
char * a[0]
char * a[1]
...
char * a[9]
char * * a;
* a
a
char a[10];
char a0
char a1
...
char a9
char * * a[10];
int * * a;
property of point variable:
------------
| name |
-----------
| type |
-----------
| content |
------------
relation:
int a;
Memery Addr
|
+-------------+-- &a
| |
| a |
| |
+-------------+
|
...
|
V
int * a;
Memery Addr
|
+-------------+-- &a
| |
| a |
| |
+-------------+
|
...
|
+-------------+-- a
| |
| * a |
| |
+-------------+
|
...
V
int * * a;
Memery Addr
|
+-------------+-- &a
| |
| a |
| |
+-------------+
|
...
|
+-------------+-- a
| |
| * a |
| |
+-------------+
|
...
|
+-------------+-- * a
| |
| * * a |
| |
+-------------+
|
...
V
自动类型转换
当运算符有不同数据类型的操作数时,按照规则进行转换。 规则只有一个,在不丢失信息的条件下把范围小的操作数转换成范围大的操作数。
赋值运算符时,右侧结果转换成左侧变量的类型,这时转换结果可能会与原结果不同。
参数传递给函数时,也会发生自动类型转换。
强制类型转换
(type-name) expression
expression 结果转换成 type-name 类型的。
变量
访问控制
- const 变量限定符,变量值不能修改
赋值运算符
i op= j
把 i 与 j 进行运算,然后结果存储回 i 变量。 op 为:
+ - * / % << >> & ^ |
可以理解为:
i = i op j
C 标准
C89/C90:
- ANSI C(X3.159-1989): 1989--1990
- ISO/IEC 9899:1990: 1990
C99: ISO/IEC 9899:1999 1999
- C11: ISO/IEC 9899:2011 2011
C17: ISO/IEC 9899:2018 2017--2018
Standard option C89/90 -std=c90 | -std=iso9899:1990 C99 -std=c99 | -std=iso9899:1999 C11 -std=c11 | -std=iso9899:2011 C17 -std=c17 | -std=iso9899:2018 Standard option C89/90 with extension -std=gnu90 C99 with extension -std=gnu99 C11 with extension -std=gnu11 默认,gcc 使用扩展
数学运算
浮点数
浮点数只能表示实数的有限子集,即具有终止二进制展开的有理数,且短于尾数的宽度。 即使是简单的分数,如 1/5,也只能用浮点数近似表示。 这个子集对于大多数目的来说已经足够大。
浮点计算以额外的精度在内部执行,然后四舍五入以适合目标类型。
- Round to nearest(default)
- Round toward plus Infinity
- Round toward minus Infinity
- Round toward zero
Arithmetic Operators
二元运算:
+
-
*
/ 除;操作数为负数时,截断方向依赖机器
% 求余数;整除时余数为 0,不能用于 float 和 double 型,操作数是负数时,余数符号依赖机器
C 标准库 math.h
标准函数库 <math.h>
。所有功能都接收 double 参数并返回 double 结果
数学常量:
M_E The base of natural logarithms.
M_LOG2E The logarithm to base 2 of M_E.
M_LOG10E The logarithm to base 10 of M_E.
M_LN2 The natural logarithm of 2.
M_LN10 The natural logarithm of 10.
M_PI Pi, the ratio of a circle’s circumference to its diameter.
M_PI_2 Pi divided by two.
M_PI_4 Pi divided by four.
M_1_PI The reciprocal of pi (1/pi)
M_2_PI Two times the reciprocal of pi.
M_2_SQRTPI Two times the reciprocal of the square root of pi.
M_SQRT2 The square root of two.
M_SQRT1_2 The reciprocal of the square root of two (also the square root of 1/2).
函数:
double ciel (double x)
round x upwards to the nearest integer, returning that value as a double
double floor (double x)
round x downwards to the nearest integer, returning that value as a double
Changelog
* 2021-10-24
* Modify: Pointer, int a, int * a, int * * a;
* 2020-07-29
* Add 浮点数,math.h,arithmetic operator,类型转换
* 2020-07-28
* Add 编译过程,语言特点,数据类型,变量,C 标准,赋值运算符,使用库
* Add simple example incompleted
* Add advance example incompleted
* 2018-12-18
* Add 指针理解
Last modified: 2021-10-24
Comments [0]
There is no comments now.