0% found this document useful (0 votes)
4 views

05-variable-names

The document discusses best practices for naming variables in C programming, emphasizing the importance of clear and descriptive names over adhering to character limits. It outlines the rules for valid variable names, reserved keywords, and various naming conventions such as snake_case and camelCase. Additionally, it highlights potential issues with variable name length and readability in code.

Uploaded by

carlotta.hoelzle
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

05-variable-names

The document discusses best practices for naming variables in C programming, emphasizing the importance of clear and descriptive names over adhering to character limits. It outlines the rules for valid variable names, reserved keywords, and various naming conventions such as snake_case and camelCase. Additionally, it highlights potential issues with variable name length and readability in code.

Uploaded by

carlotta.hoelzle
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

PT1

variable names
professor patrick baudisch
pt1
hasso-plattner institute
imagine a actual operating system (Linux has ~15 Million lines of code)

now imagine you are working on it and you find this:


counter -= 2; //decrements the counter by 2
you’ll have to search 15M of lines of code to find out.

better
number_of_active_processes -= 2;
// we just removed two running processes at once
variable names
variable names in C ::
are made up of letters (upper and lower case) and digits.
The underscore character ("_") is also permitted.
variables starting with “_” (underscore) are formally ok,
but discouraged, since conflicts with library names could occur

from <stdio.h>
_IOFBF // Input/output fully buffered.
_IOLBF // Input/output line buffered.
_IONBF // Input/output unbuffered.
not ok. continue is a reserved word
and thus cannot be a variable name

char continue = ‘1’; //true

ok or not?
<30sec brainstorming>
these are the C keywords. You cannot use them as variable names

auto double int struct


break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while
only the first 31 characters are guaranteed to be considered when
distinguishing variables.
! if your variable names differ in the 32nd character, compiler might handle it but
on other platforms the program might not compile or behave strangely

only the first 6 characters (and _no_ case-sensitivity) guaranteed


for functions and external variables (depending on assembler/linker)
and the standard library shows it
C++ renames functions so as to include
the types of their parameters. this makes
relax… for some very long function names

“ These days, (in part because of type-safe linkage in C++), you can
reasonably rely on much longer names for external symbols. If you start drifting
above 31 characters, you may run into problems - but you are also running into


readability problems too.”

http://stackoverflow.com/questions/2352209/max-identifier-length
my 2ct
use great variable names, irrespective of any 6 character limit
(the chance of your code ever running on a 1980’s main frame is smaller than
your code never running at all, because you lost track of variable names.)
styles for naming
variables
capitalization
from what you have seen so far, how do C programmers name their variables
if the variable name is a single word and
if the variable name is composed of multiple words?

<30sec brainstorming>
lowercase ::
The C standard library is mostly lowercase
(and 6ish characters long
lisp-case aka kebab-case ::
words separated by minus '-'
does not work in C
The name "snake_case" comes from the Ruby community,
where it was coined in 2004 by Gavin Kistner, writing:
"BTW...what *do* you call that naming style? snake_case?
That's what I'll call it until someone corrects me.”

snake_case ::
separates words using underscore (e.g. back_color)
in C widely used for variable names

https://en.wikipedia.org/wiki/Snake_case
camelCase ::
starts with a lower case letter (e.g. backColor)
PascalCase ::
like camelCase, but always begins with a capital letter (e.g. BackColor)
coming soon (but what might it be?)
ALL CAPS ::
in C only used for macros = the "preprocessor"

what might this do? and what might that do?

<30sec brainstorming>
i ::
as in
for (int i=3; i<5; i++)
C style guides
just some random example

https://www.cs.arizona.edu/~mccann/cstyle.html
ongoing discussion on stack overflow

http://stackoverflow.com/questions/1722112/what-are-the-most-common-naming-conventions-in-c
if you like to topic, also look at
https://en.wikipedia.org/wiki/Naming_convention_(programming)
end
variable names
professor patrick baudisch
pt1
hasso-plattner institute

You might also like