Chinaunix首页 | 论坛 | 博客
  • 博客访问: 50560
  • 博文数量: 34
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 297
  • 用 户 组: 普通用户
  • 注册时间: 2014-04-09 10:52
文章分类

全部博文(34)

文章存档

2015年(23)

2014年(11)

我的朋友

分类: C/C++

2015-04-28 10:38:31

一.宏定义

#ifndef
#endif
为了防止头文件重复包含

#ifndef  xxx
#define yyy
zzz

#endif   

如果xxx没被定义,就执行#define yyy 和 zzz

点击(此处)折叠或打开

  1. //
  2. // Header.h
  3. // C语言
  4. //
  5. // Created by 金海洋 on 15-4-28.
  6. // Copyright (c) 2015年 金海洋. All rights reserved.
  7. //

  8. #ifndef C___Header_h
  9. #define C___Header_h


  10. #endif


#if 0
#endif
常用于注释

#if x
yyy

#endif

如果x为真,就执行yyy,否则就跳过yyy

点击(此处)折叠或打开

  1. //
  2. // main.c
  3. // C语言
  4. //
  5. // Created by 金海洋 on 15-4-28.
  6. // Copyright (c) 2015年 金海洋. All rights reserved.
  7. //

  8. #include <stdio.h>

  9. int main(int argc, const char * argv[]) {
  10.     
  11.     
  12. #if 0
  13.     printf("Hello, World!\n");
  14. #endif
  15.     
  16.     return 0;
  17. }

二.枚举

枚举是被命名的整数集合,可读性非常高

点击(此处)折叠或打开

  1. //
  2. // main.c
  3. // C语言
  4. //
  5. // Created by 金海洋 on 15-4-28.
  6. // Copyright (c) 2015年 金海洋. All rights reserved.
  7. //

  8. #include <stdio.h>

  9. //定义枚举类型
  10. enum en {

  11.     yi = 1,
  12.     er,
  13.     san

  14. };

  15. int main(int argc, const char * argv[]) {
  16.     
  17.     //枚举的使用
  18.     enum en a = san;
  19.     printf("%d--%d\n",yi,a);

  20.     
  21.     return 0;
  22. }

三.static

静态的,存储在静态存储区


别的文件访问不了该函数,只有在本文件里可以使用该函数

点击(此处)折叠或打开

  1. //
  2. // Stu.c
  3. // C语言
  4. //
  5. // Created by 金海洋 on 15-4-28.
  6. // Copyright (c) 2015年 金海洋. All rights reserved.
  7. //

  8. #include "Stu.h"

  9. void static fun(void){
  10.     
  11.     
  12. }

只有本函数内才可以使用该变量a
并且默认初始化为0,变量a仅处在一份儿

点击(此处)折叠或打开

  1. //
  2. // Stu.c
  3. // C语言
  4. //
  5. // Created by 金海洋 on 15-4-28.
  6. // Copyright (c) 2015年 金海洋. All rights reserved.
  7. //

  8. #include "Stu.h"

  9. void fun(void){
  10.     
  11.     static int a;

  12.     printf("%d\n",a++);
  13.     
  14. }

点击(此处)折叠或打开

  1. //
  2. // main.c
  3. // C语言
  4. //
  5. // Created by 金海洋 on 15-4-28.
  6. // Copyright (c) 2015年 金海洋. All rights reserved.
  7. //

  8. #include <stdio.h>

  9. #include "Stu.h"

  10. int main(int argc, const char * argv[]) {
  11.     
  12.     int i = 0;
  13.     
  14.     for(i=0;i<5;i++)
  15.         fun();
  16.     
  17.     
  18.     return 0;
  19. }
输出结果:

点击(此处)折叠或打开

  1. 0
  2. 1
  3. 2
  4. 3
  5. 4


只有该文件内可以使用变量b

点击(此处)折叠或打开

  1. //
  2. // Stu.c
  3. // C语言
  4. //
  5. // Created by 金海洋 on 15-4-28.
  6. // Copyright (c) 2015年 金海洋. All rights reserved.
  7. //

  8. #include "Stu.h"

  9. static int b;

  10. void fun(void){
  11.     
  12.     static int a;

  13.     printf("%d\n",a++);
  14.     
  15. }

四.const

被修饰的变量是只读的

点击(此处)折叠或打开

  1. //
  2. // main.c
  3. // C语言
  4. //
  5. // Created by 金海洋 on 15-4-28.
  6. // Copyright (c) 2015年 金海洋. All rights reserved.
  7. //

  8. #include <stdio.h>

  9. #include "Stu.h"

  10. int main(int argc, const char * argv[]) {
  11.     
  12.     int i=11;
  13.     
  14.     int const a = 0;
  15.     //不可以
  16.     //a=1;
  17.     
  18.     int const *= &a;
  19.     //不可以
  20.     //*= 22;
  21.     //可以
  22.     b = &i;
  23.     
  24.     int *const c = &a;
  25.     //不可以
  26.     //c = &i;
  27.     //可以
  28.     *= 33;
  29.     
  30.     return 0;
  31. }

五.函数指针


点击(此处)折叠或打开

  1. //
  2. // Stu.c
  3. // C语言
  4. //
  5. // Created by 金海洋 on 15-4-28.
  6. // Copyright (c) 2015年 金海洋. All rights reserved.
  7. //

  8. #include "Stu.h"


  9. void fun(int c){
  10.     
  11.     printf("%d\n",c);
  12.     
  13. }


点击(此处)折叠或打开

  1. //
  2. // main.c
  3. // C语言
  4. //
  5. // Created by 金海洋 on 15-4-28.
  6. // Copyright (c) 2015年 金海洋. All rights reserved.
  7. //

  8. #include <stdio.h>

  9. #include "Stu.h"

  10. int main(int argc, const char * argv[]) {
  11.     
  12.     //声明函数指针
  13.     int (*f)(int);
  14.     //赋值
  15.     f = fun;
  16.     //f = &fun;
  17.     //使用
  18.     f(33);
  19.     (*f)(111);
  20.     
  21.     return 0;
  22. }
阅读(631) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~