C/C++ Variable names

Phishfry

Son of Beastie

Thanks: 872
Messages: 2,756

#1
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:
Code:
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.
 
OP
OP
Phishfry

Phishfry

Son of Beastie

Thanks: 872
Messages: 2,756

#3
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.

Active Member

Thanks: 86
Messages: 200

#4
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.
 

xavi

Active Member

Thanks: 74
Messages: 108

#8
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'.
 

_martin

Aspiring Daemon

Thanks: 135
Messages: 713

#9
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.
 
Top