ADVERTISEMENT
ADVERTISEMENT

Variables in C with Examples

In C programming, a variable is a named location in memory where a programmer can store a value. This value can be of various types, such as an integer, floating-point value, character, or string, which determines the size and layout of the variable's memory. The range of values that can be stored within that memory and the set of operations that can be applied to the variable.

Rules for Declaring Variables in C

  1. A variable name can only contain letters, digits, and underscores. It must begin with a letter or underscore.

  2. C is case-sensitive, so variables 'abc' and 'ABC' are considered different.

  3. A variable name cannot be a reserved word in C, such as 'int' or 'float'.

  4. A variable must be declared before it is used in the program.

Here are some examples of declaring variables in C:

int age;
float average;
char grade;
char name[30];

In these examples, 'age' is an integer variable, 'average' is a floating-point variable, 'grade' is a character variable, and 'name' is a string variable (an array of characters).

Examples of Valid Variables in C

int age;
float average_grade;
char _grade;
char first_name[30];

Examples of Invalid Variables in C

int 123abc;   // variable name cannot start with a digit
float float;  // float is a reserved word
char #grade;  // variable name cannot contain special characters
char first name[30];  // variable name cannot contain a space

 


ADVERTISEMENT

ADVERTISEMENT