Professional Documents
Culture Documents
Storage Classes are used to describe the features of a variable/function. These features basically
include the scope, visibility and life-time.
o Visibility
o Memory Location
o Default value
The auto storage class is the default for variables declared inside a block.
The visibility of the automatic variables is limited to the block in which they are defined.
The scope of the automatic variables is limited to the block in which they are defined.
The allocated memory to an auto variable is freed after the exit block.
By default, every local variable is automatic in the C program.
Example:
#include<stdio.h>
int main()
Or
#include<stdio.h>
int main()
int a = 10; // usually all the local variables are auto storage class variables
Therefore, making local variables static allows them to maintain their values between function calls.
The visibility of the local static variables is only within the defined block.
A similar static variable can be declared many times but can be assigned only once.
A static integral variable is initialized to 0, otherwise null.
The visibility of the static global variables is within the file in which they are declared.
The 'static' keyword is used to define the static variable.
Example1:
#include<stdio.h>
static char c;
static int i;
static float f;
void main()
{
printf(“%d %d %d”, c,i,f);
}
Example2:
#include<stdio.h>
void sum()
{
static int a = 9;
static int b = 10;
printf(“%d %d”, a,b);
a++;
b++;
}
void main()
{
int i;
for(i=0; i<3; i++)
{
Sum();
}
}
The 'Register' storage class variables are store into the CPU Registers.
Usage of the ‘&’ operator on the register variable is restricted.
This storage class helps to access the variables faster than the other ‘Auto’ variables.
Although the register storage class is used, it is the compiler’s choice to store the variables in the
register.
Pointer variables can also be stored in the register.
Static variables cannot be stored in the CPU Registers.
Example1:
#include<stdio.h>
int main()
register int a = 0;
printf(“%p”,&a);
Example2:
#include<stdio.h>
‘extern’ keyword notifies the compiler that the variable declared has an external linkage (it may be defined in
another file)
‘extern’ variables are usually not allocated with any memory.
The default initial value is ‘0’, and it can be initialized as only a global variable.
It can be declared multiple times but can be initialized only once.
The compiler notifies an error for uninitialized extern variables
1.
#include<stdio.h>
int main()
extern int a;
printf(“%d”,a);
2.
#include<stdio.h>
int a;
int main()
extern int a;
printf(“%d”,a);
}
3.
#include<stdio.h>
int a;
int main()
extern int a = 0;
printf(“%d”,a);
}
4.
int a = 20;
extern int a;
int a= 10;
#include<stdio.h>
int main()
printf(“%d”,a);
int main()
int a;
printf("%p",&a);
The compilation is a process of converting the source code into object code. It is done with the help of
the compiler.
Pre-processor:
The source code is the code which is written in a text editor and the source code file is given an extension ".c".
This source code is first passed to the pre-processor, and then the pre-processor expands this code. After
expanding the code, the expanded code is passed to the compiler.
Compiler:
The code which is expanded by the pre-processor is passed to the compiler. The compiler converts this code
into assembly code. Or we can say that the C compiler converts the pre-processed code into assembly code.
Assembler:
The assembly code is converted into object code by using an assembler. The name of the object file generated
by the assembler is the same as the source file. The extension of the object file in DOS(disk operating system) is
'.obj,' and in UNIX, the extension is 'o'. If the name of the source file is 'hello.c', then the name of the object file
would be 'hello.obj'.
Linker:
Mainly, all the programs written in C use library functions. These library functions are pre-compiled, and the
object code of these library files is stored with '.lib' (or '.a') extension.
The main working of the linker is to combine the object code of library files with the object code of our
program. Sometimes the situation arises when our program refers to the functions defined in other files; then
linker plays a very important role in this.
It links the object code of these files to our program. Therefore, we conclude that the job of the linker is to link
the object code of our program with the object code of the library files and other files.
The output of the linker is the executable file. The name of the executable file is the same as the source file but
differs only in their extensions.
In DOS, the extension of the executable file is '.exe', and in UNIX, the executable file can be named as 'a.out'.
For example, if we are using printf() function in a program, then the linker adds its associated code in an output
file.
Question 4. What is segmentation fault
Segmentation faults in C or C++ is an error that occurs when a program attempts to access a memory
location it does not have permission to access. Generally, this error occurs when memory access is
violated and is a type of general protection fault.
Function pointers in C are variables that can store the memory address of functions and can be used in a
program to create a function call to functions pointed by them.
Syntax
#include <stdio.h>
return a*b;
#include <stdio.h>
int main()
float gain(float,float);
float (*fp)(float,float);
fp=&gain;
float result;
result = gain(5,4.5);
result=(*fp)(5,2.5);
return a*b;
The const type qualifier is used to create constant variables. When a variable is created with const keyword,
the value of that variable can't be changed once it is defined. That means once a value is assigned to a constant
variable, that value is fixed and cannot be changed throughout the program.
We use the following syntax to create constant variable using const keyword.
For example, the variable which is used to store system clock is defined as a volatile variable. The value of this
variable is not changed explicitly in the program but is changed by the clock routine of the operating system.
The volatile keyword prevents the compiler from performing optimization on code involving volatile objects,
thus ensuring that each volatile variable assignment and read has a corresponding memory access.
Bitwise operator first converts the integer into its binary representation then performs its operation. Bitwise
operators subsist of two digits, either 0 or 1. Some of the bitwise operators are (&, | , ^, ~)
Name of How it is
Operator What it does
Operator used
bitwise AND operator do AND of every corresponding bits of both operands and output 1 (true) if both
& bitwise AND a&b
operands have 1 at that position otherwise 0(false).
| bitwise OR a|b
Name of How it is
Operator What it does
Operator used
bitwise OR operator do OR operation of every corresponding bits of both operands and output 0 (false) if
both operands have 0 at that position otherwise 1(true).
bitwise
~ ~a
complement performs complement operation on an operand and bitwise complement changes 1 to 0 and 0 to 1
bitwise exclusive
^ a^b
OR returns 1 if the corresponding bits of two operands are opposite else 0
Shift operators:
Operator Name of Operator What it does
<< shift left shifts the number of bits to the left side
>> shift right shifts the number of bits to the right side
Example:
a = 5, b = 6
Left Shift Operator: Left Shift Operator performs operations on the binary bits. The left shift operator is a
type of binary operator so we need two operands to shift the position of the bits to the left side and add
zeroes to the empty space on the right side after shifting the bits.
Example a<<1
a=9(decimal)
0001001 0
Result =18
Or
The output of the left shift operator, will be equivalent to multiplying varName with 2 ^ no_of_position (2
raised to power no_of_position)
a<<2
Output: 100
Right Shift Operator: Right Shift Operator performs operations on the binary bits. The Right shift operator
is a type of binary operator so we need two operands to shift the position of the bits to the right side and
add zeroes to the empty space on the left side after shifting the bits.
Example : a>>1
a=9(deimal)
Result =4
Or
The output of the right shift operator, will be equivalent to dividing varName with 2^no_of_position (2
raised to power no_of_position)
a >> n = a / (2 ^ n)
Output: 6
ISR examines an interrupt and determines how to handle it executes the handling, and then returns a logical
interrupt value. If no further handling is required the ISR notifies the kernel with a return value.
Programs
Question 12.
#include <stdio.h>
int main()
float gain(float,float);
float (*fp)(float,float);
fp=&gain;
float result;
result = gain(5,4.5);
result=(*fp)(5,2.5);
return a*b;
Fibonacci series, next number is the sum of previous two numbers for example 0, 1, 1, 2, 3, 5,
8, 13, 21 etc.
#include<stdio.h>
int main()
{
int n1=0,n2=1,n3,i,number;
printf("Enter the number of elements:");
scanf("%d",&number);
printf("\n%d %d",n1,n2);//printing 0 and 1
for(i=2;i<number;++i)//loop starts from 2 because 0 and 1 are already printed
{
n3=n1+n2;
printf(" %d",n3);
n1=n2;
n2=n3;
}
return 0;
}
Question 13. Write a program to find Largest & smallest element in array
#include<stdio.h>
int main()
scanf("%d", &size);
scanf("%d", &a[i]);
big = a[0];
big = a[i];
if(small>a[i])
small = a[i];
return 0;
Question 14. Write a program to find odd or even number using bitwise operator
#include<stdio.h>
int main()
int number;
scanf("%d", &number);
else
return 0;
#include <stdio.h>
int main ()
if (*c == 0x10)
else
return 0;
int main ()
int *ptr = p;
return 0;
a. Half Pyramid of “ * ”
#include <stdio.h>
int main()
int i, j, rows;
scanf("%d", &rows);
printf("* ");
printf("\n");
return 0;
int main() {
int i, j, rows;
scanf("%d", &rows);
printf("\n");
return 0;
#include <stdio.h>
int main()
{
int i, j;
char input, alphabet = 'A';
printf("Enter an uppercase character you want to print in the last row: ");
scanf("%c", &input);
for (i = 1; i <= (input - 'A' + 1); ++i)
{
for (j = 1; j <= i; ++j) {
printf("%c ", alphabet);
}
++alphabet;
printf("\n");
}
return 0;
}
////////////////////////////////////////////////////////////////
#include <stdio.h>
int main() {
int i, space, rows, k = 0, count = 0, count1 = 0;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i) {
for (space = 1; space <= rows - i; ++space) {
printf(" ");
++count;
}
while (k != 2 * i - 1) {
if (count <= rows - 1) {
printf("%d ", i + k);
++count;
} else {
++count1;
printf("%d ", (i + k - 2 * count1));
}
++k;
}
count1 = count = k = 0;
printf("\n");
}
return 0;
}
/////////////////////////////////////////////////////////////////
#include <stdio.h>
int main() {
int rows, i, j, space;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = rows; i >= 1; --i) {
for (space = 0; space < rows - i; ++space)
printf(" ");
for (j = i; j <= 2 * i - 1; ++j)
printf("* ");
for (j = 0; j < i - 1; ++j)
printf("* ");
printf("\n");
}
return 0;
}
Question 18. Write a C program to display all the prime numbers between 1 and n is a value given
by the user at run time.
#include<stdio.h>
void main()
scanf("%d", &n);
for(num = 1;num<=n;num++)
count = 0;
for(i=2;i<=num/2;i++)
if(num%i==0)
count++;
break;
}
if(count==0 && num!= 1)
printf("%d ",num);
Question 19. Write a C program to Set a bit, Clear a bit, Toggle a bit
#include<stdio.h>
int main()
{
int num, n;
printf("Enter the number");
scanf("%d",&num);
printf("Enter the position to shift");
scanf("%d",&n);
printf(" The vale of num after shifting nth position %d", num);
return 0;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
#include<stdio.h>
int main()
{
int num, n;
printf("Enter the number");
scanf("%d",&num);
printf("Enter the position to shift");
scanf("%d",&n);
printf(" The vale of num after shifting nth position %d", num);
return 0;
}
///////////////////////////////////////////////////////////////////////////////////////////
#include<stdio.h>
int main()
{
int num, n;
printf("Enter the number");
scanf("%d",&num);
printf("Enter the position to shift");
scanf("%d",&n);
printf(" The vale of num after shifting nth position %d", num);
return 0;
}
#include<stdio.h>
int main()
{
char result[50];
float num = 23.34;
sprintf(result, "%f", num);
printf("\n The string for the num is %s", result);
getchar();
}
STACK:
A stack is a linear data structure that follows the principle of Last In First Out (LIFO).
This means the last element inserted inside the stack is removed first.
stack can be defined as a container in which insertion and deletion can be done from
the one end known as the top of the stack.
A Stack is an abstract data type with a pre-defined capacity, which means that it can
store the elements of a limited size.
It is a data structure that follows some order to insert and delete the elements, and
that order can be LIFO or FILO.
Stack operation
#include<stdio.h>
#include<stdlib.h>
#define MAX 5
int top=-1,stack[MAX];
void push();
void pop();
void display();
void main()
{
int ch;
while(1) //infinite loop, will end when choice will be 4
{
printf("\n*** Stack Menu***");
printf("\n\n 1.Push\n 2. Pop\n 3.Display\n 4.Exit");
printf("\n\nEnter your choice(1-4):");
scanf("%d", &ch);
switch(ch)
{
case 1: push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(0);
default: printf("\nWrong Choice!!");
}
}
}
void push()
{
int val;
if(top==MAX-1)
{
printf("\nStack is full!!");
}
else
{
printf(" \n Enter element to push: ");
scanf("%d", &val);
top=top+1;
stack[top]=val;
}
}
void pop()
{
if(top==-1)
{
printf("\nStack is empty!!");
}
else
{
printf("\nDeleted element is %d", stack[top]);
top=top-1;
}
}
void display()
{
int i;
if(top==-1)
{
printf("\nStack is empty!!");
}
else
{
printf("\nStack is...\n");
for (i=top;i>-0;--i)
printf("%d\n", stack[i]);
}
}
}}
Queue:
For example, people waiting in line for a rail ticket form a queue.
Queue operation
#include<stdio.h>
#include<stdlib.h>
#define maxsize 5
void insert();
void delete();
void display();
int front = -1,rear = -1;
int queue[maxsize];
void main()
{
int choice;
while(choice != 4)
{
printf("\n*******Main Menu********* \n");
printf("\n ........................................ \n");
printf("\n 1.insert an element\n 2.Delete an element \n 3.Display the queue \n 4.Exit \n");
printf("\nEnter your choice ?");
scanf("%d", &choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nEnter valid choice??\n");
}
}
}
void insert()
{
int item;
printf("\nEnter the element\n");
scanf("\n %d", &item);
if(rear == maxsize-1)
{
printf("\n OVERFLOW \n");
return;
}
if(front == -1 && rear == -1)
{
front = 0;
rear = 0;
}
else
{
rear = rear+1;
}
queue[rear] = item;
printf("\nValue inserted ");
}
void delete()
{
int item;
if (front == -1 || front > rear)
{
printf("\n UNDERFLOW \n");
return;
}
else
{
item= queue[front];
if(front== rear)
{
front = -1;
rear = -1 ;
}
else
{
front=front + 1;
}
printf("\nvalue deleted ");
}
}
void display()
{
int i;
if(rear == -1)
{
printf("\nEmpty queue\n");
}
else
{
printf("\nprinting values.....\n");
for(i=front;i<=rear;i++)
{
printf("\n %d\n", queue[i]);
}
}
}
Question 22. What is Linked List, Types & Write a C program to create a Linked list
A linked list is a sequence of data structures, which are connected together via links. Linked
List is a sequence of links which contains items. Each link contains a connection to another
link. Linked list is the second most-used data structure after array.
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++
Doubly Circular linked list:
A Doubly Circular linked list or a circular two-way linked list is a more complex type of
linked-list that contains a pointer to the next as well as the previous node in the
sequence.
The difference between the doubly linked and circular doubly list is the same as that
between a singly linked list and a circular linked list.
The circular doubly linked list does not contain null in the previous field of the first
node.
Creating Nodes:
#include <stdio.h>
#include <stdlib.h>
// Creating a node
struct node
int value;
};
while (p != NULL)
int main()
// Initialize nodes
// ALLocate memory
one->value = 1;
two->value = 2;
three->value = 3;
// Connect nodes
one->next = two;
two->next= three;
three->next = NULL;
// printing node-value
head = one;
printLinkedlist(head);
}
Question 23. What is Null, Dangling & Void pointer
Null pointer : A Null pointer is a variable that has a value of zero or has an address that
points to nothing.
In C, we use the keyword NULL to make a variable as null pointer
Dangling Pointer: A pointer pointing to a memory location that has been deleted (or freed) is
called dangling pointer.
The dangling pointer errors can be avoided by initializing the pointer to the NULL value.
Void Pointer: The void pointer in C is a pointer that is not associated with any data types. It
points to some data location in the storage. This means that it points to the address of
variables. It is also called the general-purpose pointer.
Example :
#include<stdio.h>
int main()
{
int a = 10;
int *ap;
ap = &a;
float b = 10.12;
float *af;
af = &b;
void *vp;
vp = ap;
printf("the value of a %d \n ", *(int *)vp);
vp = af;