GCC C语言特性

GCC C特性
在C语言的编程过程中,适当使用gcc的C语言特性,可以提高我们的编程效率,代码也更整洁,更清晰。
比如语句表达式,可以把一块语句当作一个表达式,返回最后的语句,个人常使用于宏的定义,感觉比用do{ }while(0)表示一块语句更直观,而且定义的宏还能当作表达式赋值给变量。
还有定义局部变量时,加上__cleanup__属性,当局部变量退出作用域时会自动调用指定cleanup函数,这样一来,我们可以对内存指针添加释放内存函数,让它自动调用free释放内存,当一个函数内,存在多处返回时不需要多处调用free,对多线程加锁场景,巧妙使用__cleanup__特性,也可以让锁自动进行解锁操作,不用担心忘记解锁而导致死锁。对__cleanup__特性的使用,glib库中定义的g_auto和g_autoptr宏就应用得很不错,值得学习。
GCC C扩展特性有很多,以下是个人觉得非常值得学习和使用的特性。

1、语句表达式

//表达式为一块语句,最后的为返回值,类似逗号表达式
({ int y = foo (); int z;
   if (y > 0) z = y;
   else z = - y;
   z; })

#define foreach(v) \
   for(int i = 0; ({printf("i[%d] < v[%d]\n", i, v); i < v;}); ({printf("i = %d\n", i);     i++;}))

2、本地声明的标签

#define SEARCH(value, array, target)              \
do {                                              \
  __label__ found;                                \
  typeof (target) _SEARCH_target = (target);      \
  typeof (*(array)) *_SEARCH_array = (array);     \
  int i, j;                                       \
  int value;                                      \
  for (i = 0; i < max; i++)                       \
    for (j = 0; j < max; j++)                     \
      if (_SEARCH_array[i][j] == _SEARCH_target)  \
        { (value) = i; goto found; }              \
  (value) = -1;                                   \
 found:;                                          \
} while (0)

//普通标签不能在宏里使用,本地声明的标签可以
//使用语句表达式返回value
#define SEARCH(array, target)                     \
({                                                \
  __label__ found;                                \
  typeof (target) _SEARCH_target = (target);      \
  typeof (*(array)) *_SEARCH_array = (array);     \
  int i, j;                                       \
  int value;                                      \
  for (i = 0; i < max; i++)                       \
    for (j = 0; j < max; j++)                     \
      if (_SEARCH_array[i][j] == _SEARCH_target)  \
        { value = i; goto found; }                \
  value = -1;                                     \
 found:                                           \
  value;                                          \
})

3、标签作为值

//不建议使用
void *ptr;
/* … */
ptr = &&foo;
goto *ptr;

static void *array[] = { &&foo, &&bar, &&hack };
goto *array[i];

4、嵌套函数

//只能用于 C ,不能用于 C++
hack (int *array, int size)
{
  void store (int index, int value)
    { array[index] = value; }

  intermediate (store, size);
}

//嵌套函数与本地标签一起使用
bar (int *array, int offset, int size)
{
  __label__ failure;
  int access (int *array, int index)
    {
      if (index > size)
        goto failure;
      return array[index + offset];
    }
  int i;
  /* … */
  for (i = 0; i < size; i++)
    /* … */ access (array, i) /* … */
  /* … */
  return 0;

 /* Control comes here from access
    if it detects an error.  */
 failure:
  return -1;
}

5、非本地跳转

6、__builtin_函数调用

extern int myprintf (FILE *f, const char *format, ...);
extern inline __attribute__ ((__gnu_inline__)) int
myprintf (FILE *f, const char *format, ...)
{
  int r = fprintf (f, "myprintf: ");
  if (r < 0)
    return r;
  int s = fprintf (f, format, __builtin_va_arg_pack ()); //可变参数调用,不使用宏
  if (s < 0)
    return s;
  return r + s;
}

7、typeof or typeof

//可以和语句表达式一起使用
#define max(a,b) \
  ({ typeof (a) _a = (a); \
      typeof (b) _b = (b); \
    _a > _b ? _a : _b; })
    
typeof (*x) y;
typeof (typeof (char *)[4]) y;  //char *y[4];

#define pointer(T)  typeof(T *)
#define array(T, N) typeof(T [N])

