Skip to content
Tutorials Center
My learning

Topics

Lesson 7 of 135 ยท Getting Started

Keywords and Identifiers

Learn every C keyword up to C23, grouped by purpose, plus the rules for valid identifiers, names reserved for the compiler and library, and naming conventions.

A C program is built from two kinds of words. Keywords are the words the language itself owns, like int, return and while: each has a fixed meaning, and you can't use them for anything else. Identifiers are the names you invent, for variables, functions, types and macros. Good identifiers make code read almost like prose; bad ones make even simple code hard to follow.

This lesson lists every keyword in C23 grouped by what it does, explains the exact rules an identifier must follow, warns you about names that are reserved for the compiler and standard library, and sums up the naming conventions used in real C projects.

What a keyword is

A keyword is a reserved word with a meaning defined by the C standard. The compiler recognizes it before anything else, so you can't name a variable int or a function while. Try it; this example fails on purpose:

C A keyword can't be a name

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

GCC reports expected identifier or '(' before 'return': when it sees return, it expects a return statement, not a name. Rename the variable to something like result and it compiles.

All keywords are lowercase, apart from the ones that start with an underscore and a capital letter, such as _Generic. Because C is case-sensitive, Int or RETURN are not keywords; they're legal (but terrible) identifiers.

The keywords of C23

C started with 32 keywords in C89. C99, C11 and C23 added more. Here they all are, grouped by purpose, with the standard that introduced each newer one:

Basic types

Keyword Meaning
char, int, float, double the fundamental types
short, long, signed, unsigned modify the size or signedness of int and char
void "no type": no return value, no parameters, or a generic pointer
bool (C23; _Bool since C99) the Boolean type
_Complex, _Imaginary (C99) complex numbers (_Imaginary is optional and rarely supported)
_BitInt (C23) integers with an exact number of bits, like _BitInt(24)
_Decimal32, _Decimal64, _Decimal128 (C23) decimal floating point (optional)

Constants

Keyword Meaning
true, false (C23) the two Boolean values
nullptr (C23) the null pointer constant

Building your own types

Keyword Meaning
struct, union, enum structures, unions and enumerations
typedef gives a new name to an existing type
typeof, typeof_unqual (C23) the type of an expression

Type qualifiers

Keyword Meaning
const the object must not be modified
volatile the value can change outside the program's control (hardware registers)
restrict (C99) a promise that a pointer is the only way to reach its data
_Atomic (C11) atomic access for multithreading

Storage and linkage

Keyword Meaning
auto automatic storage (the default for locals); in C23 it also infers a type
static keeps a local alive between calls, or hides a global from other files
extern declares something defined in another file
register a historical hint to keep a variable in a CPU register
thread_local (C23; _Thread_local since C11) one copy of the variable per thread
constexpr (C23) a constant whose value is known at compile time
inline (C99) a hint that a function can be expanded at the call site
_Noreturn (C11) the function never returns (C23 prefers the [[noreturn]] attribute)

Control flow

Keyword Meaning
if, else choose between paths
switch, case, default multi-way choice
while, do, for loops
break, continue leave a loop or switch, or skip to the next loop iteration
goto jump to a label
return leave a function, optionally with a value

Compile-time tools

Keyword Meaning
sizeof size of a type or object in bytes
alignof, alignas (C23; _Alignof, _Alignas since C11) query or set alignment
static_assert (C23; _Static_assert since C11) fail compilation if a condition is false
_Generic (C11) choose an expression based on a type

That's the whole language: about 50 words, and you'll use fewer than 30 regularly. Compare that with the thousands of functions in the standard library, which are identifiers, not keywords. printf, main and malloc are just names.

Note: Why do some C11 keywords look odd, like _Bool and _Static_assert? When C99 and C11 added them, plain names like bool would have broken existing programs that already used those words. Names starting with _ and a capital letter were reserved, so they were safe. Headers such as <stdbool.h> then offered the friendly spellings as macros. C23 finally made bool, true, false, static_assert, alignof, alignas and thread_local real keywords; the old spellings still work.

Here are some of the C23 keywords in action. You'll study each in its own lesson:

C Some C23 keywords

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

None of these needs a header in C23. With an older standard you'd include <stdbool.h> for bool, write NULL (from <stddef.h> or <stdio.h>) instead of nullptr, and use #define or enum instead of constexpr. Constants and Booleans cover these.

Rules for identifiers

An identifier names a variable, function, type, structure member, label or macro. The rules are short:

  1. It may contain letters (aโ€“z, Aโ€“Z), digits (0โ€“9) and the underscore _.
  2. It must not start with a digit.
  3. It must not be a keyword.
  4. It's case-sensitive: speed, Speed and SPEED are three different identifiers.
  5. No spaces, hyphens or other punctuation: max-speed means max minus speed.
