Javatpoint Logo
Javatpoint Logo

Variables in C

A variable is the name of the memory location. It is used to store information. Its value can be altered and reused several times. It is a way to represent memory location through symbols so that it can be easily identified.

Variables are key building elements of the C programming language used to store and modify data in computer programs. A variable is a designated memory region that stores a specified data type value. Each variable has a unique identifier, its name, and a data type describing the type of data it may hold.

Syntax:

The syntax for defining a variable in C is as follows:

Here,

  • data_type: It represents the type of data the variable can hold. Examples of data types in C include int (integer), float (a floating-point number), char (character), double (a double-precision floating-point number),
  • variable_name: It is the identifier for the variable, i.e., the name you give to the variable to access its value later in the program. The variable name must follow specific rules, like starting with a letter or underscore and consisting of letters, digits, and underscores.

For example, to declare the integer variable age:

It declares an integer variable named age without assigning it a specific value. Variables can also be initialized at the time of declaration by assigning an initial value to them. For instance:

Here, the variable count is declared an integer and initialized with 0.

Note: Variables should be defined before they are used within the program. The scope of a variable determines where it is accessible. Variables declared inside a function or block are considered local variables, while declared outside any function are considered global variables.

Syntax:

Let us see the syntax to declare a variable:

An example of declaring the variable is given below:

Here, a, b, and c are variables. The int, float, and char are the data types.

We may also provide values while defining variables, as shown below:

Rules for defining variables

In C, Variable names must follow a few rules to be valid. The following are the rules for naming variables in C:

  • Allowed Characters:

Variable names include letters ( uppercase and lowercase ), digits, and underscores. They must start with a letter (uppercase or lowercase) or an underscore.

  • Case Sensitivity:

C is a case-sensitive programming language. It means that uppercase and lowercase letters are considered distinct.

For example, myVar, MyVar, and myvar are all considered different variable names.

  • Keywords:

Variable names cannot be the same as C keywords (reserved words), as they have special meanings in the language.

For example, you cannot use int, float, char, for, while, etc., as variable names.

  • Length Limitation:

There is no standard limit for the length of variable names in C, but it's best to keep them reasonably short and descriptive.

Some compilers may impose a maximum length for variable names.

  • Spaces and Special Characters:

Variable names cannot contain spaces or special characters (such as !, @, #, $, %, ^, &, *, (, ), -, +, =, [, ], {, }, |, \, /, <, >,., ?;, ', or ").

Underscores are the only allowed special characters in variable names.

  • Reserved Identifiers:

While not strictly a rule, it is advisable to avoid specific patterns or identifiers common in libraries or standard usage.

For example, variables starting with __ (double underscores) are typically reserved for system or compiler-specific usage.

Valid examples of variable names:

Invalid examples of variable names:

Following these rules ensures that your variable names are valid and conform to the C language's syntax and conventions. Choosing meaningful and descriptive names for variables is essential to enhance the readability and maintainability of your code.

The three components of declaring a variable

Let us explain the three aspects of defining a variable: variable declaration, variable definition, and variable initialization, along with examples.

1. Variable Declaration:

The process of telling the compiler about a variable's existence and data type is known as variable declaration. It notifies the compiler that a variable with a specific name and data type will be used in the program. Still, no memory for the variable is allocated at this moment. It is usually seen at the start of a function or block before the variable is utilized.

The general syntax for variable declaration is

Example of variable declaration:

2. Variable Definition:

The process of reserving memory space for the variable to keep its contents during program execution is known as a variable definition. It is based on the data type and connects the variable name with a particular memory address of sufficient size.

A variable in C can be declared and defined in the same statement, although they can also be separated if necessary.

Example of variable definition:

3. Variable Initialization:

Variable declaration is the act of informing the compiler about the existence and data type of a variable. It informs the compiler that a variable with a specific name and data type will be used in the program, but that memory for the variable still needs to be allocated.

Not explicitly initialized variables will contain garbage/random data that may result in unexpected program behavior.

Example of variable initialization:

In the example above, we have variable age, salary, and initial declarations. After that, we define these variables and initialize them with initial values (e.g., age = 25). Later in the program, we can modify the values of these variables (e.g., age = 30).

Types of Variables in C

There are many types of variables in c:

  1. local variable
  2. global variable
  3. static variable
  4. automatic variable
  5. external variable

Local Variable

A variable that is declared inside the function or block is called a local variable.

It must be declared at the start of the block.

You must have to initialize the local variable before it is used.

Global Variable

A variable that is declared outside the function or block is called a global variable. Any function can change the value of the global variable. It is available to all the functions.

It must be declared at the start of the block.

Static Variable

A variable that is declared with the static keyword is called static variable.

It retains its value between multiple function calls.

If you call this function many times, the local variable will print the same value for each function call, e.g, 11,11,11 and so on. But the static variable will print the incremented value in each function call, e.g. 11, 12, 13 and so on.

Automatic Variable

All variables in C that are declared inside the block, are automatic variables by default. We can explicitly declare an automatic variable using auto keyword.

External Variable

We can share a variable in multiple C source files by using an external variable. To declare an external variable, you need to use extern keyword.

myfile.h

program1.c

Conclusion:

Variables are critical components in C that store and manipulate data in the memory. They act as named placeholders for memory regions, making identifying and retrieving stored data simple. The syntax for creating a variable in C includes the following:

  • Declaring the data type.
  • Specifying the data that the variable may carry.
  • Giving the variable a unique name.

Following specific rules, such as starting with a letter, underscore, and avoiding reserved keywords, ensures valid variable names. Variables can be declared without initialization or assigned an initial value at the time of declaration. Once the variable is declared, variables can be used throughout the program to store and manipulate data. They can be reassigned with new values as needed during program execution.


Next TopicData Types in C



Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA