2.5 Constants, Variable and Data Type

Download as pdf or txt
Download as pdf or txt
You are on page 1of 28

C program Essentials

Devyani Soni
Department of Computer science and Engineering
Content

 Variables
 Constants
 Data type
Variable
 A variable in C language is the name associated with some memory location
to store data of different types. There are many types of variables in C
depending on the scope, storage class, lifetime, type of data they store,
etc.

 A variable is the basic building block of a C program that can be used in


expressions as a substitute in place of the value it stores.

Definition

“A variable in C is a memory location with some name that helps store


some form of data and retrieves it when required. We can store different
types of data in the variable and reuse the same variable for storing some
other data any number of times.”
C Variable Syntax

The syntax to declare a variable in C specifies the name and the type of the variable.

data_type variable_name = value; // defining single variable


or
data_type variable_name1, variable_name2; // defining multiple variable

Here,
 data_type: Type of data that a variable can store.
 variable_name: Name of the variable given by the user.
 value: value assigned to the variable by the user.
Example

int var; // integer variable


char a; // character variable
float fff; // float variables
 There are 3 aspects of defining a variable:

1. Variable Declaration
2. Variable Definition
3. Variable Initialization

1. C Variable Declaration
Variable declaration in C tells the compiler about the existence of the variable
with the given name and data type. When the variable is declared, an entry in
symbol table is created and memory will be allocated at the time of initialization
of the variable.
2. C Variable Definition
In the definition of a C variable, the compiler allocates some memory and some
value to it. A defined variable will contain some random garbage value till it is
not initialized.
Example

int var;
char var2;

3. C Variable Initialization
Initialization of a variable is the process where the user assigns some meaningful
value to the variable when creating the variable.
Example
int var = 10;
Rules for Naming Variables in C

You can assign any name to the variable as long as it follows the following rules:

1. A variable name must only contain alphabets, digits, and underscore.


2. A variable name must start with an alphabet or an underscore only. It cannot
start with a digit.
3. No white space is allowed within the variable name.
4. A variable name must not be any reserved word or keyword.
C Variable Types

The C variables can be classified into the following types:

1. Local Variables
2. Global Variables
3. Static Variables
4. Automatic Variables
5. Extern Variables
6. Register Variables
1. Local Variables in C
A Local variable in C is a variable that is declared inside a function or a block of
code. Its scope is limited to the block or function in which it is declared.

/ C program to declare and print local variable inside a function.

#include <stdio.h>
void function()
{
int x = 10; // local variable
printf("%d", x);
}
int main()
{ function(); }
2. Global Variables in C
 A Global variable in C is a variable that is declared outside the function or a
block of code. Its scope is the whole program i.e. we can access the global
variable anywhere in the C program after it is declared.
Example of Global Variable in C

#include <stdio.h>

int x = 20; // global variable

void function1() { printf("Function 1: %d\n", x); }

void function2() { printf("Function 2: %d\n", x); }

int main()
{

function1();
function2();
return 0;
}
3. Static Variables in C
 A static variable in C is a variable that is defined using the static keyword. It can
be defined only once in a C program and its scope depends upon the region
where it is declared (can be global or local).

 The default value of static variables is zero.

Syntax of Static Variable in C

static data_type variable_name = initial_value;

 As its lifetime is till the end of the program, it can retain its value for multiple
function calls as shown in the example.
#include <stdio.h>
void function()
{
int x = 20; // local variable
static int y = 30; // static variable
x = x + 10;
y = y + 10;
printf("\tLocal: %d\n\tStatic: %d\n", x, y);
// C program to
}
demonstrate use
int main() of static variable
{
printf("First Call\n");
function();
printf("Second Call\n");
function();
printf("Third Call\n");
function();
return 0;
}
Output:

First Call
Local: 30
Static: 40
Second Call
Local: 30
Static: 50
Third Call
Local: 30
Static: 60
4. Automatic Variable in C

 All the local variables are automatic variables by default. They are also known
as auto variables.
 Their scope is local and their lifetime is till the end of the block. If we need,
we can use the auto keyword to define the auto variables.
 The default value of the auto variables is a garbage value.

Syntax of Auto Variable in C

auto data_type variable_name;


or
data_type variable_name; (in local scope)
// C program to demonstrate use of automatic variable

#include <stdio.h>

void function()
{
int x = 10; // local variable (also automatic)
auto int y = 20; // automatic variable
printf("Auto Variable: %d", y);
}
int main()
{

function();
return 0;
}
5. External Variables in C

 External variables in C can be shared between multiple C files. We can


declare an external variable using the extern keyword.
 Their scope is global and they exist between multiple C files.

Syntax of Extern Variables in C

extern data_type variable_name;

#include <stdio.h>
void printValue()
{
printf("Global variable: %d", x);
}
6. Register Variables in C

 Register variables in C are those variables that are stored in the CPU register
instead of the conventional storage place like RAM. Their scope is local and
exists till the end of the block or a function.

 These variables are declared using the register keyword. The default value of
register variables is a garbage value.

Syntax of Register Variables in C

register data_type variable_name = initial_value;


#include <stdio.h>

int main()
{
register int var = 22; // register variable
printf("Value of Register Variable: %d\n", var);
// C program to
return 0; demonstrate the
} definition of
registervariable

Output:

Value of Register Variable: 22


Constant

 The constants in C are the read-only variables whose values cannot be


modified once they are declared in the C program. The type of constant can
be an integer constant, a floating pointer constant, a string constant, or a
character constant. In C language, the const keyword is used to define the
constants.

 Definition

“A constant in C is a variable that cannot be modified once it is declared in


the program. We can not make any change in the value of the constant
variables after they are defined.”
How to Define Constant in C?
 We define a constant in C language using the const keyword. Also known as a
const type qualifier, the const keyword is placed at the start of the variable
declaration to declare that variable as a constant.

Syntax to Define Constant

const data_type var_name = value;


// C program to illustrate constant variable definition

#include <stdio.h>
Int main()
{
const int int_const = 25; // defining integer constant using const keyword
const char char_const = 'A’; // defining character constant using const keyword
const float float_const = 15.66; // defining float constant using const keyword

printf("Printing value of Integer Constant: %d\n", int_const);


printf("Printing value of Character Constant: %c\n", char_const);
printf("Printing value of Float Constant: %f", float_const);

return 0;
}
Data Types

 In Programming, data type is an attribute associated with a piece of data that


tells a computer system how to interpret its value. Understanding data types
ensures that data is collected in the preferred format and that the value of
each property is as expected.

Definition:
“An attribute that identifies a piece of data and instructs a computer system on
how to interpret its value is called a data type.”

The data comes in different forms. Examples include:

your name – a string of characters


your age – usually an integer.
the amount of money in your pocket- usually decimal type
today's date - written in date time format
Types of data types

 There are 3 types of data types.


1. Primitives data types:
They are predefined data types that are independent of all other kinds and
include basic values of particular attributes, like text or numeric values.
They are the most fundamental type and are used as the foundation for more
complex data types. Most computer languages probably employ some
variation of these simple data types.

1. Composite Data types:


Composite data types are made up of various primitive kinds that are
typically supplied by the user. They are also referred to as user-defined or
non-primitive data types.
Composite types fall into four main categories: semi-structured (stores data
as a set of relationships); multimedia (stores data as images, music, or
videos); homogeneous (needs all values to be of the same data type); and
tabular (stores data in tabular form).
3. User-defined data type
A user-defined data type (UDT) is a data type that derived from an existing
data type. You can use other built-in types already available and create your
own customized data types.
End

You might also like