C Foundation

What is C? C Compiler Installation C Extensions C Compiler C Interpreter C Program Structure

C Basics

C Keywords C Data Types C Identifiers C Variables C Constant C Escape Sequences C Constant and Volatile C Typecast

Operators

What is Operator C Comma Operator C Arithmetic Operators C Relational Operators C Logical Operators C Bitwise Operators C Conditional Operators C : : Operator C Operator Priority

Basic IO's

Basic IO's C Formatted Functions C Unformatted Functions C Common Functions

Control Statements

What is Control Statement C if Statement C if else Statement C Nested if Statement C Else if Statement C Break Statement C Continue Statement C Switch Statement C Goto Statement

Looping

What is Control Loop C for Loop C Nested for Loop C while Loop C Nested while Loop C do while Loop C Nested do while loop

Functions

What is Function C User Defined Functions C Recursion C Passing Parameters

Scope

Scope C Local Scope C Global Scope

Storage Classes

What is Storage Class C Auto C Extern C Static C Register

Array

What is Array C One Dimensional Array C Two Dimensional Array C Multi Dimensional Array C Arrays Of Strings

String

What is String C String Functions

Pointer

What is Pointer C Pointers Arithmetic C Pointer to Pointer C Pointers and Arrays C Pointers and Strings C Pointer to Functions Void Pointers Null Pointers C Null and Void Pointer

Structure

What is Structure C Struct within Struct C Array within Structure C Pointer to Structure C Structure and Function C Enum C Bitfield Structure C Type def

Union

What is Union

Files

What is File C read a file C write a file C File Handling C Error Handling C Low Level Disk I/O C Other file functions

Memory Allocation

What is Memory Allocation C Malloc() C Calloc() C Free() C Realloc() C Coreleft()

C Reference

All ASCII Code Basic C Questions

C Interview

C Interview Sets All Star Patterns All Number Patterns All Alphabet Patterns All Series Patterns
The ones who are crazy enough to think they can change the world are the ones who do.
- Steve Jobs

Constants and Volatile

Constants

Things which are all unchangable are said to be constant whereas things which are all changable are said to be volatile. The following diagram clearly represents the relationship between constant and volatile.

In the following diagram sealed box contains a variable i which is initialized by 16 at the time of declaration which indicates that one cannot change the value of variable i after initialization whereas opened box contains a varibale j which is intialized by 30 at the time of declaration, open box indicates the one can change the value of variable j at anytime.

Constant vs Volatile in C

Constant vs Volatile

The following table represents the constant against volatile

Constant Volatile
Constant variables are unchangable. Volatile variables are changable.
Constant variable can be created by using the keyword const. Volatile varibale can be created by using the keyword Volatile.
For example const int i = 16 is the way of declaring and initializing constant variable. For example volatile int j = 30 is the way of declaring and initializing volatile variable.
Constant variable can only be initialized at the time of declaration. Volatile varibale can be initialized at anytime.
By default, all variables are not constant. By default, all variables are volatile.
The const variable is otherwise known as Read Only Variable. The volatile variable is otherwise known as Read/Write Variable.
Another way to achieve constant variable by the use of #define preprocessor directive e.g.) #define a 5. Another way to achieve volatile variable by the use of nothing e.g.) int a = 5 is volatile.

Constant Program Using Keyword

Let's write a C program to demonstrate the purpose of constant variable

constant.c
#include <stdio.h>
int main()
{
const int i = 16;
i = i + 1;
printf("The value of i = %d ",i);
return 0;
}

Note:

Variable i is constant here. Thus, incrementing its value by any number is impossible throughout the entire program. So the above program will cause an error.

Constant Program Without Using Keyword

Let's write a C program to demonstrate the purpose of constant variable without using keyword.

noconstant.c
#include <stdio.h>
#define i 16
int main()
{
printf("The value of i = %d ",i);
return 0;
}
  • The value of i = 16

Note:

Variable i is constant here because we defined using preprocessor directive. Thus, incrementing its value by any number is impossible throughout the entire program.

Volatile Program Using Keyword

Let's write a C program to demonstrate the purpose of volatile variable

volatile.c
#include <stdio.h>
int main()
{
volatile int j = 1;
j = j + 1;
printf("The value of j = %d ",j);
return 0;
}
  • The value of j = 2

Note:

Variable 'j' is declared as volatile variable. Thus, incrementing its value by any number is possible throughout the entire program.

Volatile Program Without Using Keyword

Let's write a C program to demonstrate the purpose of volatile variable

novolatile.c
#include <stdio.h>
int main()
{
int j = 1;
j = j + 1;
printf("The value of j = %d ",j);
return 0;
}
  • The value of j = 2

Note:

Variable 'j' is volatile because by default all variables are volatile. Thus, incrementing its value by any number is possible throughout the entire program.

Report Us

We may make mistakes(spelling, program bug, typing mistake and etc.), So we have this container to collect mistakes. We highly respect your findings.

Report