好的C编程风格 [英] Good C programming style

查看:71
本文介绍了好的C编程风格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!


我正在考虑一些好的编程风格,以及一些

主题的优点和缺点。当然,这与缩进无关......学生

现在依赖于java(太糟糕了)我需要一些严肃的动机

许多问题..我希望你能帮助我:)我现在从两个主要的

开始,其他人肯定会来!


- 函数变量:他们' '习惯了java,所以没有指针。个人

我会使用指针,但为什么可以更好地使用:

void myfun(int& i)或(int * i)或(int i)

假设功能可以和不可以(当然最后一种情况)修改

a参数。我看到性能问题(堆栈相关)......


- 定义:为什么他们使用函数和变量定义?为什么他们不应该?

#define MY_PI 3.1415

#define fun(x)((x)*(x))

-

Sensei< se ****** @ mac.com>


愚蠢和天才之间的区别在于天才有它的

限制。 (A.爱因斯坦)

Hi!

I''m thinking about a good programming style, pros and cons of some
topics. Of course, this has nothing to do with indentation... Students
are now java-dependent (too bad) and I need some serious motivations
for many issues... I hope you can help me :) I begin with the two major
for now, others will come for sure!

- function variables: they''re used to java, so no pointers. Personally
I''d use always pointers, but why could be better to use:
void myfun(int &i) or (int *i) or (int i)
given that the function can and cannot (the last case of course) modify
a parameter. I see performance issues (stack related)...

- defines: why they use function and variable defines? why they shouldn''t?
#define MY_PI 3.1415
#define fun(x) ((x) * (x))
--
Sensei <se******@mac.com>

The difference between stupidity and genius is that genius has its
limits. (A. Einstein)

推荐答案

" Sensei" < SE ****** @ tin.it>在留言中写道

news:di ********** @ news.doit.wisc.edu ...
"Sensei" <se******@tin.it> wrote in message
news:di**********@news.doit.wisc.edu...
嗨!
<我正在考虑一个好的编程风格,一些主题的优点和缺点。
当然,这与缩进无关......学生现在依赖于java依赖(也是我需要一些严肃的动机来解决很多问题......


所以你在教C?

我希望你能帮助我:)我现在从两个专业开始,其他人肯定会来的!

- 函数变量:他们习惯了java,所以没有指针。我个人总是会使用指针,但为什么可以更好地使用:
void myfun(int& i)


但你不喜欢t似乎能够区分C和C ++

或(int * i)或(int i)
假设函数可以和不可以(当然最后一种情况)修改
参数。我看到性能问题(堆栈相关)...


你似乎没有意识到关于

过早优化的共识
- 定义:为什么他们使用函数和变量定义?为什么他们不应该?
#define MY_PI 3.1415
#define fun(x)((x)*(x))


你不要似乎没有意识到

真实函数可以给出的类型检查,而宏则没有。
Hi!

I''m thinking about a good programming style, pros and cons of some topics.
Of course, this has nothing to do with indentation... Students are now
java-dependent (too bad) and I need some serious motivations for many
issues...
So you''re teaching C?
I hope you can help me :) I begin with the two major for now, others will
come for sure!

- function variables: they''re used to java, so no pointers. Personally I''d
use always pointers, but why could be better to use:
void myfun(int &i)
but you don''t appear able to distinguish C from C++
or (int *i) or (int i)
given that the function can and cannot (the last case of course) modify a
parameter. I see performance issues (stack related)...
and you don''t appear to be aware of the consensus about
premature optimization

- defines: why they use function and variable defines? why they shouldn''t?
#define MY_PI 3.1415
#define fun(x) ((x) * (x))
and you don''t appear to be aware of the type-checking that a
real function can give, and that macros do not.



快速运行在另一个方向,

-Mike


Running quickly in the other direction,
-Mike


Sensei写道:
我正在考虑一个好的编程风格,一些主题的利弊。当然,这与缩进无关......学生现在依赖于java(太糟糕了)我需要一些严肃的动机来解决很多问题...我希望你能帮助我: )我现在从两个专业开始
,其他人肯定会来!

你问的问题是基本的。阅读一本关于C的好书和

这些主题应该得到一些细节处理,以及解释

,这将有助于你决定什么是合适的和为什么。

