×

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

Library Functions in C

What are library functions? Library functions are the built-in functions (functions provided by the system) which are stored in the library. The library is the common location where these functions are...

3 minutes read.

Factorial Program in C using For Loop

Before move on the factorial program. We have to know about the for loop in C programming language. The syntax of the For Loop is: for (initialization statement; test expression; an increment...

3 minutes read.

Commenting in C

Commenting in C Commenting in the C language is used to give out the information about the lines of the code which are included. It is one of the things that...

3 minutes read.

GCD program in C

C language : Dennis Ritchie developed the general-purpose computer language C at Bell Laboratories in 1972. Despite being an ancient language, it is extremely popular. It is among the most widely used...

4 minutes read.

Enumeration (Enum) in C

Enumeration (Enum) in C: In the C programming language, the enum is referred to as an enumerated type. Enum is a user defined data type consisting of integer values and...

3 minutes read.

Floor() Function in C

Floor() function is a built-in function in C which is defined in the math.h header file. This function is used to return the nearest integer value which is less than...

4 minutes read.

While Loop Syntax in C

What is Loop? The statements in the sequence are repeatedly executed via looping statements in C until the condition is met. The body of a loop and a control statement make...

4 minutes read.

Multithreading in C interview questions

Q1. What exactly is a thread? Ans: A thread is a brief sequence of instructions intended to be planned and carried out by the CPU apart from the parent process. Q2. Can...

4 minutes read.

Bit Stuffing Program in C

What is a Bit Stuffing? The process of inserting one or more extra bits (0) into data is known as bit stuffing. To provide signalling information to a recipient, these are...

3 minutes read.

Pointer to pointer in C

A pointer to another pointer is another type of multiple indirections and a chain of many pointers. Generally, a pointer consists of the address of the variable. Once a pointer to...

4 minutes read.

Integer Promotions in C

As we know that some of the data types such as char, short int, Enum takes a smaller number of bytes than compared to int. When an operation is applied...

3 minutes read.

Infix to Postfix program in C

Infix Expression: In infix expression, an operator is placed between the two operands. Example: x + y, here operator + is placed between operands x and y. Postfix Expression: In...

3 minutes read.

Find out Power without using POW function in C

Introduction: In this discussion, we will discuss how we find out power without using the POW function in C. In this article, you'll understand how to compute integer powers in...

3 minutes read.

Isalnum() function in C

Introduction: The isalnum () is a function used in C programming language. This function checks the passing number or argument is an alphanumeric number or not. The alphanumeric number consists of the...

3 minutes read.

Character Set in C

Introduction Every language has some basic elements which are used to represent information. The English language includes alphabets which together shape a sentence, after which the sentences are used to shape...

3 minutes read.

C Program for Mean and Median of an Unsorted Array

In this tutorial, we will look at how to determine the mean and median of a given unsorted array. To determine the Mean: To get the average, mean is determined. The formula...

2 minutes read.

Ferror() in c

Ferror():  In C, the ferror() function checks assuming there is a blunder in the given stream. The ferror() function is utilized to check for the document blunder on given stream. A return...

4 minutes read.

Socket Programming in C

Socket programming is a process that connects the two or more nodes on a networking communication with respective to each other. One of the sockets that determines the socket (I.e.,...

5 minutes read.

Advantages of Dynamic Memory Allocation in C

Dynamic Memory Allocation Dynamic memory allocation is a process of assigning or allocating memory to a variable during the execution of a program. Dynamic memory allocation provides storage according to the...

3 minutes read.

Array Example in C

An array is a collection of similar types of data elements arranged in such a way that any number of values can be assigned to it. It can store values that...

4 minutes read.