//__auto_type 类似于 C++ 中的 auto
#define max(a,b) \
  ({ __auto_type _a = (a); \    //优先使用__auto_type,而不是typeof
      __auto_type _b = (b); \
    _a > _b ? _a : _b; })

8、条件表达式(省略操作数)

x ? : y

9、128位整型

__int128
unsigned __int128

19、空结构体

struct empty {
};    //空结构体,占用字节数为 0

20、可变长度数组

FILE *
concat_fopen (char *s1, char *s2, char *mode)
{
  char str[strlen (s1) + strlen (s2) + 1];    //数组长度不是常量表达式,长度可变
  strcpy (str, s1);
  strcat (str, s2);
  return fopen (str, mode);
}

void
foo (int n)
{
  struct S { int x[n]; };    //结构体中包含可变长度数组
}

27、非常量初始值

foo (float f, float g)
{
  float beat_freqs[2] = { f-g, f+g };
  /* … */
}

30、case 范围

switch(i)
{
    case low ... high:
        /* ... */
    case 1 ... 5:
        /* ... */
}

33、通用函数属性

constructor 构造函数属性
destructor 析构函数属性

34、变量__cleanup__属性

#include <stdio.h>                                                                           
#include <stdlib.h>
#include <string.h>

void free_str(char **str)
{
    printf("free %p\n", *str);
    free(*str);
}

//设置变量退出作用域自定调用函数释放内存
#define FREE_STR __attribute__ ((__cleanup__(free_str)))

#define Str FREE_STR char


int main()
{
    Str *p = malloc(100);
    strcpy(p, "test for auto free.");
    printf("%s\n", p);

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 你好!C语言的HelloWorld程序通常是这样的: ``` #include <stdio.h> int main() { printf("Hello, World!\n"); return 0; } ``` 这个程序会输出"Hello, World!",作为学习C语言的入门程序,它非常简单易懂。希望对你有帮助! ### 回答2: C语言是一种基础的编程语言,让我们开始学习它的第一个程序:HelloWorld。HelloWorld是一个简单的程序,它的作用是在屏幕上打印出"Hello World"这个字符串。 在C语言中,我们可以使用printf函数来实现打印功能。首先,我们需要在程序的开头引入stdio.h头文件,这个头文件包含了printf函数的定义。 接下来,我们需要在main函数中编写代码。main函数是C程序的入口函数,它是程序执行的起点。我们可以在main函数中调用printf函数来打印"Hello World"字符串。 代码如下: #include <stdio.h> int main() { printf("Hello World\n"); // 使用printf函数打印字符串 return 0; } 在这段代码中,printf函数的参数是一个字符串常量,用双引号括起来。字符串常量用于表示一串字符,可以在屏幕上打印出来。 最后,我们使用return 0语句来表示程序的正常结束。main函数的返回值类型是int,通常约定返回0表示程序运行成功。 要运行这个程序,我们需要将它保存为以.c为后缀名的文件,比如helloworld.c。然后,我们可以使用C语言编译器如gcc对文件进行编译生成可执行文件,比如a.out。最后,我们可以在命令行中输入./a.out来运行这个程序,在屏幕上就会打印出"Hello World"。 ### 回答3: C语言是一种广泛应用于编写程序的高级编程语言。它是贝尔实验室的Dennis Ritchie于1972年开发的。C语言简洁、高效,并具有跨平台的特性,成为了一种非常受欢迎的编程语言。 下面是使用C语言编写的HelloWorld程序的示例: ```c #include <stdio.h> int main() { printf("Hello World!\n"); return 0; } ``` 在这个示例程序中,我们首先包含了`stdio.h`头文件,这个头文件提供了输入输出相关的函数和常量的定义。 接下来,在`main`函数内部,我们使用`printf`函数来打印字符串"Hello World!"到控制台。`printf`函数用于格式化输出,并在字符串的末尾加上`\n`,表示换行。 最后,我们使用`return 0`来表示程序的正常结束。 当我们编译并运行这个程序时,控制台上会输出"Hello World!"的信息,表示程序成功地输出了这个简单的欢迎语。 HelloWorld程序是最简单的C语言程序之一,它通常用于新手学习C语言的入门示例。通过编写和运行这个程序,我们可以了解C语言的基本语法和程序结构,以及如何在控制台上输出文本。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值