tags:

views:

152

answers:

6

I have heard a lot about the importance of programming style. In my opinion, indention is easy to deal with. But other things frustrated me a lot. Considering a particular example to demonstrate the use of inet_makeaddr.

/* demonstrate the use of host address functions */
#include <stdio.h>
#include <arpa/inet.h>
#include <netinet/in.h>

int
main(void)
{
 /* inet_makeaddr demo */
 uint32_t neta = 0x0a3e5500;
 uint32_t hosta = 0x0c;

 struct in_addr alla = inet_makeaddr(neta, hosta);

 printf("makeaddr of net: %08x and host: %08x = %08x\n", 
    neta, hosta, alla);

 return 0;
}

Somebody may want to write as follows:

 uint32_t neta;
 uint32_t hosta;
 struct in_addr alla;

 neta = 0x0a3e5500;
 hosta = 0x0c;
 alla = inet_makeaddr(neta, hosta);

Then others may always initialize the variable when defined:

 uint32_t neta = 0;
 uint32_t hosta = 0;
 struct in_addr alla = {0};

 neta = 0x0a3e5500;
 hosta = 0x0c;
 alla = inet_makeaddr(neta, hosta);

Is any one of these better than the other ones, or it is just a personal taste?

A: 

I like to initialize values when defining. At least you know you won't have any "silly" NULL reference errors.

Kyle Rozendo
+4  A: 

I think the first of the three examples is the best: the second example one has uninitialized variables, and the third example has variables initialized to a meaningless (zero) value. I prefer to initialize variables (with a meaningful value) as soon as I define them (so that I don't have uninitialized variables). See also Should Local Variable Initialisation Be Mandatory?

ChrisW
The third one is wasteful; there's no benefit to setting the variables to 0 and then overriding that assignment. The compiler might omit the zeroes - but it might not.
Jonathan Leffler
Not exactly,at least microsoft c/c++ compiler would initiliaze stack variable with CC.Initiliazing stack var(esp.struct) with 0 is becuase some Windows APIs only recieve zero initiliazed structures.
Jichao
@jcyang: In that case, you would actually be using 0 for the value and not immediately setting it with the value you should have used to initialize in the first place. Initializing to 0 and immediately setting to something else is just silly.
Chuck
@Chuck: In the case I only need to set some spec fileds of one structure and Windows require the other fileds to be zero-initiliazed,I will initialize the structure with zero.
Jichao
@jcyang: The 0xCC assignment is only done for debug builds (one of the reasons release code may behave differently)
Peter Olsson
@Peter Olsson:Thanks for the message.I have tried vc6 release and its true that release build does not do 0xcc assignment.
Jichao
You might like to read http://stackoverflow.com/questions/420343/separate-debug-and-release-builds
ChrisW
A: 

The bottom one has the small advantage that even if you change the code so that the initialization is no longer performed, you'll never have garbage in the variables, but only zeros. The top one has the same advantage though. My own preference in C is for functions to be extremely short, so that you never have to worry about those kinds of things, so I use the top form or the second form. But if your functions are long-winded, initializing everything to zero might be the way to go.

Kinopiko
A: 

Please consider reading Linus' thoughts on coding style for the Linux kernel.

Bart J
AFAICS, the URL does not cover variable initialization.
Jonathan Leffler
A: 

Personally I define the variables close to the function call if they are interesting for the function call. If it is an uninteresting variable I usually define it it in the declaration.

Peter Olsson
This is C not C++. In C you have to declare all variables at the top of the scope.
Steve Rowe
@Steve: Not in C99. It allows for mixed declarations and code.
Chuck
A: 

It is usually always better to initialise variables in any language. Somehow it's just oen of those little things that make your life easier, just like leaving a trailing comma.

Although if you are going to initialise your variables it's probably best to do it with a value that means something to your algorithm, otherwise you're not solving anything, just changing the way everything behaves when you create a bug.

Swizec Teller