You are on page 1of 37

Question 1.

Ranges of data types:

Question 2. Storage classes

Storage Classes are used to describe the features of a variable/function. These features basically
include the scope, visibility and life-time.

The storage classes decide the following entities:


o Lifetime

o Visibility

o Memory Location

o Default value

There are 4 storage classes


o Automatic (auto)
o External (extern)
o Static (static)
o Register (register)
1. Automatic storage class:

 The auto storage class is the default for variables declared inside a block.

 ‘auto’ is the keyword used to define the automatic variables.

 Automatic variables are allocated memory automatically at runtime.

 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 automatic variables are initialized to garbage by default.

 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()

auto int a = 10;

printf(“the value of variable a is %d”,a);

Or

#include<stdio.h>

int main()

int a = 10; // usually all the local variables are auto storage class variables

printf(“the value of variable a is %d”,a);

2. 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 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();
}
}

3. Register storage class:

 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>

printf(“value of a after assignment : %p”,a);

4. External storage class:

 ‘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

Find the output for : (find the fault and write)

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);

Example : print address of any variable


#include<stdio.h>

int main()

int a;

printf("%p",&a);

Summary for storage classes:

Question 3. Stages of compilation process:

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.

Question 5. What is function declaration & function definition

Question 6. what is function pointer, write a program

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>

int multiply(int, int);


int main()

int (*ptr)(int,int) = &multiply;

int result = (*ptr)(5,6);

printf("the multiplication of a n b is : %d",result);

int multiply(int a, int b)

return a*b;

Calling a Function through Function Pointer

#include <stdio.h>

int main()

float gain(float,float);

float (*fp)(float,float);

fp=&gain;

float result;

result = gain(5,4.5);

printf("\n result is %f",result);

result=(*fp)(5,2.5);

printf("\n result is %f",result);

float gain(float a, float b)

return a*b;

Question 7. Type Qualifiers


Type qualifiers are the keywords used to modify the properties of variables. Using type qualifiers, we can
change the properties of variables.

Constant Type Qualifier:

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.

The keyword const is used at the time of variable declaration.

We use the following syntax to create constant variable using const keyword.

const datatype variableName ;

EX: (Try this)


#include<stdio.h>
int main()
{
int i = 9;
const int x = 10;
i = 15;
x = 100; // creates an error
printf("i = %d\n x = %d", i, x );
}

Volatile type qualifier:


The volatile type qualifier is used to create variables whose values can't be changed in the program explicitly
but can be changed by any external device or hardware.

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.

Question 8. Difference between Malloc() & Calloc()


Question 9. Bitwise operators
An Bitwise operator is used for the manipulation of data at the bit level. These operators are not applied for
the float and double datatype.

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

a & b = 4 (In Decimal) a | b = 7 (In Decimal) a ^ b = 3 (In Decimal)

AND Operation OR Operation XOR Operation

0101 0101 0101

0110 0110 0110

----- ---- ----

0100 = 4 0111 = 7 0011=3

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)

Binary form of 9 is 0000 1001

Remove the 1st bit from left side 000 1001

and add 0 at right side

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 << n = a * (2 ^ n) =25 * 2^2= 25 *4=100

n= number of positions you want to shift

a<<2

Input: 25 << 2 ^2= 25*4

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)

Binary form of 9 is 0000 1001

Remove the last bit from right side  0000 100

and add 0 at left side

0 0000 100=0000 0100

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)

Input: 25 >> 2 = 25/(2^2) = 6.8

Output: 6

Question 10. Endianess


Endianness indicates whether integers are represented with the most significant byte stored at the lowest
address (big endian) or at the highest address (little endian).

Question 11. Interrupt & ISR


An interrupt service routine (ISR) is a software routine that hardware invokes in response to an interrupt.

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.

Calling a Function through Function Pointer

#include <stdio.h>

int main()

float gain(float,float);

float (*fp)(float,float);

fp=&gain;

float result;

result = gain(5,4.5);

printf("\n result is %f",result);

result=(*fp)(5,2.5);

printf("\n result is %f",result);

float gain(float a, float b)

return a*b;

Question 12. Write a C program to print Fibonacci series

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()

int a[50], size, i, big, small;

printf("\nEnter the size of the array: ");

scanf("%d", &size);

printf("\nEnter the %d elements of the array: \n", size);

for(i = 0; i < size; i++)

scanf("%d", &a[i]);

big = a[0];

for(i = 1; i < size; i++)

if(big < a[i])

big = a[i];

printf("\nThe largest element is: %d", big);


small = a[0];

for(i = 1; i < size; i++)

if(small>a[i])

small = a[i];

printf("\nThe smallest element is: %d", small);

return 0;

