apple   
 博客 
 控制 
 选项 
 关于 
icons



C Programing Language
Published on Tue Nov 12 2019 10:13:00 GMT+0800

This is a note about The C programing language .

I’m refactoring this note into English, too.

C Basic Knowledge

C variable , data types

C语言中存在已经定义好的数种数据类型,每种数据类型都有其可表示值的范围.

接下来要背书了,学个计算机有时候就是和学高中语文一样难受.

下表列出了关于标准整数类型的存储大小和值范围的细节:

类型 存储大小 值范围
char 1 字节 -128 到 127 或 0 到 255
unsigned char 1 字节 0 到 255
signed char 1 字节 -128 到 127
int 2 或 4 字节 -32,768 到 32,767 或 -2,147,483,648 到 2,147,483,647 依环境而定
unsigned int 2 或 4 字节 0 到 65,535 或 0 到 4,294,967,295 依环境而定
short 2 字节 -32,768 到 32,767
unsigned short 2 字节 0 到 65,535
long 4 字节 -2,147,483,648 到 2,147,483,647
unsigned long 4 字节 0 到 4,294,967,295

下表列出了关于标准浮点类型的存储大小、值范围和精度的细节:

类型 存储大小 值范围 精度
float 4 字节 1.2E-38 到 3.4E+38 6 位小数
double 8 字节 2.3E-308 到 1.7E+308 15 位小数
long double 16 字节 3.4E-4932 到 1.1E+4932 19 位小数

Functions of Input and Output

scanf

scanf是格式化输入的函数,可以将用户从键盘输入的内容写到一个地址中去.scanf的使用格式是这样的:

scanf(“%d”,&a);

%d代表输入的是一个整型变量,相类似的还有%f(浮点型变量),%lf(长浮点型变量),%c(字符型变量),%s(字符串).

File R/W

1
2
3
4
5
6
7
8
9
10
11
12
#include<stdio.h>
int main()
{
//fopen
FILE *p = fopen("filename","r");
char str[100] = {0};
fscanf(p,"%s",str);

//freopen
freopen("filename","r",stdin);
scanf("%s",str);
}

Preprocessing command

Preprocessing command normally be placed before the start of function definition. It is used to process something before compile .

Macro definition

Macro definition normally starts with # , such as:

1
2
#define
#include

The rules of using macro definition:

1
2
3
4
5
6
7
8
#define PI 3.1415926//不含参数的构造,语句结束不需要分号,#include亦然
//编译器会直接将所有的PI替换为3.1415926
#define R 3.0
#define L 2*PI*R
//以上的两行也是可以的,宏定义会展开.
printf("L");//格式控制输出时不置反

#define S(a,b) a*b//含参构造,遇到S(数字,数字),替换为a*b

宏名一般使用大写字母表示,这是一种约定俗成的规则.

宏定义不会为标识符分配内存空间

宏定义的终止:

1
#undefine PI

File includes

1
2
#include<file name>
#include"file name"//偏向于用户自定义部分

Conditional Compile

1
2
3
4
5
6
7
#ifdef //flag
#define LETTER 1

#if LETTER
//code
#else
//code

Pointer

Pointer is a sort of variable that storages address.

Here are some basic operations of using pointer.

1
2
3
4
int a=3;
int *ptr = &a;//ptr is a variable that points to the address of a
*ptr;//use * to access the data at the address which ptr points to.
*(ptr++);//returns *ptr,and then ptr++

Use pointer to access array:

1
2
3
int a[]={1,2,3,5,2,6,2,5}
int *ptr = &a;//the address of an array is just same as the address of the first element of the array.
printf("%d",*ptr++); //the ptr++ refers the pointer is going to point to the next element of the array

Use pointer to access multi dimension array;

Array Use variable Use pointer Use pointer Use pointer
a[i] a[i] *(a+1) *(a+1) *(a+i)
a[i][j] a[i][j] (\(a+i)+j) *(a[i]+j) *(&a[i][0]+j)

An address like a (the array is a[3][3]) can’t be a value of int *p;

So,how to do that?

Use int (*p)[4] , a pointer of this type can have a value like a (the array is a[2][4]).

Pointer Array

1
2
int *p[4];//a define of a pointer array
char *p[4]={"Brazil","Russia","India","China"};//a array of pointer,each points to a string.

Sort a char *p[]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
char *p[]={"Aa","Bb","Cc","Dd"};
for(int i=0;i<4;i++)
{
int pos =i;
char max = *p[i];
for(int j=i;j<4;j++)
{
if(*p[j]>max)
{
max=*p[j];
pos=j;
}
}
char* t= p[i];
p[i]=p[pos];
p[pos]=t;
}


return 0;
}

Dynamic Memory Space

1
2
3
4
int *p = (int*)malloc(4);; //set a space whose size is 4.
int *p[4] = (int*)calloc(n,sizeof(int));//set n*size array.
realloc(p,8);//resize.
free(p);//delete.

Pointer to Pointer

1
2
3
4
int **p;
**p=2;
*p;//an address
**p;//a value

Parameters to main

1
2
3
4
5
6
7
8
9
10
11
12
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main(int argc,char* argv[])//argc refers string numbers and argv refers string that put in.
//argv[0]=your program name
{
for(int i=1;argv[i]!=NULL;i++)
printf("%s\n",argv[i]);


return 0;
}

Dynamic Memory space

Malloc

1
2
3
4
5
int main()
{
struct Student *head;
head = (struct Student *)malloc(sizeof(struct Student));
}

Calloc

1
2
3
4
int main()
{
head = (struct Student*)calloc(100,sizeof(struct Student))
}

Realloc

1
2
3
4
int main()
{
realloc(*p,size);
}

Free

1
2
3
4
int main()
{
free(*p);
}

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include<stdio.h>
#include<stdlib.h>

struct Student
{

int age=0;
struct Student *next=NULL;
};

int main()
{
struct Student *head,*current;
int i=0;
head = (struct Student*)malloc(sizeof(struct Student));
current=head;
while(i==0)
{
printf("Add student? 0/yes 1/no\n");
scanf("%d",&i);
if(i==0)
{
printf("input age\n");
scanf("%d",&(*current).age);
(*current).next=(struct Student*)malloc(sizeof(struct Student));
current = (*current).next;
}else
{
(*current).age=0;
(*current).next=NULL;
}

}
return 0;
}

File operation

File pointer

1
2
3
FILE *fp;//create a pointer point to the head of the file.
fp = fopen("file.txt","a+");//open the file.
fclose(fp);

File functions

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//freopen
freopen("File","a+",stdin);
freopen("File","w",stdout);
fclose(ptr);//or the file will not be write.

fgetc(fp);
fputc(ch,fp);
//read and write character in a file.

fgets(str,n,fp);//See carefully, there's n in parameter list, n refers the string's length is n-1 .
fputs(str,fp);

fscanf(fp,"Format",parameter list);
fprintf(fp,"Format",parameter list);

//read and write in bit
fread(buffer,size,count,fp);
fwrite(buffer,size,count,fp);

//examples:
fread(&stu_list[i],sizeof(struct student),1,fp);
fwrite(&stu_list[i],sizeof(struct student),1,fp);//you can see it as using disk as memory.

Radom R/W files

1
2
3
4
rewind(fp);//move the file flag back to the head.
fseek(fp,100L,0);//move the flag to the position 100 byte away from the head.
fseek(fp,100L,1);//move foward 100 byte from current position
fseek(fp,-50L,3);//move back 50 byte from the end.
start position name number reference
head SEEK_SET 0
current position SEEK_CUR 1
end SEEK_END 2