c 语言学习笔记01
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
1. c 语言
1.1.1.helloworld
文件名:test_pointer.c
文件内容:
#include <stdio.h>
#include <malloc.h>
int main()
{
printf("hello world\n\n");
return 0;
}
编译加运行:
[root@localhost c_study]# gcc test_pointer.c ;./a.out
hello world
1.1.
2.获取键盘输入
文件名:getKeyboardInptStr.c
文件内容:
#include <stdio.h>
#include <malloc.h> int main()
{
char *a= malloc(sizeof(char *));
char *b= malloc(sizeof(char *));
scanf("%s",a);
scanf("%s",b);
printf("%s \t%s\n",a,b);
free(a);
free(b);
return 0;
}
编译:
gcc getKeyboardInptStr.c -o hello.exe
运行
[root@localhost tcp_ip]# ./hello.exe abc
ddd
abc ddd
[root@localhost tcp_ip]#
1.1.
2.1.从键盘输入构造数组
文件名keyboard_input.c
文件内容:
#include <stdio.h>
#include <malloc.h>
int main()
{
char * strs[4];
char *a;
int i;
for(i=0;i<4;i++){
a= malloc(sizeof(char *));
scanf("%s",a);
strs[i]=a;
//printf("%s \t\n",a);
//free(a);
}
printf("=====================================\n");
for(i=0;i<4;i++){
printf("%s \t\n",strs[i]);
}
return 0;
}
1.1.3.读取文件
读取文件有两个操作:打开流(fopen)和关闭流(fclose)#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
int main(int ac,char**av)
{
int exit_status=EXIT_SUCCESS;
FILE *input;
while( *++av!=NULL)
{
input=fopen(*av,"r");
if(input==NULL)
{
perror(*av);
exit_status=EXIT_FAILURE;
continue;
}
if(fclose(input)!=0)
{
perror("fclose");
exit(EXIT_FAILURE);
}
}
return exit_status;
}
1.1.3.1.打开文件流
1.1.3.1.1.ungetc 用法
文件名:ungetc_test.c
文件内容:
#include<stdio.h>
#include<ctype.h>
int main()
{
int value;
int ch;
value=0;
while((ch=getchar())!=EOF && isdigit(ch))
{
value=value*10;
value+=ch-'0';
}
//ungetc(ch,stdin);
printf("%d\n",value);
ch=getchar();
printf("last char is %c\n",ch);
return value;
}
若注释掉红色部分:
[root@localhost read_file]# gcc ungetc_test.c;echo 234abc|./a.out 234
last char is b
否则:
[root@localhost read_file]# gcc ungetc_test.c;echo 234abc|./a.out 234
last char is a
1.1.3.
2.关闭流
1.1.3.3.读取文本文件
文件名称:read_file03.c
文件内容:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
int main()
{
int value;
int ch;
char *input_file="ab.txt";
FILE *input;
input=fopen(input_file,"r");
while((ch=fgetc(input))!=EOF)
{
printf("%c",ch);
}
return 0;
}
1.2.指针
1.2.1.取地址和取值
取地址:&
取值:*
1.2.1.1.取地址
int age=23;
int *age_adrr=&age;
1.2.1.2.取值
参阅取地址。
printf("My age is \t%d\n",*age_adrr);
1.2.1.3.范例01:
文件名:test_pointer.c
#include <stdio.h>
#include <malloc.h>
int main()
{
int age=23;
int *age_adrr=&age;
printf("My age is \t\t%d\n",age);
printf("My age address is \t%d\n",age_adrr);
printf("My age is \t\t%d\n",*age_adrr);
//printf("hello world\n\n");
return 0;
}
运行:
[root@localhost c_study]# gcc test_pointer.c ;./a.out
My age is 23
My age address is -1074593988
My age is 23
[root@localhost c_study]#
1.2.2.左值和右值
1.2.3.字符串操作
1.2.3.1.split
#include <string.h>
#include <stdio.h>
#include <malloc.h>
char** StringSplit(char* string, char* split) {
char** result;
/*First assign a char * of memory, and then dynamicly allocate the rest of the memory*/ result = (char **) malloc(sizeof(char *));
memset(result, 0, sizeof(char *));
/*Define a pointer for ergodic and a pointer for searching for position*/
char* p = string;
char* pos = string;
/*No matter whether there is the division string, it will be split into a string definitely*/ int count = 0;
while (*p != '\0') {
char* temp;
char* tt;
/*Find the string*/
pos = strstr(p, split);
/*result is zero indicate that there is not this character in the rest of string*/
if (pos == 0) {
result = (char **) realloc(result, sizeof(char *) * (count + 2));
//result[0] = count;
result[count] = p;
result[count + 1] = NULL;
return result;
}
temp = (char *) malloc(sizeof(char) * (pos - p + 1));
memset(temp, 0, sizeof(char) * (pos - p + 1));
/*set head pointer ,in order to use for assignment */
tt = temp;
while (p <= pos) {
*temp++ = *p++;
}
*--temp = '\0';
result = (char * *) realloc(result, sizeof(char *) * (count + 1));
//result[0] = count;
result[count] = tt;
count++;
/*Set pointer for the next ergodic duration (important). When split length is more than 1, don't set will be more assignment unnecessary string*/
p += strlen(split) - 1;
}
return result;
}
int main() {
char **nameArr = StringSplit("set work_mem to 2048;", " ");
// char *name = StringSplit("set work_mem to 2048;", " ")[0];
printf("%s----\n", nameArr[0]);
}
运行结果:
[root@localhost split2]# gcc split_test02.c ;./a.out
set----
1.2.3.2.求char** 的长度
1.3.链表
文件名称:struct_test.c
文件内容:
#include <stdio.h>
#include <malloc.h>
typedef struct {
int age;
char *name;
} Student;
int main()
{
Student *student=(Student *)malloc(sizeof(Student));
student->age=3;
char *name_tmp="huangwei";
student->name=name_tmp;
printf("My age is %d\n",student->age);
printf("My name is %s\n",student->name);
return 0;
}
运行:
[root@localhost c_study]# gcc struct_test.c ;./a.out;rm -f a.out My age is 3
My name is huangwei
1.3.1.1.链表定义的两种种方式
方式一:
struct Node{
int age;
char *name;
struct Node *next;
} Student;
方式二:
typedef struct Node{
int age;
char* name;
struct Node* next;
} Student;
文件名:struct_test03
内容:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
struct Node{
int age;
char *name;
struct Node *next;
} Student;
int main()
{
struct Node *student1=(struct Node *)malloc(sizeof(Student));
struct Node *student2=(struct Node *)malloc(sizeof(Student));
student2->name="jian";
student2->age=22;
student1->age=3;
char *name_tmp="huangwei";
student1->name=name_tmp;
student1->next=student2;
printf("%s age is %d\n",student1->name,student1->age);
printf("%s is %d\n",student1->next->name,student1->next->age);
return 0;
}
运行:
[root@localhost c_study]# gcc struct_test03.c ;./a.out
huangwei age is 3
jian is 22
1.3.1.
2.求链表的长度
int length_p(Student* p)
{
if (p==NULL)
{
return 0;
}
int leng_l=0;
while(p->next!=NULL){
leng_l++;
p=p->next;
}
if(p!=NULL)
{
leng_l++;
}
return leng_l;
}
1.4.预处理语句
预处理命令
#include<stdio.h>
#define PI 3.14
int main()
{
printf("PI is %f\n",PI);
return 0;
}
运行结果:
[root@localhost define3]# gcc define_test01.c ;./a.out PI is 3.140000
#include<stdio.h>
#define PI 3.14
#define SUM(x,y) x+y
#define CHENG(X,Y) (X)*(Y)
int main()
{
printf("PI is %f\n",PI);
printf("The sum is %d\n",SUM(3,4));
printf("The cheng is %d\n",CHENG(4,2));
return 0;
}
运行结果:
[root@localhost define3]# gcc define_test01.c ;./a.out PI is 3.140000
The sum is 7
The cheng is 8
#include<stdio.h>
#define NAME "whuang"
int main()
{
#undef NAME
printf("My name is %s\n",NAME);
return 0;
}
运行结果:
会报错:
[root@localhost define3]# gcc define_test02.c ;./a.out
define_test02.c: In function ‘main’:
define_test02.c:6: 错误:‘NAME’未声明(在此函数内第一次使用) 修改为:
#include<stdio.h>
#define NAME "whuang"
int main()
{
printf("My name is %s\n",NAME);
#undef NAME
return 0;
}
就OK 了
1.4.1.ifdef
#include<stdio.h>
#define NAME "whuang"
int main()
{
printf("My name is %s\n",NAME);
#ifdef NAME
printf("def NAME\n");
#endif
return 0;
}
运行结果:
[root@localhost define3]# gcc define_test03.c ;./a.out My name is whuang
def NAME
1.4.
2.else
#include<stdio.h>
#define NAME "whuang"
int main()
{
printf("My name is %s\n",NAME);
#undef NAME
#ifdef NAME
printf("def NAME\n");
#else
printf("NOT def NAME\n");
#endif
return 0;
}
运行结果:
[root@localhost define3]# gcc define_test03.c ;./a.out My name is whuang
NOT def NAME
1.4.3.ifndef
#include<stdio.h>
#define NAME "whuang"
int main()
{
printf("My name is %s\n",NAME);
#undef NAME
#ifndef NAME
printf("NOT def NAME\n");
#else
printf("def NAME\n");
#endif
return 0;
}
运行结果:
[root@localhost define3]# gcc define_test04_ifndef.c ;./a.out My name is whuang
NOT def NAME
1.5.经典摘录
1,读取文件时,返回的为什么是int,而不是char?
2,xx
1.6.常见错误
1.6.1.在C99 模式之外使用‘for’循环初始化声明
报错信息如下:
[root@localhost read_file]# gcc keyboard_input.c
keyboard_input.c: In function ‘main’:
keyboard_input.c:7: 错误:在C99 模式之外使用‘for’循环初始化声明
1.7.内存对齐
文件名:memery_test01.c
文件内容:
#include <stdio.h>
struct Test1 {
char c1;
char c2;
short s;
int i;
};
struct Test2 {
char c1;
short s;
char c2;
int i;
};
int main()
{
struct Test1 a;
struct Test2 b;
printf("%d\n", sizeof(a));
printf("%d\n", sizeof(b));
return 0;
}
运行结果:
[root@localhost memery3]# gcc memery_test01.c ;./a.out
8
12
说明:两个结构体,其中是相同的数据类型,只不过顺序不同,但是占用的空间却不同。