Module III
Arrays & Strings:
Topic:1
Arrays Basics:
It is a group of elements.
❖ The elements should be same data type.
❖ The elements must be continuously in memory.
❖ All the elements can be identified by a common name.
Declaration / Creation of array :-
Array declaration is as same as normal variable declaration only the difference is it
always ends with [size] .
Syntax :
Datatype ArrayName[ size ];
Example :
int a[100];
char ch[10];
float rate[50];
Index :- To indentify the elements of an array we have to use a +ve integer it is called
index. It is also called as sub script. It always begins with 0. It is called lower boundary.
It always ends with size-1. It is called upper boundary.
Accessing Array elements : We can access the array elements with the help of arrayName
and index.
III MODULE II PART
Storage Classes:
Every Variable in a program has memory associated with it.
Memory Requirement of Variables is different for different types of variables.
In C, Memory is allocated & released at different places
Storage class of variable Determines following things
Where the variable is stored
Scope of Variable
Default initial value
Lifetime of variable
Where the variable is stored:
Storage Class determines the location of variable, where it is declared. Variables declared
with auto storage classes are declared inside main memory whereas variables declared with
keyword register are stored inside the CPU Register.
Scope of Variable:
Scope of Variable tells compile about the visibility of Variable in the block. Variable may
have Block Scope, Local Scope and External Scope. A scope is the context within a computer
program in which a variable name or other identifier is valid and can be used, or within
which a declaration has effect.
Default Initial Value of the Variable:
Whenever we declare a Variable in C, garbage value is assigned to the variable. Garbage
Value may be considered as initial value of the variable. C Programming have different
storage classes which has different initial values such as Global Variable have Initial Value as
0 while the Local auto variable have default initial garbage value.
Lifetime of variable:
Lifetime of the = Time Of variable Declaration - Time of Variable Destruction Suppose we
have declared variable inside main function then variable will be destroyed only when the
control comes out of the main .i.e end of the program.
Different Storage Classes:
• Auto Storage Class
• Static Storage Class
• Extern Storage Class
• Register Storage Class
Automatic (Auto) storage class
This is default storage class
All variables declared are of type Auto by default
In order to Explicit declaration of variable use ‗auto„ keyword
auto int num1 ; // Explicit Declaration
Features:
Example:
void main()
auto mum = 20 ;
{ auto num = 60 ;
printf("nNum : %d",num);
printf("nNum : %d",num);
}
Note : Two variables are declared in different blocks , so they are treated as
different variables
External ( extern ) storage class in C Programming:
Variables of this storage class are ―Global variables‖
Global Variables are declared outside the function and are accessible to all
functions in the program
Generally , External variables are declared again in the function using keyword
extern In order to Explicit declaration of variable use ‗extern„ keyword
extern int num1 ; // Explicit Declaration
Features :
Example:
int num = 75 ;
void display();
void main()
extern int num ;
printf("nNum : %d",num);
display();
}
void display()
extern int num ;
printf("nNum : %d",num);
Static Storage Class:
The static storage class instructs the compiler to keep a local variable in
existence during the life- time of the program instead of creating and
destroying it each time it comes into and goes out of scope. Therefore, making
local variables static allows them to maintain their values between function
[Link] static modifier may also be applied to global variables. When this is
done, it causes that variable's scope to be restricted to the file in which it is
declared.
In C programming, when static is used on a class data member, it causes only
one copy of that member to be shared by all the objects of its class.
#include<stdio.h>
void func(void);
static int count = 5; /* global variable */
main()
while(count--)
func();
return 0;
}
void func( void )
static int i = 5; /* local static variable*/
i++;
printf("i is %d and count is %d\n", i, count);
Register Storage Class:
register keyword is used to define local variable.
Local variable are stored in register instead of RAM.
As variable is stored in register, the Maximum size of variable = Maximum Size
of Register unary operator [&] is not associated with it because Value is not
stored in RAM instead it is stored in Register.
This is generally used for faster access.
Common use is ―Counter―
Example:
#include<stdio.h>
int main()
int num1,num2;
register int sum;
printf("\nEnter the Number 1 : ");
scanf("%d",&num1);
printf("\nEnter the Number 2 : ");
scanf("%d",&num2);
sum = num1 + num2;
printf("\nSum of Numbers : %d",sum);
return(0);
User defined Data types: type definition and enumeration:
type definition:
A type definition, typedef, gives a name to a data type by creating a new type
that can then be used anywhere a type is permitted.
Its purpose is to redefine the name of an existing variable type.
The general syntax of the typedef is as follows,
typedef datatype IDENTIFIER;
where typedef is the keyword that tells the compiler about the type definition,
data_type refers to an existing data type and IDENTIFIER refers the “new”
name given to the data type.
Note that using typedef, we are not creating new data types.
Instead we are creating only new name for the existing data type.
These new data type names are called user-defined data types.
Suppose we want to store marks scored in various subjects in variables sub1,
sub2 and sub3.
These variables can be declared as follows,
int sub1, sub2, sub3;
Using the user-defined data types, the variables can be declared as shown
below, typedef int MARKS;
MARKS sub1, sub2, sub3;
//Example program to demonstrate typedef
Enumeration:
An enumeration is a user-defined data type that consists of integral constants.
To define an enumeration, keyword enum is used.
Syntax:
enum flag {const1, const2,……constN};
Here, name of the enumeration is flag. Constants like const1, const2, ............. ,
constN are values of type flag.
By default, const1 is 0, const2 is 1 and so on. You can change default values of
enum elements during declaration (if necessary).
// Changing the default value of enum elements enum
suit{
club=0;
diamonds=10;
hearts=20;
spades=3;
};
Declaration of enumerated variable:
Above code defines the type of the data but, no any variable is created.
Variable of type enum can be created as:
enum boolean{ false;
true;
};
enum boolean check;
Here, a variable check is declared which is of type enum boolean.
Example of enumerated type:
#include<stdio.h>
enum week{ sunday, monday, tuesday, wednesday, thursday, friday, saturday};
int main()
enum week today;
today=wednesday;
printf("%d day",today+1);
return 0;
Output:
4 day