DEV Community

Cover image for Rules for Naming Variables in C Language
Mukesh Kumar
Mukesh Kumar

Posted on

Rules for Naming Variables in C Language

πŸŽ™οΈ Introduction

Hello everyone!

In this lesson, we will understand the Rules for Naming Variables in C Language.

In programming, variable names are not chosen randomly. C language follows specific rules for naming variables. If we do not follow these rules, the compiler will generate errors and the program...Read More

πŸ”Ή Rule 1: A Variable Name Must Start with a Letter or Underscore

In C, a variable name must begin with:

An alphabet (A–Z or a–z)
OR

An underscore (_)

For example:

age

total

_value

These are valid variable names.

However, a variable name cannot start with...Read More

πŸ”Ή Rule 2: Variable Names Cannot Start with a Digit

Although numbers can be used inside a variable name, they cannot be placed at the beginning.

For example:

marks1 βœ”

student2 βœ”

total2025 βœ”

But:

2025total ❌

This rule ensures clarity and avoids confusion...Read More

πŸ”Ή Rule 3: No Special Symbols Allowed

Variable names cannot contain special characters such as:

@

%

&

$

!

For example:

total@marks ❌

price#1 ❌

Only letters, digits, and underscore are...Read More

πŸ”Ή Rule 4: No Spaces in Variable Names

Variable names cannot contain spaces.

For example:

total marks ❌

student name ❌

If you need separation between words, you can...Read More

πŸ”Ή Rule 5: Variable Names Cannot Be Keywords

C has reserved keywords like:

int

float

return

if

while

char

These words already have predefined meanings in C.

You cannot use them as variable names...Read More

πŸ”Ή Rule 6: Variable Names Are Case-Sensitive

C is case-sensitive.

This means:

age

Age

AGE

All three are different variables.

So programmers must be careful while naming...Read More

πŸ”Ή Rule 7: Meaningful Variable Names Should Be Used

Although not a strict rule of the compiler, it is a good programming practice to use meaningful names.

Instead of:

x

a

b

Use:

totalMarks

studentAge

accountBalance

Meaningful names make programs easier to read...Read More

πŸ”Ή Rule 8: Length of Variable Name

C allows long variable names, but it is recommended to keep them reasonable and meaningful.

Very long names make code harder to read.

For example:

totalAmountOfStudentsInClass βœ” (but too long)...Read More

πŸ“Œ Quick Summary of Rules

Let’s quickly revise:

Must start with a letter or underscore.

Cannot start with a number.

No special characters allowed...Read More

Top comments (0)