什么是用C最常见的命名约定? [英] What are the most common naming conventions in C?

查看:103
本文介绍了什么是用C最常见的命名约定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是命名约定常用的C使用?我知道至少有二:


  1. 的GNU / Linux / K&安培; R与lower_case_functions

  2. ?名称 ?与UpperCaseFoo功能

我只是在这里谈论℃。我们的大多数项目都是我们用C小型嵌入式系统中。

下面是一个我使用我的下一个项目的规划:


ç命名约定

 结构体的首字母大写
结构成员LOWER_CASE或小写枚举ETitleCase
枚举成员ALL_CAPS或小写公共职能pfx_TitleCase(PFX =两个或三个字母的模块preFIX)
私有函数的首字母大写
琐碎的变量I,X,N,F等...
局部变量LOWER_CASE或小写
全局变量g_lowerCase或g_lower_case(由G_ preFIX搜索)


解决方案

这里最重要的事情是一致性。这就是说,我按照GTK +编码惯例,可归纳如下:


  1. 在瓶盖的所有宏和常量: MAX_BUFFER_SIZE TRACKING_ID_ preFIX

  2. 结构体的名称和类型定义的首字母大写:的GtkWidget TrackingOrder

  3. ,关于结构操作功能:经典的C风格: gtk_widget_show() tracking_order_process()

  4. 指针:这里没有什么花哨的:
    的GtkWidget *富 TrackingOrder *栏

  5. 全局变量:只是不使用全局变量。他们是邪恶的。

  6. 函数是有的,但
    不应该被直接调用,或有
    晦涩的用途,或什么:一个或多个
    强调开头:
    _refrobnicate_data_tables() _destroy_cache()

What are the naming conventions commonly use in C? I know there are at least two:

  1. GNU / linux / K&R with lower_case_functions
  2. ? name ? with UpperCaseFoo functions

I am talking about C only here. Most of our projects are small embedded systems in which we use C.

Here is the one I am planning on using for my next project:


C Naming Convention

Struct  	TitleCase
Struct Members  lower_case or lowerCase

Enum    	ETitleCase
Enum Members    ALL_CAPS or lowerCase

Public functions    pfx_TitleCase (pfx = two or three letter module prefix)
Private functions   TitleCase
Trivial variables   i,x,n,f etc...
Local variables lower_case or lowerCase
Global variables    g_lowerCase or g_lower_case (searchable by g_ prefix)

解决方案

The most important thing here is consistency. That said, I follow the GTK+ coding convention, which can be summarized as follows:

  1. All macros and constants in caps: MAX_BUFFER_SIZE, TRACKING_ID_PREFIX.
  2. Struct names and typedef's in camelcase: GtkWidget, TrackingOrder.
  3. Functions that operate on structs: classic C style: gtk_widget_show(), tracking_order_process().
  4. Pointers: nothing fancy here: GtkWidget *foo, TrackingOrder *bar.
  5. Global variables: just don't use global variables. They are evil.
  6. Functions that are there, but shouldn't be called directly, or have obscure uses, or whatever: one or more underscores at the beginning: _refrobnicate_data_tables(), _destroy_cache().

这篇关于什么是用C最常见的命名约定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