DevAcademia
C++C#CPythonJava
  • C Basics

  • Introduction to C
  • Getting Started with C
  • C Syntax
  • C Output
  • C Comments
  • C Variables
  • C Data Types
  • C Constants
  • C Operators
  • C Booleans
  • C If...Else Statements
  • C Switch Statement
  • C While Loops
  • C For Loops
  • C Break and Continue
  • C Strings
  • C User Input
  • C Memory Address
  • C Pointers
  • C Files
  • C Functions

  • C Functions
  • C Function Parameters
  • C Scope
  • C Function Declaration
  • C Recursion
  • C Math Functions
  • C Structures

  • C Structures
  • C Structs & Pointers
  • C Unions
  • C Enums

  • C Enums
  • C Memory

  • C Allocate Memory
  • C Access Memory
  • C Reallocate Memory
  • C Deallocate Memory
  • C Structs and Memory
  • C Memory Example
  • C Quiz

  • C Quiz
  • C Basics

  • Introduction to C
  • Getting Started with C
  • C Syntax
  • C Output
  • C Comments
  • C Variables
  • C Data Types
  • C Constants
  • C Operators
  • C Booleans
  • C If...Else Statements
  • C Switch Statement
  • C While Loops
  • C For Loops
  • C Break and Continue
  • C Strings
  • C User Input
  • C Memory Address
  • C Pointers
  • C Files
  • C Functions

  • C Functions
  • C Function Parameters
  • C Scope
  • C Function Declaration
  • C Recursion
  • C Math Functions
  • C Structures

  • C Structures
  • C Structs & Pointers
  • C Unions
  • C Enums

  • C Enums
  • C Memory

  • C Allocate Memory
  • C Access Memory
  • C Reallocate Memory
  • C Deallocate Memory
  • C Structs and Memory
  • C Memory Example
  • C Quiz

  • C Quiz

Loading C tutorial…

Loading content
C BasicsTopic 12 of 64
←PreviousPrevNextNext→

Variable Names in C

Rules for Naming Variables

• Names must begin with a letter or underscore.

• They may include letters, numbers, and underscores but no spaces or special characters.

• They cannot be a C keyword (e.g., `int`, `if`, `return`).

• C is case-sensitive: `Age`, `age`, and `AGE` are all different.

• Avoid leading underscores in your own names—identifiers beginning with an underscore followed by an uppercase letter or another underscore are reserved for the implementation; a single leading underscore is reserved at file scope.

Valid NamesInvalid Names
count, student_age, _balance2count, my-variable, float

Keywords vs Identifiers

Keywords are reserved words with special meaning in C (e.g., `int`, `char`, `while`).

They cannot be used as variable names. A few common keywords: `auto`, `break`, `case`, `char`, `const`, `continue`, `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`.

Best Practices

• Use meaningful names (e.g., `studentScore` instead of `ss`).

• Follow a consistent naming style (snake_case or camelCase).

• Avoid overly long or cryptic names.

• Avoid names that differ only by case (e.g., `total` vs `Total`).

• Prefer `const` variables for typed constants and `#define` UPPER_SNAKE_CASE for macros.

• For portability, stick to ASCII letters, digits, and underscores in identifiers.

Common Naming Conventions

CategoryConventionExampleNotes
Local variables / paramslower_snake_casestudent_count
Functionslower_snake_casecompute_average()
Struct / enum namesPascalCase or snake_casestruct PlayerStatsPick one style per project
Constants (macros)UPPER_SNAKE_CASE#define MAX_SIZE 256Prefer const when possible
Typedef aliasesSuffix or PascalCasetypedef unsigned long usize;Avoid new names ending in _t for POSIX portability

Good vs Bad (with Comments)

Example
#include <stdio.h>

int main(void) {
    int student_count = 30;     // good: descriptive, snake_case
    int maxScore = 100;         // good: descriptive, camelCase
    int x1 = 5;                 // acceptable but vague

    // int 2items = 10;        // invalid: cannot start with a digit
    // int my-score = 10;      // invalid: hyphen not allowed
    // int float = 3;          // invalid: 'float' is a keyword

    printf("%d %d %d\n", student_count, maxScore, x1);
    return 0;
}
Output
30 100 5

Case Sensitivity Demonstration

C distinguishes identifiers by case. These are three different variables:

Example
#include <stdio.h>

int main(void) {
    int age = 18;
    int Age = 21;
    int AGE = 30;
    printf("age=%d Age=%d AGE=%d\n", age, Age, AGE);
    return 0;
}
Output
age=18 Age=21 AGE=30

Constants: const vs Macro

Prefer typed constants when possible; use macros for conditional compilation or when a true compile-time constant is required in preprocessor contexts.

Example
#include <stdio.h>

#define MAX_STUDENTS 32          // macro constant (no type)
const int pass_mark = 60;        // typed constant

int main(void) {
    int group_size = MAX_STUDENTS;
    printf("group=%d pass=%d\n", group_size, pass_mark);
    return 0;
}
Output
group=32 pass=60

Avoid Ambiguous Characters

Pick names that are easy to read aloud and in certain fonts:

• Avoid single-letter names like `l` (ell) or `O` that can be confused with `1` or `0`.

• Prefer `index` over `i` in long scopes; reserve short names for tiny loops.

Test your knowledge: Variable Names in C
Quiz Configuration
4 of 8 questions
Sequential
Previous allowed
Review enabled
Early close allowed
Estimated time: 5 min
C BasicsTopic 12 of 64
←PreviousPrevNextNext→