• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

C/C++ Variable names



Thanks: 753
Best answers: 1
Messages: 2,323

I am new to C -code but I notice that when a variable name is related to its function it makes it easier to read the code to see what it does.
So for instance I have learned from this forum that buf is short for buffer. Seems quite reasonable. But often in code I am seeing 'fd' used.
From reading I gather that fd means file descriptor.
Sometimes when there is an actual fopen or other file related function I understand why they use fd for variable name.
But when it is used for special system character devices I wonder why.
Here is an example:
int fd = open("/dev/gpiochip0", 0);
So this opens the hardware character device for use and has an integer data type.
Why would you use fd as the variable name.
Obviously you could name it anything you want but we do have standard coding conventions.
Why fd for a character device variable name. I know /dev/gpiochip0 is a file but not a normal file but special, right?

This snippet was not taken from FreeBSD code but some examples I am working with.
Like I say you could name it anything but fd seems hard to read the code with.



Thanks: 753
Best answers: 1
Messages: 2,323

The more I read about 'file descriptor' in the unix sense, it seems to be about sockets and pipes as well as files.
In my narrow mind fd=floppy disk!!
int gc0 = open("/dev/gpiochip0", 0);/*Would seem to be more descriptive*/

Bobi B.


Thanks: 44
Best answers: 1
Messages: 97

int fd is for open(2) and socket(2), as they return integer file descriptors. fopen(3) is upper-level, usually buffered, and returns a pointer to FILE structure, hence FILE *fp.

If you write a tight code and keep function size reasonable you're okay to use fd and/or fp in utility functions (like open a file and abort upon failure), as they don't really care what file system object they work on; can be a file, a character device, a FIFO, a socket, you got the idea. Long-lived variables and/or struct member names you better name appropriately, like http_listener_fd, gpio0 or mcast_recv_fd.

Take a look at Hungarian naming notation, or any other naming notation, in this regard, although IMHO Hungarian is often misunderstood.

In the end, when you work on somebody else's code you should follow their rules (naming, formatting, etc.). When you work in a team, you better use common rules. When you work on your projects it's up to you, just be consistent.


Active Member

Thanks: 71
Messages: 104

So is that what the ch variable represents?
No, in this instance it stands for 'character'. Which is why in the switch statement at line 51 in the code that you've referenced, the variable 'ch' is compared to the alphabetic character 'f'.
However, when used with BSD utilities like chmod, chgrp, chown, it does mean 'change'.


Aspiring Daemon

Thanks: 125
Messages: 674

More often than not it's more important to mind the style(9) you write with than the variable names themselves. There are some common variable names (fd,fp as explained by Bobi B. , i,j as loop vars, c as in char , etc..). But you can name them anything you want ; I've not seen variable naming convention for BSD ; correct me if I'm wrong.