Question 14. Write a program to find odd or even number using bitwise operator

#include<stdio.h>

int main()

int number;

printf("Enter number: ");

scanf("%d", &number);

/* Checking Even or Odd */


if (number & 1)

printf("%d is Odd.", number);

else

printf("%d is Even.", number);

return 0;

Question 15. Write a program to find the Endianness

#include <stdio.h>

int main ()

unsigned int x = 0x76543210;

char *c = (char*) &x;

printf ("*c is: 0x%x\n", *c);

if (*c == 0x10)

printf ("Underlying architecture is little endian. \n");

else

printf ("Underlying architecture is big endian. \n");

return 0;

Question 16. Write a program to access array elements using pointers


#include <stdio.h>

int main ()

int p[5] = { 2, 9, 10, 33, 12 };

int *ptr = p;

for ( int i = 0; i < 5; i++)

printf ("&p[%d] = %p \t p[%d] = %d\n", i, ptr + i, i, *(ptr + i));

return 0;

Question 17. Write a program to print the

a. Half Pyramid of “ * ”
#include <stdio.h>

int main()

int i, j, rows;

printf("Enter the number of rows: ");

scanf("%d", &rows);

for (i = 1; i <= rows; ++i)

for (j = 1; j <= i; ++j)

printf("* ");

printf("\n");

return 0;

b. Half Pyramid of Numbers


#include <stdio.h>

int main() {

int i, j, rows;

printf("Enter the number of rows: ");

scanf("%d", &rows);

for (i = 1; i <= rows; ++i) {

for (j = 1; j <= i; ++j) {

printf("%d ", j);

printf("\n");

return 0;

c. Half Pyramid of Alphabet’s

#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;
}

Full Pyramids of “ * “, Numbers & ABCD


#include <stdio.h>
int main() {
int i, space, rows, k = 0;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i, k = 0) {
for (space = 1; space <= rows - i; ++space) {
printf(" ");
}
while (k != 2 * i - 1) {
printf("* ");
++k;
}
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;
}

/////////////////////////////////////////////////////////////////

Inverted full pyramid of *

#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()

int i, num, n, count;

printf("Enter the range: ");

scanf("%d", &n);

printf("The prime numbers in between the range 1 to %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);

num = num | (1<<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);

num &= ~(1<<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);

num = num ^ (1<<n);

printf(" The vale of num after shifting nth position %d", num);
return 0;
}

Question 20. Write a C program to convert an integer to string

#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();
}

Question 21. Write a C program to perform Stack & Queue operation:

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.

Standard Stack Operations

The following are some common operations implemented on the stack:

o push(): When we insert an element in a stack then the operation is known as


a push. If the stack is full then the overflow condition occurs.
o pop(): When we delete an element from the stack, the operation is known as
a pop. If the stack is empty means that no element exists in the stack, this
state is known as an underflow state.
o isEmpty(): It determines whether the stack is empty or not.
o isFull(): It determines whether the stack is full or not.'
o peek(): It returns the element at the given position.
o count(): It returns the total number of elements available in a stack.
o change(): It changes the element at the given position.
o display(): It prints all the elements available in the stack.

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:

 A queue can be defined as an ordered list which enables insert operations to be


performed at one end called REAR and delete operations to be performed at
another end called FRONT.

 Queue is referred to be as First In First Out list.

 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.

Types of Linked list


1. Singly Linked List:
 It is the simplest type of linked list in which every node contains some data and a
pointer to the next node of the same data type.
 The node contains a pointer to the next node means that the node stores the
address of the next node in the sequence.
 A single linked list allows traversal of data only in one way.
2. Doubly Linked List
3. :
 A doubly linked list or a two-way linked list is a more complex type of linked list which
contains a pointer to the next as well as the previous node in sequence, Therefore, it
contains three parts are data, a pointer to the next node, and a pointer to the
previous node.
 This would enable us to traverse the list in the backward direction as well.

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++

4. Circular Linked List:


 A circular linked list is that in which the last node contains the pointer to the first
node of the list. While traversing a circular linked list, we can begin at any node and
traverse the list in any direction forward and backward until we reach the same node
we started.
 Thus, a circular linked list has no beginning and no end.


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;

struct node *next;

};

// print the Linked List value

void printLinkedlist(struct node *p)

while (p != NULL)

printf("%d ", p->value);


p = p->next;

int main()

// Initialize nodes

struct node *head;

struct node *one = NULL;

struct node *two = NULL;

struct node *three = NULL;

// ALLocate memory

one = malloc(sizeof(struct node));

two = malloc(sizeof(struct node));

three = malloc(sizeof(struct node));

// Assign value values

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;

printf("the value of a %f \n", *(float *)vp);

You might also like