Identifier Valid? Why
total yes letters only
player2 yes digits allowed after the first character
_count yes, but avoid it see reserved names below
max_speed yes underscores separate words
2nd_place no starts with a digit
max-speed no - is the minus operator
my score no contains a space
float no a keyword
Float yes case differs from the keyword (but confusing)

The standard guarantees that compilers distinguish at least the first 63 characters of internal names and 31 characters of names visible to the linker. Modern compilers have no practical limit, so choose clear names over short ones.

Since C99, identifiers may also include other Unicode letters, and GCC accepts them written directly in UTF-8. Code that's shared internationally sticks to ASCII names, though, because they're easy to type everywhere.

C Valid identifiers

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

It prints 45 15 5 12.5. Every name here follows the rules; the comments note which ones break common conventions.

Reserved names: avoid these

Some identifiers are legal to type but reserved for the compiler and the standard library. If you use them, your code may clash with something the implementation defines, today or in a future version, and the result is undefined behavior:

  • Names starting with an underscore followed by a capital letter or another underscore, like _Count or __total. They're reserved everywhere. That's where keywords like _Generic and compiler macros like __FILE__ live.
  • Names starting with a single underscore at file scope (outside any function), like a global _buffer.
  • Names of standard library functions, macros and types when you include their header or declare them at file scope: printf, strlen, exit, errno, size_t, NULL, EOF, INT_MAX.
  • Patterns reserved for future library additions: names beginning with str, mem or wcs followed by a lowercase letter (strip, memo), and is or to followed by a lowercase letter (isvalid, tonumber). C23 relaxed some of these rules, but avoiding them is still the safe choice.
  • On POSIX systems (Linux, macOS), names ending in _t are reserved for system types, so name your own types without it if you want to be strictly correct.

Using a standard library name for your own variable can produce confusing errors. This example fails to compile on purpose:

C Hiding a library function

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Inside main, printf now means the int variable, so the call fails with called object 'printf' is not a function or function pointer. The compiler isn't wrong: you hid the function behind your variable. Pick a different name, and the problem disappears.

Tip: A simple rule keeps you safe: never start your own names with an underscore, and don't reuse names from the standard library. Large projects often add a short prefix to every public name, such as png_ in libpng or SDL_ in SDL, to avoid clashes with other libraries too.

Naming conventions

The language doesn't care about style, but people do. Most C code follows these conventions, and this course uses them:

What Convention Examples
Variables and functions snake_case, lowercase with underscores line_count, read_config()
Macros and #define constants UPPER_SNAKE_CASE MAX_USERS, BUFFER_SIZE
Enum constants usually UPPER_SNAKE_CASE COLOR_RED, STATE_IDLE
Types (struct, typedef) snake_case; some projects use PascalCase struct point, Point
Library-wide names a common prefix json_parse(), json_free()

Some guidelines for choosing names:

  • Say what it is, with units if needed. timeout_ms beats t; price_cents beats price.
  • Short names for short scopes. i and j are fine as loop counters; a variable that lives for 200 lines needs a real name.
  • Functions are actions. Use verbs: parse_line, print_report, is_empty (a function returning true/false reads well as a question).
  • Avoid names that differ only in case or a single character, like count and Count, or item and items in the same function.
  • Be consistent. If the codebase writes num_items, don't add itemCount next to it.

Variables puts these names to work.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which of these is a valid C identifier?

    point_3d uses only letters, digits and underscores and doesn't start with a digit. The others start with a digit, contain a minus sign, or are a keyword.
  2. 2. Which word became a real keyword in C23, no header needed?

    nullptr is a C23 keyword. NULL is a macro from standard headers, and printf and main are ordinary identifiers.
  3. 3. Why should you avoid naming a variable _Total?

    Identifiers that begin with _ followed by an uppercase letter (or another _) are reserved everywhere for the compiler and standard library.
  4. 4. What happens when you declare int printf = 1; inside main and then call printf("hi\n"); there?

    The local variable hides the library function inside main. Calling an int isn't possible, so GCC reports that the called object is not a function.
  5. 5. By common C convention, what is BUFFER_SIZE most likely to be?

    UPPER_SNAKE_CASE is conventionally reserved for macros and constants, while variables and functions use lowercase snake_case.

Summary

  • Keywords are reserved words with a fixed meaning; C23 has about 50, grouped into types, qualifiers, storage, control flow and compile-time tools.
  • C23 turned bool, true, false, nullptr, static_assert, alignas, alignof, thread_local, constexpr and typeof into keywords; old _Bool-style spellings still work.
  • Identifiers use letters, digits and _, can't start with a digit or be a keyword, and are case-sensitive.
  • Avoid reserved names: leading underscores, standard library names, and str/mem/is/to prefixes followed by a lowercase letter.
  • Convention: snake_case for variables and functions, UPPER_SNAKE_CASE for macros and constants, prefixes for library-wide names.

Next: Variables, where you'll declare, initialize and use your first variables properly.