C语言基础

1.宏定义
<1>.常量定义

#define MATH_PI 3.14

void defineH() {
    // 宏在编译阶段就把宏对应的常量给替换了,所以很快;
    printf("%f", MATH_PI);
}

<2>.方法定义

// 定义宏方法,宏方法没有具体的返回类型;当多行的时候,在后面添加一个反斜杠;
#define MAX(A,B) {\
     A>B?A:B \
}

调用:MAX(12,30);

2.C语言中的指针

指针实际上就是对变量物理内存的引用,在程序中,会对每一个变量分配一块内存,这块内存在自己的地址,地址是唯一的,通过指针就可以访问到这块内存,可以读取,修改这块内存上的变量对应的值;下边来看例子;

    int a = 4;
    int *p;
    p = &a;
    printf("%d\n",p); // 地址值 
    printf("%d\n",*p); //数值>>4
    *p = 14;
    printf("%d\n",a); //数值发生改变>>14
    
    int arr[] = {1,2,3,4,5,6,7,8,9};
    p = arr; // p指向arr(实际上是指向了arr数组的第一个数值)
    printf("%d\n",*p); //>>1
    printf("%d\n",*(p+4)); // p+4,p往后移动>>4

    p = &arr[1];
    p = p+1;
    printf("%d\n",*p); //>>3

3.结构体
(1).结构体定义

struct People {
    int age;
    char *name;
};

使用:

    struct People p;
    p.age = 12;
    printf("%d", p.age);

(2).结构体指针

/**
 * 需要引入stdlib.h
 */
void structpointer() {
    // 声明一个People指针(需要初始化)并且分配一个内存地址,内存大小为结构体People的大小
    struct People *p = malloc(sizeof(struct People));
    p->age = 20;
    p->name = "ls";
    struct People *p1 = p;
    p1->name = "ls1";
    printf("hello struct People %s", p->name); // 输出结果为ls1
    // 使用malloc分配的指针在不需要使用之后需要释放
    free(p);
}

(3).结构体与联合体

typedef struct  _Player
{
    int age;
    int job;
    union 
    {
        int classId;
        char officeAddress[20];
    }jobInfo;
}Player; 

  上面的关键字union就在Player中定义了一个联合体,那么联合体有什么用呢?联合体往往用于保存某一个特定的属性,可能有的对象有这个属性而有的对象没有,比如上面这个例子中的job属性,如果是学生就没有officeAddress,而对应的有班级编号classId,而其它工作者正好相反;那这样定义有什么好处呢?union代码块中定义的属性,程序在运行的时候分配的内存大小为union中的属性中需要内存最大的那个属性所占用的内存大小;在上面的例子中,union分配的内存大小即为20,而不是classId和officeAddress两者的和24;所以,很明显,如果union中有多个不同属性,则可以显著的减小内存的开支;

4.函数指针

/**
 * 函数指针
 */
void functionpointer(int i) {
    printf("hello functionpointer %d \n", i);
}
int functionpointer1() { printf("hello functionpointer1 \n"); return 0; }
void functionpointerexe() { void (*p)(int); p = functionpointer; p(34); int (*a)(); a = functionpointer1; a(); }

5.typedef关键字
<1>.定义结构体

typedef struct {
    int age;
} P1;

通过typedef定义之后,在申明P1 struct的时候就不再需要在前面加struct关键字了;

    P1 p;
    p.age = 12;
    printf("%d", p.age);

<2>.定义函数指针

typedef int (*Void)();
Void v = functionpointer1;
v(); // 输出functionpointer1

6.引入自定义头文件

hello.h

// 如果没有定义HELLO_H_,则定义HELLO_H_,是为了防止头文件重复;
#ifndef HELLO_H_
#define HELLO_H_

void sayHello();

#endif /* HELLO_H_ */

hello.c

#include <stdio.h>
// hello.h中sayHello具体实现
void sayHello() {
    printf("hello c");
}

main.c

#include "hello.h"

int main(int argc, const char * argv[]) {
    sayHello();
}

7.文件操作

read:

void fileRead1() {
    FILE * f = fopen("xx.txt", "r");
    fseek(f, 0, SEEK_END); // seek到文件末尾:获取文件大小
    long size = ftell(f); // 返回文件大小
    char buf[size + 1];
    fseek(f, 0, SEEK_SET); // 重新回到文件开始
    fread(buf, sizeof(unsigned char), size, f);
    buf[size] = '\0';
    fclose(f);
    printf("%s", buf);
}

write:

void fileWrite1() {
    // f可能为空,需要进行判断!
    FILE * f = fopen("xx.txt", "w");
    fprintf(f, "hello c");
    fclose(f);
}

8.其他

<1>.生成随机数:

    srand((int) time(NULL ));
    int randNum = rand();
    printf("%d,", randNum);
    return 0;

<2>.字符串操作:

    char buf[100];
    memset(buf, 0, 100); // 
    sprintf(buf, "hello c %d s%f,", 12, 23.2);
    printf("%s", buf);

<3>.输入输出:

void getput() {
    char buf[100];
    gets(buf); // 等待用户输入
    puts(buf);
}

int scanprint() {
    int a;
    scanf("%d", &a);
    printf("%d", a);
    return 0;
}

 

posted @ 2014-05-10 19:03  Livingstone  阅读(365)  评论(0编辑  收藏  举报