The rules for naming variables, or identifiers, in C are simple: The names must start with a letter and contain letters and numbers. The _ (underscore) counts as a letter. Upper- and lowercase letters are considered differently. And only the first 31 characters are significant, though some platforms may extend this length.
I’ve covered the snake_case and camelCase methods for naming identifiers in an Exercise post from last year. Another naming convention is brought to you from Microsoft: Hungarian notation.
Named for Microsoft programmer Charles Simonyi, whom I assume is Hungarian, this naming convention uses prefixes to identify variable types. Remember that C variable names — identifiers — sport no relation to whatever it is they’re naming. Identifiers can label variables, constants, functions, and just about anything. The name ticks
could identify a function, a char, an int, a float, and so on. When Hungarian notation is applied, you might use the name lTicks
, where the prefixed little L indicates that the variable is a long int. It’s these prefixes that define Hungarian notation and identify variable data types.
The following table lists various Hungarian Notation prefixes. This list isn’t “official” and variations exist. I suppose the point is to be consistent in your own code.
Prefix | Meaning | Prefix | Meaning |
---|---|---|---|
b |
boolean | i |
int |
by |
unsigned char (byte) | l |
long |
c |
character | n |
integer |
d |
double | p |
pointer |
dw |
unsigned long | s |
string |
f |
float | sz |
null-terminated string |
fp |
floating point | u |
unsigned |
g_ |
global/external | v |
void |
h |
handle | w |
word (unsigned int) |
It was the sz
prefix that sent me on my Hungarian notation voyage of discovery. I was examining code online and saw a variable such as szTextInput
. The “text input” part is obvious, but I only guessed that “sz” might mean zero-terminated string, “string zero.” A quick search confirmed what I knew and identified the naming method as Hungarian notation.
Hungarian Notation isn’t without its drawbacks. First and foremost, naming a variable with Hungarian Notation does nothing to change the variable’s underlying type. In fact, you must rely on the programmer being honest when using these prefixes. I also observe a lack of consistency and several variations on Hungarian Notation, probably due to its non-binding nature.
Another disadvantage is that, unlike snake_case and camelCase, Hungarian notation does little to make a variable more readable. In some cases, it truly obfuscates the variable name.
In a way, Hungarian Notation reminds me of the sigils used in other programming languages. BASIC was my first computer language, which used character suffixes (sigils) to identify variable types. For example, TEXT$
identifies a string variable and COUNT%
is an integer. These sigils are coded into the language and required with the variable’s declaration and use.
Most importantly, it’s good to know what Hungarian Notation is and why it’s used. Whether you use it in your own code, or whether you develop something similar, is all up to you. C is quite flexible, which is both its weakness and strength.
Having used Hungarian notation in various projects over the years, I am still unsure about whether itʼs a good or a bad idea to annotate variable names with type information.
Once one gets used to these prefixes something feels off if theyʼre missing… but are they really useful? I guess with modern IDEs the answer is “no”, but itʼs not hard to see why back in the day, when code was written in simple text editors, the answer might have been “yes”.
To my knowledge even Microsoft only uses Hungarian notation for (somewhat legacy) Win32 code, when writing .NET (C#) applications the use of these prefixes is actively discouraged.
I could see using such notation as a suffix, though it still has its problems.
One of the reasons I consistently declare variables at the top of a function is for reference. It’s easier to me to look up a data type there than it is to hunt through the code for a midstream declaration.
I suppose the bottom line is to be consistent. Thank you again for sharing such great details!
In VS Code and presumably other IDEs you can mouseover a variable and a popup tells you the type, therefore Hungarian notation is superfluous these days.
A major problem is that if you need to change the type of a variable (which mostly happens with numeric types) it messes things up. Again though modern IDEs can automate that sort of stuff.