C

Variables In C – The Most Detailed

A variable is a memory location’s name. It is used to store information. Its value can be changed and reused multiple times. It is a method of representing a memory location with a symbol so that it can be easily identified. The following article will share about variables in C.

What Do Variables Mean in C?

A variable in the C programming language is a user-defined or user-readable custom name assigned to a memory location. Variables store a value that can be changed and reused multiple times during program execution.
A variable can be an alphabet or digits that begin with an underscore, but a keyword cannot be declared as a variable name.
Now that you’ve grasped the fundamental definition of Variables in C, proceed to the declaration section.

Variable Declaration Demonstration Program in C

To use data in the program, we must declare and assign a variable of the appropriate data type. The data is then accessed using that variable name.
When a variable is declared, no memory space is allocated to it. It occurs only when the variable is initialized.
So, what if we declare a variable but never initialize it? When we declare a variable without first initializing it, it just stores some garbage or a value of zero. If we assign a value to it, the previous value will be overwritten. To better understand the preceding concept, consider the following example.

// Program to illustrate the declaration of variables in C
#include <stdio.h>
int main()
{
    // declaring alpha variable with value 10.
    int alpha = 10;
    // declaring beta variable with value 20.
    int beta = 20;
    // declaring c variable without initializing it.
    int c=0;
    // Checking the initial value of c variable.
    printf("The initial value of c is %d\n", c);
    c= alpha + beta;
    // printing C variable value.
    printf("The sum of alpha and beta is %d", c);
    return 0;
}

Output:

The initial value of c is 0
The sum of alpha and beta is 30

The above program shows that the initial value of c is 0. And when we reassign the new value to C variable, the old value is overwritten.

Variable Types in C

In c, there are numerous types of variables:

Variable located locally

A variable declared within a function or block is referred to as a local variable.
It has to be declared at the beginning of the block.

  1. void function1(){
  2. int x=10;//local variable
  3. }

The local variable must be initialized before it can be used.

The Global Variable

A global variable is one that is declared outside of a function or block. The value of the global variable can be changed by any function. It is accessible to all functions. It has to be declared at the beginning of the block.

  1. int value=20;//global variable
  2. void function1(){
  3. int x=10;//local variable
  4. }

Variable that remains constant

A static variable is one that is declared with the static keyword.
It keeps its value even after multiple function calls.

  1. void function1(){
  2. int x=10;//local variable
  3. static int y=10;//static variable
  4. x=x+1;
  5. y=y+1;
  6. printf(“%d,%d”,x,y);
  7. }

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

Declaration of User Defined Types

A user-defined type declaration is one in which the data type is defined by the user.
Some of the most common data types are struct, Union, enum, typedef, and so on.
Structure Structures are used to combine different types of data into a single user-defined data type.
Union Unions are user-defined data types whose members share a common memory location, allowing any of them to be accessed at the same time. When we only need to access one member, we use Union.
Typedef To define the data type, we must use the keyword typedef. As shown below, we can now use those new data types in our program.
As an example,

typedef char person_name;
typedef int person_age;
typedef float salary;

We’ve created a new data type called person name, as well as person age and salary. Now we can declare variables using these data types, as shown below.

person_name Akhil, Bhanu, Chaitanya;
person_age 22, 23, 24;
salary 22.22, 23.23, 24.24;

Akhil, Bhanu, and Chaitanya are declared as char type variables, while 22, 23, and 24 are declared as int type variables and 22, 23, 24, respectively.
We can create our own data types by using user-defined data types. For example, we can create a new data type called person info to store a person’s name, age, and salary. It also improves the program’s readability.
The primary distinction between primary type declaration and user-defined type declaration is that in the former, we can assign any variable name to any data type. However, we can use any identifier to any data type in the user-defined type declaration.

Following Steps

Your next topic could be “Data Structures in C.” You’ve already learned about variables in the C programming language. The following fundamentals will be data structures and the various data structures used for various purposes.
If you want to start a career in software development, check out Simplilearn’s Courses, which will provide you with the work-ready software development training you need to succeed today. Are you looking for a more in-depth training program in today’s most in-demand software development skills, tools, and languages? If so, our Post Graduate Program in Full Stack Development may be just what you need to advance your career. Investigate the course and enroll as soon as possible.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button