- 函数变量:它们用于java,所以没有指针。个人
我会使用指针,但为什么可以更好地使用:
void myfun(int& i)或(int * i)或(int i)


int& i不是C.你可能在考虑使用C ++。


至于何时使用指针,何时不使用指针,这很简单:指针对于

可变大小的数据,如字符串,指针也用于在C中实现
逐个引用,即你需要改变的参数。 />
指针也用于结构,见下文。最后,指向

函数,因为没有其他方法可以传递它们。

假设函数可以和不可以(当然最后一种情况)修改
一个参数。我看到性能问题(与堆栈相关)......

大部分无关紧要。 AC成语总是通过

指针传递结构类型,因为通过值传递大多数结构意味着复制,这在大多数情况下确实是浪费时间,特别是如果值是不是修改了
。虽然编译器可以在某些情况下对其进行优化,但预计并不是b $ b,并且程序员不会指望它。

- 定义:他们使用的原因函数和变量定义?为什么他们不应该?
#define MY_PI 3.1415


建立C练习是使用#define作为常量,因为C有点

在常数方面令人费解。声明变量const为了数组大小的目的,
没有使其值成为常量表达式,例如
。此外,内存是为const变量分配的(很棒

oxymoron,顺便说一下:-)当它通常没有必要时。


没有诸如功能之类的东西。或变量或变量。然而,定义。

宏只建立文本替换规则。

#define fun(x)((x)*(x))
I''m thinking about a good programming style, pros and cons of some
topics. Of course, this has nothing to do with indentation... Students
are now java-dependent (too bad) and I need some serious motivations for
many issues... I hope you can help me :) I begin with the two major for
now, others will come for sure!
The questions you''re asking are elemental. Read a good book on C and
these topics should be treated in some detail, along with explanations
that will help you decide on what is appropriate and why.
- function variables: they''re used to java, so no pointers. Personally
I''d use always pointers, but why could be better to use:
void myfun(int &i) or (int *i) or (int i)
int &i is not C. You may be thinking of C++.

As for when to use pointers and when not, it''s simple: pointers for
variable-sized data like strings, pointers also to implement
call-by-reference in C, that is, arguments you''ll need to change.
Pointers also for structs, see below. And finally, pointers for
functions, since there''s no other way to pass them.
given that the function can and cannot (the last case of course) modify
a parameter. I see performance issues (stack related)...
Mostly irrelevant. A C idiom is to always pass structure types by
pointer because passing most structures by value means copying, which is
indeed a waste of time in most cases, especially if the value isn''t
modified. While a compiler could optimize this in some cases, it''s not
expected to, and programmers don''t count on it.
- defines: why they use function and variable defines? why they shouldn''t?
#define MY_PI 3.1415
Established C practice is to use #define for constants since C is a bit
puzzling when it comes to constants. Declaring a variable "const" does
not make its value a constant expression for purposes of array sizes,
for example. Also, memory is allocated for const variables (great
oxymoron, incidentally :-) when it typically isn''t necessary.

There''s no such thing as a "function" or "variable" define, however.
Macros just establish textual replacement rules.
#define fun(x) ((x) * (x))



尽可能使用函数,必要时使用宏。上面的例子

就是你不需要的情况。


至于什么时候你需要宏,这不是问题编码风格。


S.


Use functions when you can, macros when you have to. The example above
is a case of when you don''t have to.

As for when you need macros, that''s not a matter of coding style.

S.


Sensei< se ****** @ tin.it>写道:
Sensei <se******@tin.it> writes:
我正在考虑一些好的编程风格,一些主题的优点和缺点。当然,这与缩进无关......学生现在依赖于java(太糟糕了)我需要一些严肃的动机来解决很多问题...我希望你能帮助我: )我现在从两个专业开始,其他人肯定会来!

- 函数变量:他们习惯了java,所以没有指针。个人
我会使用指针,但为什么可以更好地使用:
void myfun(int& i)或(int * i)或(int i)
给出功能可以和不可以(当然最后一种情况)
修改参数。我看到性能问题(堆栈相关)...


术语函数变量是误导;你的意思是参数(函数声明中括号之间声明的事件)或

参数(函数调用中传递的实际值)。


