×

Variable in C

Variable is an identifier that holds data in memory. It is used to identify input data in a program. The value of the variable can change at the time of executions. Let’s consider the syntax of a variable.
Syntax: Datatype variable_name;
Syntax: Datatype  variable_name1, variable_name2,variable_name3…variable nameN.
We can also declare multiple variables with the same data type by using comma (,). Example:
int a, b; // a and b are variable and int is the data type.
char c; // c is a variable and char is the data type.
Rules for declaring the variable:
  • Variable name should start with alphabets or underscore (_).
  • No space is allowed in a variable while declaration.
  • Variable must not be any reserved word or keyword.
Let’s see some example of how to declare the variables.
Valid declaration  Invalid  declaration
int  b; int 2;
char _name; int  a b;
int a4; int  double;

Types of variables in C.
There are various types of variable in C:
  1. local variable
  2. global variable
  3. static variable
  4. automatic variable
  5. external variable
1. Local variable: A local variable is declared inside of the function or block. It must be declared at the start of the block. Example:
void function1()
{
float x= 34.9; // Here x is local variable.
}
2. Global variable A global variable is declared outside of the function or block. The value of the global variable can be changed.
char name=”ABC”; // global variable
void function1()
{
int x=10; // local variable
}
3. Static variable A static variable is declared with the static keyword. It contains its value during the function call. Example:
void function1(){
int x=5;//local variable will print the same value.
static int y=5; //static variable will print the incremented value like: 5,6,7,...N
x=x+1;
y=y+1;
printf("%d,%d",x,y);
}
4. Automatic variable An automatic variable is declared inside the block. The auto keyword is used to declare the automatic variables.
void main()
{
int a=5;// Here a is local variable or also automatic variable.
auto int b=10;//automatic variable
}
5. External variable An external variable is used to share the multiple C source files by using external variables // save it as abc.h
extern int x=5;  //external variable (also global)
//save as prog1.c
#include "myfile.h"
#include <stdio.h>
void printValue()
{
printf("Global variable: %d", global_variable);
}

Related Topics

Passing Array to Function in C

Need to pass Arrays? The need to pass arrays to a function arises when we need to pass a list of values to a given function. During the course of our programming...

4 minutes read.

Armstrong Number in C

The Armstrong number is defined as the sum of each of its digits to the power of the number base for the each given number with any given number base....

3 minutes read.

Dangling pointers in C

Dangling pointers in C: A pointer is a variable that stores the memory address of other variables. The pointers may also store the address of other’s memory items. They are...

4 minutes read.

#include in C

In the C programming language, #include is another way of inferring a standard, or a user defines file into the application or program. The preprocessor of the programming standard generally...

4 minutes read.

Execution flow of C program

There are various steps of the execution of the C program that is given below. The following step of the execution Write source codes Preprocess Compile Link edit Load Execute Editor or...

1 minute read.

Explain the Increment and Decrement Operators in C

In the C programming language, the increment operator (++) and the decrement operator (--) are used to increase or decreasing a variable’s value by 1, respectively. The increment operator is written...

10 minutes read.

Remove an element from an array in C

A collection of objects or pieces of the same data type stored in a single memory block is known as an array. A data structure called an array is used...

3 minutes read.

Find reverse of an array in C using functions

Introduction: Here, we describe how to find the reverse array in C using functions. Suppose you have an array with n elements. I need to display the elements present in...

3 minutes read.

Pointer in C

Pointer is a variable that is used to contain the address of another variable. We can easily create a pointer in C language. Example: int *ptr Symbol Description & (ampersand sign) Address of an operator...

1 minute read.

C program to Store Information of Students Using Structure

What is the Structure in C? User-defined data types include structures. Structures aid your ability to combine things of various categories into a single group. Like arrays, it operates similarly. A...

3 minutes read.

fgets() function in C

fgets() function is present in standard input and output library i.e, stdio.h library. It is a built-in function or pre-define function. It is used to read the specified stream from...

4 minutes read.

Prototype in C

A prototype is nothing but a model, a model of initial creation of an intended product. Similarly, in the C programming language, all functions have a prototype. In general, all...

4 minutes read.

Unformatted input() and output() function in C

Unformatted Input and Output functions take the character, character array, and strings as input. We can read-only character data types with these functions. These functions are already defined in the...

5 minutes read.

C and C++ Binary Files

What is a binary file? A file in which the content is written in binary format is called a binary file. A binary file is not a text file. There are...

7 minutes read.

Volatile in C

Introduction A volatile keyword is a qualifier in C. Qualifiers are nothing but keywords which are used to modify the properties of a variable. Qualifiers are of two types: 1) Const The const type...

3 minutes read.

Format Specifier in C

Format Specifier in C Format specifiers can be described as an operator that is used to print the data referred by any object or variable in combination with the printf ()...

3 minutes read.

Multiplication Table in C

Displaying table up to 10 #include <stdio.h> int main() {     int num, i = 1;     printf("     Enter any Number:");     scanf("%d", &num);     printf("Multiplication table of %d: ", num);  ...

1 minute read.

Heap Sort in C

In this tutorial, we will learn about heap sorting in C language, but before going to Heap sort, we have to know the concept of Complete Binary Tree. What is Complete...

8 minutes read.

Local Labels in C

Anyone who has written programs in the C programming language is required to be familiar with the "go to" and "labels" used in C to navigate between functions. "Local labels"...

4 minutes read.

C String Manipulation programs without using Library Functions

C string manipulation programs without using Library Functions The string is the character list, and typically we operate with library functions to read and print the entire string, but here you...

6 minutes read.