TExcelTAD
Home › C › C Variable Names

C Variable Names (Identifiers)

Every variable, function and type you create needs a name. In C these names are called identifiers. The compiler enforces a few strict rules about what a name may look like, and on top of those rules there are conventions that make code pleasant to read.

The rules

Rule Valid Invalid
May contain letters, digits and underscores total_2, maxValue total-2, max value
Must start with a letter or underscore _count, item1 1item
No spaces or special characters ($ @ # ! …) price_usd price$
Case-sensitive score and Score are different —
Cannot be a keyword int_value int, return, while

Here are some valid names in action:

c · try it yourself
#include <stdio.h>

int main(void) {
    int studentCount = 28;
    int student_count = 30;     // a different variable!
    int _hidden = 1;
    int x2 = 4;
    double max_speed_kmh = 120.5;

    printf("%d %d %d %d %.1f\n", studentCount, student_count, _hidden, x2, max_speed_kmh);
    return 0;
}
28 30 1 4 120.5

And some names the compiler rejects:

c
int 2fast = 1;        // error: starts with a digit
int my-age = 30;      // error: '-' is the minus operator
int first name = 5;   // error: space in the name
int float = 1.0;      // error: 'float' is a keyword

C keywords

You cannot use any of these as identifiers:

text
auto      break     case      char      const     continue  default   do
double    else      enum      extern    float     for       goto      if
inline    int       long      register  restrict  return    short     signed
sizeof    static    struct    switch    typedef   union     unsigned  void
volatile  while     _Bool     _Complex  _Imaginary _Alignas _Alignof  _Atomic
_Generic  _Noreturn _Static_assert _Thread_local

Note: Names that start with an underscore followed by a capital letter (like _Bool), or with two underscores, are reserved for the compiler and standard library. Avoid them in your own code, even though the compiler may accept them.

Case sensitivity in practice

Because C is case-sensitive, total, Total and TOTAL are three separate identifiers. Relying on that is confusing, so don't — but be aware of it when you get an "undeclared identifier" error that seems impossible.

c · try it yourself
#include <stdio.h>

int main(void) {
    int total = 1;
    int Total = 2;
    int TOTAL = 3;
    printf("%d %d %d\n", total, Total, TOTAL);
    return 0;
}
1 2 3

Naming conventions

The compiler only cares about the rules; humans care about conventions. Common C conventions are:

Kind of name Typical style Example
Variables and functions snake_case (lowercase with underscores) word_count, read_file()
Macros and constants defined with #define UPPER_CASE MAX_USERS, BUFFER_SIZE
Types (struct, typedef) snake_case or PascalCase struct point, Student
Loop counters short names i, j, k

Some projects (and libraries like the Windows API) use camelCase instead. Whatever you pick, stay consistent within a project.

Choosing good names

Compare these two versions of the same calculation:

c · try it yourself
#include <stdio.h>

int main(void) {
    // Hard to understand
    int a = 60, b = 24, c = a * b;
    printf("%d\n", c);

    // Self-explanatory
    int minutes_per_hour = 60;
    int hours_per_day = 24;
    int minutes_per_day = minutes_per_hour * hours_per_day;
    printf("%d\n", minutes_per_day);
    return 0;
}
1440
1440

Both print the same thing, but only the second tells the reader what is being calculated.

Tip: Good names describe the meaning of the value (unread_messages), not its type (int_value). Short names are fine when the scope is tiny, such as a loop counter i.

Test yourself

Which of these is a valid C identifier?

  1. 3d_model
  2. my-var
  3. _total2
  4. long

Are count and Count the same variable in C?

  1. Yes
  2. No, C is case-sensitive
  3. Only inside main
  4. Only if declared as int

Which naming style is conventional for #define constants in C?

  1. camelCase
  2. snake_case
  3. UPPER_CASE
  4. kebab-case