声明void myfun(int& i)是无效的C.(我认为它是'b $ b C ++;参见comp.lang.c ++。)


void myfun之间的选择int * p)和void myfun(int i)不是好吗?b $ b真的是风格问题;这只是一个功能

需要做的事情。 (注意我已经更改了指针的名称

参数;调用指针i会产生误导。)如果myfun需要

更改值调用者指定的对象,或者如果您希望

指向整数数组,请使用指针。如果你只是想要
传递一个整数值,请使用int。


注意,如果myfun的参数是一个指针,你只能通过

地址的ojbect(或空指针);你不能通过42或x + 3.


如果参数是结构类型,通常传递一个

指针是有意义的如果你不想修改struct对象。传递

参数意味着复制参数并将其存储在

参数中;对于大型结构,这可能非常昂贵。

这个考虑并不适用于整数,因为传递一份副本之间没有重要的

性能差异一个int并传递一个

的地址副本。

- 定义:为什么他们使用函数和变量定义?为什么他们不应该?
#define MY_PI 3.1415
#define fun(x)((x)*(x))
I''m thinking about a good programming style, pros and cons of some
topics. Of course, this has nothing to do with indentation... Students
are now java-dependent (too bad) and I need some serious motivations
for many issues... I hope you can help me :) I begin with the two
major for now, others will come for sure!

- function variables: they''re used to java, so no pointers. Personally
I''d use always pointers, but why could be better to use:
void myfun(int &i) or (int *i) or (int i)
given that the function can and cannot (the last case of course)
modify a parameter. I see performance issues (stack related)...
The term "function variables" is misleading; you mean parameters (the
things declared between the parentheses in a function declaration) or
arguments (the actual values passed in a function call).

The declaration "void myfun(int &i)" is not valid C. (I think it''s
C++; see comp.lang.c++.)

The choice between "void myfun(int *p)" and "void myfun(int i)" isn''t
really a matter of style; it''s just a matter of what the function
needs to do. (Note that I''ve changed the name of the pointer
parameter; calling a pointer "i" is misleading.) If myfun needs to
change the value of an object specified by the caller, or if you want
to point to an array of integers, use a pointer. If you just want to
pass an integer value, use an int.

Note that if myfun''s parameter is a pointer, you can only pass the
address of an ojbect (or a null pointer); you can''t pass 42 or x+3.

If the parameter is of a struct type, it often makes sense to pass a
pointer even if you don''t want to modify the struct object. Passing
an argument means making a copy of the argument and storing it in the
parameter; for large structures, this can be significantly expensive.
This consideration doesn''t apply to ints, since there''s no significant
performance difference between passing a copy of an int and passing a
copy of its address.
- defines: why they use function and variable defines? why they shouldn''t?
#define MY_PI 3.1415
#define fun(x) ((x) * (x))




对于MY_PI(BTW,其他人的PI更接近3.1416),C不会为
提供定义常量浮点值的机制。你

可以声明

const double my_pi = 3.14159265358979323848264;

但这会创建一个只写变量,而不是一个真正的常量。宏#

对于这类事情非常有用。


至于类似函数的宏,它们有时是最好的创建方式

内联函数。 (C99具有内联函数,但并非所有编译器都支持它们。)还有一些你可以在一个宏中做的事情,你可以很容易地做到这一点。函数,例如使用类型名称作为

参数。但尽量不要太棘手。宏可能很危险,因为它们在编译的早期阶段就已经扩展了。他们可以看起来像函数调用或变量声明一样*但是他们不会像他们一样行动。例如,他们不关注

范围。


-

Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。



For MY_PI (BTW, everybody else''s PI is closer to 3.1416), C doesn''t
provide a mechanism for defining a constant floating-point value. You
can declare
const double my_pi = 3.14159265358979323848264;
but that creates a write-only variable, not a real constant. Macros
are useful for this kind of thing.

As for function-like macros, they''re sometimes the best way to create
inline functions. (C99 has inline functions, but not all compilers
support them.) There are also things you can do in a macro that you
can''t easily do in a function, such as using a type name as an
argument. But try not to be too tricky. Macros can be dangerous,
since they''re expanded in a very early stage of compilation. They can
*look* like function calls or variable declarations, but they don''t
necessarily act like them; for example, they pay no attention to
scope.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


这篇关于好的C编程风格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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