KEMBAR78
C Language Notes-1 | PDF | Software Development | Software
0% found this document useful (0 votes)
12 views78 pages

C Language Notes-1

Uploaded by

haibhaihacker050
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views78 pages

C Language Notes-1

Uploaded by

haibhaihacker050
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 78

C LANGUAGE NOTES

IV TH SEM
DECE

Prepared BY :

S.MAHABOOB SHAREEF

LECTURE IN ECE

written by : S. Mahaboob shareef , Lecture in ECE


1
INDEX:

S.N.O TOPIC PAGE NUMBERS


*ESSAY QUESTIONS:
1 UNIT-II ( CONDITIONAL STATEMENTS AND
ARRAYS) CODES 3-20
2 UNIT-III( STRINGS,FUNCTIONS&POINTERS) 21-27
3 UNIT-IV ( STRUCTURES & UNIONS) 28-32
4 UNIT-V ( BASICS OF MATLAB) 33-45
*SMALL QUESTIONS:
5 UNIT-1(C-PROGRAMMING BASICS) 46-50
6 UNIT-II ( CONDITIONAL STATEMENTS AND 51-52
ARRAYS) CODES
7 UNIT-III( STRINGS,FUNCTIONS&POINTERS) 53-54
8 UNIT-IV ( STRUCTURES & UNIONS) 55-57
9 UNIT-V ( BASICS OF MATLAB) 58-60
10 10 MARKS QUESTIONS 61-70

WEIGHTAGE MARKS WISE


S.N.O UNIT TITLE ALLOTED DISTRIBUCTION WEIGHTAGE
R U AP AN
1 C
PROGRAMMING 6 3 3 - - ( CO 1)
BASICS
2 CONDITIONAL
STATEMENTS AND 22 3 3 16 - ( CO 2)
ARRAYS
3 STRINGS,FUNCTIONS
& POINTERS 24 3 3 8 10 ( CO 3)

4 STRUCTURES
& UNIONS 14 3 3 8 - ( CO 4)
5 BASICS OF
MATLAB 14 3 3 8 - ( CO 5)

TOTAL 80 15 15 40 10

written by : S. Mahaboob shareef , Lecture in ECE


2
ESSAY QUESTIONS
UNIT-II : CONDITIONAL STATEMENTS AND ARRAYS( ESSAY -CODES)
1.Writ a program to find sum of first “n” natural number

ANSWER:

#include < stdio.h>

# include < conio.h>

void main()

int i, sum=0,n;

clrscr();

printf(“enter the value of n : ”);

scanf (“%d”,&n) ;

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

sum = sum+i;

printf(“sum of n natural numbers is %d”, sum );

getch();

2. Write a progam to find whether the given number is Armstrong NUMBER or NOT?

Answer:

# include <stdio.h>

# include<conio.h>

written by : S. Mahaboob shareef , Lecture in ECE


3
void main()

int num,sum=0,rem,temp;

printf(“enter the number : ”);

scanf (“%d”,& num);

temp = num ;

while (num>0)

rem = num%10;

sum = sum+rem*rem*rem ;

num = num/10 ;

if ( temp == sum )

printf(“Entered no is Armstrong number”);

else

printf(“entered number is not a Armstrong number”);

getch();

3. Write a program to find whether the given number is palin doome or not ?

#include<stdio.h>
#include<conio.h>
written by : S. Mahaboob shareef , Lecture in ECE
4
void main()
{
int num,rev=0,rem,temp ;
clrscr();
printf(“Enter the number:”) ;
scanf(“%d”,&num);
temp = num ;
while (num >0)
{
rem = num%10 ;
rev = rev*10+rem ;
num = num/10 ;
}
if (temp==rev)
{
printf(“The given number is a palindrome”);
}
else
{
printf(“The given number is not a palindrome”);
}
getch();
}

4..write a program to find the sum of digits of a given number ?

#include<stdio.h>
#include<conio.h>
void main()
{
int num, sum=o,rem;
clrscr();
printf(“enter the number : ”) ;
scanf (“%d”,&num) ;
while (num>o)
{
rem = num%10;
Sum = sum+rem;
written by : S. Mahaboob shareef , Lecture in ECE
5
num = num/10;
}
printf(“sum of digits of number=%d”,sum) ;
getch() ;
}

5.Write a program to print fibonaci series using loops?


Answer:
#include<stdio.h>
#include<conio.h>
void main()
{
int x=0,y=1,z,num, i ;
clrscr();
printf(“Enter the number:”);
scanf(“%d”,&num);
printf(“\n %d%d”,x,y);
for(i=2; i<num; i++)
{
z=x+y;
printf(“ %d ”,z ) ;
x=y;
y=z;
}
getch()
}

Output:
Enter number:5
0,1,1,2,3

6.Write a program to find whether the given number is a prime number or not ?

Answer:

#include<stdio.h>
#include<conio.h>
void main()
{
written by : S. Mahaboob shareef , Lecture in ECE
6
int num, i, flag=0;
clrscr();
printf("Enter the number : ");
scanf("%d",&num);
if(num==0||num==1)
{
flag=1;
}
for(i=2; i<num ; i++)
{
if(num% i == 0)
{
flag=1;
}
}
if(flag==1)
{
printf("The given number is not a prime number");
}
if(flag==0)
{
printf("The given number is a prime number");
}
getch();

7.Write a program to print prime numbers between given numbers ?

Answer:

#include<stdio.h>
#include<conio.h>
void main()
{
int n1,n2,i,j,flag=0;
clrscr();
printf(“enter n1 and n2 : ”);
scanf(“%d%d”,&n1,&n2);
printf(“prime numbers between %d and %d are\n”,n1,n2);

written by : S. Mahaboob shareef , Lecture in ECE


7
for(i=n1+1; i<n2; i++)
{
flag=0;
for(j=2; j<i; j++);
{
if(i%j==0)
{
flag=1;
}
}
if(flag==0)
{
printf(“%d\n”,i);
}
}
getch();
L
8.Write program to print all even and odd numbers?

Answer:

#include<stdio.h>
#include<conio.h>
void main()
{
int num, i;
clrscr();
printf(“Enter the numbers :”);
scanf(“%d”,&num);
printf(“even numbers are : ”);
for(i=1, i<=num; i++)
{
if(i%2==0)
{
printf(“%d\t”,i);
}
}
printf(“odd numbers are:”);
for(i=1; i<=num; i++)
{
for(i%2==1)
written by : S. Mahaboob shareef , Lecture in ECE
8
{
printf(“%d\t”,i);
}
}
getch();

9.write a C program to perform matrix addition?

Answer:

#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],b[3][3],c[3][3],i,j;
clrscr();
printf(“enter Matric-A elements”);
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
scanf(“%d”, &a[i][j]);
}
}
printf(“Enter Matric- B elements:”);
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
scanf(“%d”,&b[i][j]);
}
}
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
C[i][j]=a[i][j]+b[i][j];
}
}
written by : S. Mahaboob shareef , Lecture in ECE
9
printf(” Resultant matrix :\n”);
for (i=0; i<3; i++)
{
for(j=0; j<3 ; j++)
{
printf(“ %d\t”, c[i][j]);
}
printf(“\n”);
}
getch();

10.write a program to perform matrix multiplication ?

ANSWER:

#include<stdio.h>

#include<conio.h>

void main()

int a[3][3],b[3][3],c[3][3],i,j,k;

clrscr();

printf(“enter matrix-A elements:”);

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

for (j=0; j<3; j++)

scanf(“%d”,&a[i][j]);

printf(“enter matrix-B elements:”);

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

{
written by : S. Mahaboob shareef , Lecture in ECE
10
for(j0; j<3; j++)

scanf(“%d”,&b[i][j]);

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

for(j=0; j<3; j++)

c[i][j]=0;

for(k=0; k<3; k++)

C[i][j]=c[i][j]+a[i][k]*b[k][j];

printf(“resultant matrix-C\n”);

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

for(j=0; j<3; j++)

printf(“%d\t”, c[i][j] ) ;

printf(“\n”);

written by : S. Mahaboob shareef , Lecture in ECE


11
getch();

11. Write a program to Sort an array of ‘n’ numbers?

Answer:

#include<stdio.h>

#include<conio.h>

void main()

int n,a[10],i,j,temp;

clrscr();

printf(“enter the number of elements in the array : ”);

scanf(“%d”,&n);

printf(“enter array elements: ”);

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

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

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

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

if(a[i]>a[j])

temp=a[i];

written by : S. Mahaboob shareef , Lecture in ECE


12
a[i] =a[j];

a[ j ]=temp;

printf(“the number after ascending order:”);

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

printf(“%d \t”,a[i]);

getch();

12.Write a program to find bigset number in the array?

Answer:

#include<stdio.h>

#include<conio.h>

void main()

int a[20],n,i;

clrscr();

printf(‘Enter the elements in the array:’);

scanf(“%d”,&n);

printf(“enter array elements:”);

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

written by : S. Mahaboob shareef , Lecture in ECE


13
{

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

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

if( a[0] <a[i])

a[0]=a[i];

printf(“The biggest number is %d”,a[0] );

getch();

13. Write a program to find the sum of elements of an array?

ANSWER:

#include<stdio.h>

#include<conio.h>

void main()

int i, a[20], n, sum=0;

clrscr();

printf(“Enter the number of elements in the array :”);

scanf(“%d”,&n );

printf(“Enter array elements :”);

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

written by : S. Mahaboob shareef , Lecture in ECE


14
{

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

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

sum = sum+a[i];

printf(“sum of elements in the array=%d”,sum);

getch();

14. write the program to find biggest of three numbers

ANSWER:

#include<stdio.h>

#include<conio.h>

Void main()

int a, b, c, big ;

clrscr();

printf(“enter three numbers:”);

scanf(“%d%d%d”,&a,&b,&c);

big=(a>b)?(a>c?a:c):(b>c?b:c);

printf(“biggest number=%d”,big);

getch();

written by : S. Mahaboob shareef , Lecture in ECE


15
( OR )

Answer:

#include<stdio.h>

#include<conio.h>

void main()

int a,b,c;

clrscr();

printf(“Enter three numbers:”);

scanf(“%d%d%d”,&a,&b,&c);

if((a>b)&&(a<c))

printf(“%d is the biggest number”,a);

else if (b>c)

printf(“%d is the biggest number”,b);

else

printf(“%d is the biggest number:”,c);

getch();

15.Write a program to find whether a given year is leap year or not?

written by : S. Mahaboob shareef , Lecture in ECE


16
ANSWER:

#include<stdio.h>

#include<conio.h>

void main()

int year;

clrscr();

printf(“enter the year:”);

scanf(“%d”,year);

if((year%4==0&&year%100!=0) || year %400 == 0)

printf(“%d is a leap year”,year);

else

pirntf(“%d is not a leap year”,year);

getch();

16. write a program to check whether a given number is even or odd by using bitwise
logical operator.?

#include<stdio.h>

#include<conio.h>

void main()

{
written by : S. Mahaboob shareef , Lecture in ECE
17
int num;

printf(“Enter a number: ”);

scanf(“%d”,&num);

if(num%2==1)

printf(“%d is odd”,num);

else

printf(“%d is an Even”,num );

getch ();

17. Write a program to check whether a given character is vowel or constant by using switch
statements ?

ANSWER:

#include<stdio.h>

#include<conio.h>

Void main()

char ch ;

clrscr();
written by : S. Mahaboob shareef , Lecture in ECE
18
printf(“Enter the character:”);

scanf(“%c”,&ch);

switch(ch)

case ’a’:

printf(“It is a vowel”);

break;

case ’e’:

printf(“It is a vowel”);

break;

case ’i’:

printf(“It is a vowel”);

break;

case ’o’:

printf(“It is a vowel”);

break;

case ’u’:

printf(“It is a vowel”);

break;

case ’A’:

printf(“It is a vowel”);

break;

case ’E’:

printf(“It is a vowel”);

break;

written by : S. Mahaboob shareef , Lecture in ECE


19
case ’I’:

printf(“It is a vowel”);

break;

case ’O’:

printf(“It is a vowel”);

break;

case ’U’:

printf(“It is a vowel”);

break;

default:

printf(“It is a Constant”);

getch();

18. Write a program to perform arithmetic operations using switch case statements

ANSWER:

#include<stdio.h>

#include<conio.h>

Void main()

int a,b,option ;

clrscr();

printf(“1.addition : \n 2. Subtraction : \n 3. Multiplication :\n 4. Division :\n 5. Modulus:


\n”);

printf(“Enter a and b”);


written by : S. Mahaboob shareef , Lecture in ECE
20
scanf(“%d %d”, &a,&b);

printf(“Enter the option:”);

scanf(“%d”,&option);

switch(option)

Case 1:

printf(“Addition of %d and %d is %d”,a,b,a+b);

break;

Case 2:

printf(“Subtraction of %d and %d is %d”,a,b,a-b);

break;

Case 3:

printf(“ Multiplication of %d and %d is %d”,a,b,a*b);

break;

Case 4:

printf(“ Division of %d and %d is %d”,a,b,a/b);

break;

Case 5:

printf(“ modulus of %d and %d is %d”,a,b,a%b);

break;

default :

printf(“ Enter the valid option”);

getch();

written by : S. Mahaboob shareef , Lecture in ECE


21
19.Explain “ if “ condition with an example?

Statement:

“ if “ is a simple decision making statement : it consists of the test condition and if the given
condition is true only then the code “ if “ block will be executed .

Syntax :

if( Condition)

// statements

EXAMPLE

# include<stdio.h>

int main()

int num=10;

if(num>0)

printf(“ Number is positive “);

return 0;

20.Explain ‘if-else’ condition,with one example ?

Statement:

if – else condition is a decision making condition it condition two blocks if and else ,If give
condition is true , The condition present in the if block is executed , otherwise else condition is executed.
written by : S. Mahaboob shareef , Lecture in ECE
22
SYNTAX :

if(condition) else

{ {

// statements // statements

} }

EXAMPLE: {

# include<stdio.h> printf(“%d is less than 50”,n);

#include<conio.h }

void main() else

{ {

int n; printf(“ %d is greather than 50”,n);

printf(“ Enter the number”); }

scanf(“%d”/&n); getch();

if(n<50) }

21.Explain nested if-else condition and explain with one example ?

Statement:

A nested if –else condition refers to placing one if – else statement inside another. this alloes for
more complex decision-making based on multiple conditions

syntax: // statements

if(condition1) }

{ else if(condition2)

written by : S. Mahaboob shareef , Lecture in ECE


23
{ }

// statements else (condition)

} {

else if(condition3) // statements

{ }

// statements

EXAMPLE:

# include<stdio.h> printf(“\n Tuesday”); }

#include<conio.h> } else if (n==6)

void main () else if (n==3) {

{ { printf(“\n saturday”);

int num=7; printf(“\n Wednesday”); }

clrscr(); } else if (n==7)

printf(“\n The 7th day is:”); else if (n==4) {

if(n==1) { printf(“\n Sunday”);

{ printf(“\n Thursday”); }

printf( “ \n Monday “); } getch();

} else if (n==5) }

else if (n==2) {

{ printf(“\n Friday”);

22.Explain for loop with an example ?

Statement:

A for loop is a repetition control structure that allows to efficiently to write a loop that needs to
execute a specific number of times.

SYNTAX:

for(initialization ; condition; updation )


written by : S. Mahaboob shareef , Lecture in ECE
24
{

// statements

EXAMPLE:

# include<stdio.h>

#include<conio.h>

void main()

int n,sum=0;

printf(“ Enter the number :”);

scanf(“%d”,&n);

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

sum=sum+i;

printf(“ sum of the n natural numbers is %d”,sum);

getch();

written by : S. Mahaboob shareef , Lecture in ECE


25
23.Explain the while loop with an example ?

STATEMENT:

The loop can be used to iterate a part of code while the given condition remains true.

SYNTAX:

while(condition)

// statements

EXAMPLE:

# include<stdio.h> {

int main() printf(“hello world \n”);

{ i++;

int i=0; }

while(i<5) return 0;

written by : S. Mahaboob shareef , Lecture in ECE


26
}

24.Explain do while condition with an example ?

STATEMENT:

do while statements creates a structured loop that executes as long as a specified condition
is true at the end of each pass through the loop.

SYNTAX:

do

// statements

while(condition);

EXAMPLE: int i=0;

#include<stdio.h> do{

int main() printf(“ hello world \n”);

{ i++;

written by : S. Mahaboob shareef , Lecture in ECE


27
}while(i<5); }

return 0;

UNIT-3: STRINGS,FUNCATIONS & POINTERS


1. Explain passing of parameter to the function.?

ANSWER:

We can pass parameter to the function in two ways:


1 call –by value
2 call by reference

Call by value :
In this method the function receives a copy of the variables value, and any changes
made to the value inside the function do not affect the original variables value.
Example : swapping two numbers using call-by value
#include<stdio.h>
void swap(intx,inty);
int main()
{
int a=5,b=10;
written by : S. Mahaboob shareef , Lecture in ECE
28
printf(“before swapping : a=%d,b=%d\n”,a,b);
swap(a,b);
printf(“ After swapping : a=%d, b=%d\n”,a,b);
return 0;
}
void swap( int x, int y)
{
int temp;
temp =x;
x=y;
y= temp;
}

OUTPUT:
Before swapping a=5, b=10
After swapping a=5 ,b=10

Swap function receives ‘a’ and ‘b’ as parameters. Inside the function, the values are swapped.
Since it is call – by –value the original values ‘a’ & ‘b’ are not affected by the swap

CALL BY REFERENCE :
In c, call by reference is a method ,where a reference to the memory location
of a variable is passed through the function to modify the value stored at that memory location,
which will also affect the original variable outside the function

EXAMPLE: swapping two numbers using call by reference

#include<stdio.h>
Void swap (int *x , int *y);
int main()
{
int a=5,b=10;
printf(“before swapping : a=%d,b=%d\n”,a,b);
Swap(&a,&b);
printf(“ After swapping : a=%d, b=%d\n”,a,b);
return 0;
}
Void swap( int * x, int * y)
{

written by : S. Mahaboob shareef , Lecture in ECE


29
int temp;
temp=*x;
*x=*y;
*y= temp;

OUTPUT:
Before swapping a=5, b=10
After swapping a=10 ,b=5

_______________________________________________________________________________________

2. Write the options of getchar(), getch(), getche(),and putchar() function ?

ANSWER:

getchar():

In C, getchar() function is used to read a single character from the keyboard (or)
standard input device & returns its ASCII value as an integer.

Example:

#include<stdio.h>

int main()

int ch;

ch = getchar();

printf(“ASCII value of entered character is %d”, ch);

Return 0;

OUTPUT :

suppose ,if we enter character is : ‘A’, then the output is :

ASCII value of enter character is 65

getch():

written by : S. Mahaboob shareef , Lecture in ECE


30
getch() is a function in the C programming language that reads a single character from the
keyboard without echoing the character to the screen . getch() function defined in the “conio.h” library

EXAMPLE:

#include<stdio.h>

#include<conio.h>

int main()

char ch;

printf(“ Enter the character : ”);

ch = getch();

printf(“ \n you entered : %c”, ch);

return 0;

OUTPUT: Enter a character :

you entered k

getche():

getche() is a function in the C programming language that reads a single character from the
keyboard and immediately echoes it to the console. (i.e, the character is displayed on the screen)

EXAMPLE:

#include<stdio.h>

#include<conio.h>

int main()

char ch;

printf(“ Enter the character”);

written by : S. Mahaboob shareef , Lecture in ECE


31
ch = getche();

printf(“ \n you entered : %c”, ch);

return 0;

OUTPUT: Enter a character : h

you entered h

putchar():

‘ putchar()’ is a function in a C programming language that is used to write a character to the


standard output to stream . It is declared in the < stdio.h> header file.

EXAMPLE:

#include<stdio.h> put char(ch);

int main() return 0;

{ }

char ch=’A’; Output: A

3.Explain the string handling functions strcat(), strcmp(), strcmp() and strlen() with example?

Strcat() :

strcat() is a standard library function in C language , that is used to concatenate two strings. It is
defined in the ‘ string.h’ header file. Syntax: strcat (str1 , str 2) ;

EXAMPLE:

#include<stdio.h>

#include<string.h>

int main()

char str 1[50]=”hello”;

char str2 [50]=”world”;

strcat (str1, str 2);


written by : S. Mahaboob shareef , Lecture in ECE
32
printf(“Concatenated string is: s\n”,str 1);

return 0 ;

OUTPUT : HELLO WORLD

In the above example str2 is appended source string str 1

Strcmp() :

strcmp() is a standard library function in c programming language that is used to compare two
strings

• SYNTAX: strcmp (str 1 , str 2)

i ) strcmp() returns a value zero, if both strings are identical

ii) if both are not equal, strcmp() returns the numerical difference between ASCII values of the first non -
matching pair of characters

EXAMPLE:

#include<stdio.h>

#include<string.h>

int main()

char str 1[20]=”jerry”, str 2[20]=”ferry”;

int i,j,k;

i= strcmp(string1, “Jerry”);

j = strcmp( string 1,string 2);

printf(“%d%d”,i,j);

return 0;

OUTPUT: 0 4

written by : S. Mahaboob shareef , Lecture in ECE


33
Strcpy :

‘strcpy () ’ is a standard libaryb function in C programming language that is used to copy a string from one
location to another

SYNTAX: strcpy( str1 ,str 2);

str2 is copied into str1

EXAMPLE:

#include<stdio.h>

#include<string.h>

int main()

char source[20]=”Janasena”, target[20];

strcpy( target , source);

printf(“ source string : %s\n”,source);

printf(“ target string : %s\n”, target );

return 0;

OUTPUT:

source string = janasena

target string= janasena

Strlel() :

This function counts the number of characters present in a string.

SYNTAX: len = strlen(str);

EXAMPLE:

#include<stdio.h>

#include<string.h>

int main()

written by : S. Mahaboob shareef , Lecture in ECE


34
char str1 = “Humpty Dumpty ”;

int len;

len = strlen(str1);

printf(“ string = %s length is = %d”,str 1, len );

return 0;

OUTPUT: string = Humpty Dumpty length is = 13

written by : S. Mahaboob shareef , Lecture in ECE


35
UNIT – IV : STRUCTURES & UNIONS
1. Illustrate structures with a program to read and print a book database consisting of title of book, author,
no ..of pages, price as fields?

#include<stdio.h> scanf(“%f”,&b[i].price);

#include<conio.h> }

struct book printf(“ The book details are: \n”);

{ printf(“ title\t author\t no.of pages\t


price\n”);
char title[20];
printf(“______ ______ ______
char autor [20];
_______\n”);
int no- pages ;
for(i=0;i<3;i++)
float price ;
{
};

void main()
printf(“%s\t%s\t%d\t%f\n”,b[i].title,b[i].author,b[
{ i].no_pages,b[i].price);

int i; }

struct book b[3]; getch();

printf(“ Enter book details:\n”); }


for (i=0; i<3;i++) RESULT:
{ INPUT:
printf(“ Enter the book name”);
Enter book details:
scanf(“%s”,b[i].title);
enter book name: MP
printf(“ Enter the author name”);
enter author name: SMS
scanf(“%s”,&b[i].author);
enter no.of pages: 125
printf(“ Enter the no.of pages”);
enter the price: 130
scanf(“%d”,&b[i].no_pages);
enter book name: EC
printf(“ Enter the Price”);
enter author name: BMK
written by : S. Mahaboob shareef , Lecture in ECE
36
enter no.of pages: 140 enter author name: GNR

enter the price: 150 enter no.of pages: 140

enter book name: ADC enter the price: 160

OUTPUT: title author no_pages price

MP SMS 125 130

EC BMK 140 150

ADC GNR 150 160

_______________________________________________________________________________________________

2. Explain the conditional pre-processor directives with examples.?

The conditional pre-processor directives in c are used to include or exclude certain blocks of code based
on a set of conditions. some of the preprocessor directives are explained below

1. #if :
this directive allows you to test a condition and include (OR) exclude code based on the
result

EXAMPLE: #if SOME_CONSTANT==42

// code to be executed if SOME_CONSTANT is equal to 42


#endif

2.#ifdef:

This directive tests whether a morco has been defined If the marco has been defined, the code
between the #if def and # endif directives is included in the compilation process

EXAMPLE: #ifdef DEBUG_MODE

// code to be executed if the DEBUG_MODE marco is defined

#endif

3. #ifndef:

written by : S. Mahaboob shareef , Lecture in ECE


37
this directive tests whether a macro has not been defined. if macro has not not been defined , the code
between the #ifndef and #endif directives is included in the compilation process

example:

#ifndef RELEASE_MODE

//code to be executed if the RELEASE-MODE macro is not defined.

#endif.

4. #elif:

this directive is used with #if to test additional conditions if the previous condition is false.

example:

#if SOME_CONSTANT==42

“code to be executed if SOME_CONSTANT==42

#elif SOME_CONSTANT==43

“code to be executed if SOME CONSTANT==43

#else

“code to be executed if neither condition is true

#endif

5. #else:

this directive is used with #if to include code that will executes if the condition is false

example:

#if SOME _CONSTANT==42

“code to be executed is SOME _CONSTANT equal to 42.

#else

“code to be executed if SOME_CONSTANT is not equal to 42

#enif

6.. #endif:

which ends conditional text.

_______________________________________________________________________________________________

3. Explain unconditional pre processor directives with examples ?

written by : S. Mahaboob shareef , Lecture in ECE


38
In C ,pre processer directives are instructions that are processed by the pre processer before the actual
compilation of the code begins. Unconditional pre processer directives are always executed by pre processor
without any condition . some of the unconditional pre processor directives are:

1. # define :

This directive is used to define a Macro (or) to define a constant.

1.Example to define constant :

# define PI 3.14

This code defines PI constant with a value 3.14

2. Example to define Macro :

# include < stdio.h >

# define Mul ( a,b ) a*b

void main()

int = x;

x= Mul (5,6);

printf(“%d”,x);

The program prints 30 as output. The marco Mul(a,b) expanded and it is replaced with a*b . During
execution the program prints 30

2. # include :

This directive is used to include the contents of a header file in the source code.

EXAMPLE: # include < stdio.h>

This code includes the standard input / output header file in the source code

3. # undef:

This is used to define a Macro

EXAMPLE:

# include < stdio.h >

# define Mul ( a,b ) a*b \* defining a marco *\

written by : S. Mahaboob shareef , Lecture in ECE


39
void main()

int x;

x= Mul (6,7);

printf(“ %d\n”,x);

/* Marco prints 42 */

# undef Mul(a,b) a*b

/* undefining Marco */

# ifndef Mul(a,b) a*b

/* if marco is not defined code executes */

printf(“ c is wow”);

# endif

OUTPUT: 42

c is wow

4. #error:

This directive is used to generate compilation error message .

EXAMPLE: # ifndef DEBUG

# error Debug mode is not enabled

# endif

5. # warning:

This is used to generate a compilation warning message. # warning This code is deprecated and may not
work in future.

6. # pragrna:

This directive is used to provide additional information to the compiler about how to handle
the source code

EXAMPLE: # pragma GCC optimize (“ O2 ”)

written by : S. Mahaboob shareef , Lecture in ECE


40
UNIT-V : BASICS OF MATLAB
1.Explain with an example the matrix operations such as 1. Addition ,2. Subtraction 3. Multiplication 4.
Traspose 5. inverse using matlab ?

>> A=[1 2 3; 4 5 6 ; 7 8 9 ]
A= 1 2 3
4 5 6
7 8 9

>> B=[1 4 6 ; 3 2 1 ; 7 4 3 ]
B= 1 4 6
3 2 1
7 8 9

i) Matrix addition :
>> C = A+B

C = 2 6 9
7 7 7
14 12 12
ii) Matrix subtraction:

>> D = A-B
D = 0 -2 -3
1 3 5
0 4 6

iv) Matrix multiplication :

>> E =A*B
E= 28 20 17
61 50 47
94 80 77

iv) Transpose:
>> F = A’

F= 1 4 7
written by : S. Mahaboob shareef , Lecture in ECE
41
2 5 6

3 6 9

v) inverse:

G = inv( B)

G = -0.1111 -0.6667 0.4444

0.1111 2.1667 0.944

0.1111 -1.333 0.5556

____________________________________________________________________________________

2. Write the syntax and usage of decision making statements . i) if-end. ii) if-else-end statement in
matlab ?

( i ) if-end:

SYNTAX:

if condition

% code to execute if the condition is true

end.

Explanation: If the condition is true , the code in if block will be executed . If the condition is false, the
code in if block doesn’t executed

EXAMPLE :

>> x=5 ;

>> if x> 0

disp( ‘ x is positive ‘);

end

In the above example x=5, so the condition is ture, if- block will be excuted

OUTPUT : x is positive

(ii). if- else :

Syntax :

written by : S. Mahaboob shareef , Lecture in ECE


42
if condition

% code to be executed if condition is true

else

% code to be executed if condition is false

end.

EXPLANTION : If the condition is true , the code in if block will be executed . If the condition is false, the
code in else block will be executed

EXAMPLE:

x=-5 ;

if x>0

disp (‘ The number is positive’);

else

disp (‘ The number is negative’);

OUTPUT: The number is negative

In the above program condition is false, so else will be execuated.

_______________________________________________________________________________________

3. Explain while loop used in Matlab. ?

while loop :

Syntax:

while condition

% code to be executed as long as condition is ture

end

EXPLINATION: In MATLAB , a ‘ while ‘ loop is used to execute a block of code repeatedly as long as
condition is true.

EXAMPLE :

sum = 0 ;

i =1;

written by : S. Mahaboob shareef , Lecture in ECE


43
while i<=10

sum= sum+i;

i=i+1;

end

disp(sum);

OUTPUT:

55

loop executes 10 times, when i reaches 11 , condition fails, loop won’t executes

_______________________________________________________________________________________

3. State the use of (i) SIMULINK and ( ii ) GUI ?

i) SIMULINK : SIMULINK is a graphical programming environment in MATLAB that allows you to model,
simulate and analyze dynamic systems.

common uses of SIMULINK :

1. MODEL DEVELOPMENT :

You can use Simulink to create models for physical systems, electrical systems, mechanical
systems and control systems. Simulink also provides a library of pre defined blocks that can be used to
model physical system

2.SIMULATION:

Simulink enables you to simulate you mode in both time and frequency domain.

3. CONTROL SYSTEM DESGIN :

Simulink provides tools for designing and simulation of control system. we can simulate PID
controllers , state space controllers etc..

4. CODE GENERATION:

Simulink provides support for generating ‘ C ’ code from Simulink models.

5. REAL TIME SYSTEMS :

Simulink supports real time simulation and rapid prototyping of embedded systems.
Simulink also used in signal processing algorithms and Image processing systems

written by : S. Mahaboob shareef , Lecture in ECE


44
ii ).GUI :

Graphical user interface in MATLAB, allows you to create interactive applications with buttons, sliders,
menus, and ather graphical objects that allows user to internet with data and algorithm.

COMMON USES OF GUI :

1. DATA VISUALIZATION :

GUI can be used to create interactive visualization that allows users to explore and
analyze data in real time.

2. ALGORITHM DEVELOPMENT :

GUI can be used to provide interactive environment for algorithm development ,with
different parameters and settings.

3.EDUCATION:

GUI can be used to create interactive simulations and exercises that help in students to learn
and understand complex concepts.

4. CUSTOMISED APPLICATIONS:

GUI can be used to create user friendly applications

5. CONTROL SYSTEM DESGIN :

Using GUI in matlab, we can design and interact with PID controllers, in control system

4. MATLAB COMMANDS ?

i) plot(x,y) :
Draw 2-D plot :

1. x = [ 0:0.01:2*pi];

y = sin(x);

plot (x,y)

written by : S. Mahaboob shareef , Lecture in ECE


45
2. x = [ 0:0.01:2*pi];

𝑦1 = sin(x);

𝑦2 = cos(x);

plot(x, 𝑦1 , 𝑥 , 𝑦2, )

3. x=[0:0.01:2*pi*5];

𝑦1 = sin(x);

𝑦2 = cos(x);

plot(x, 𝑦1 , 𝑥 , 𝑦2, )

ii) title:

to add title to a plot

. x = [ 0:0.01:2*pi];

y = sin(x);

plot (x,y)

written by : S. Mahaboob shareef , Lecture in ECE


46
title(‘ graph of sine wave ‘ )

x label : to add label on the x-axis

y label : to add label on y-axis

. x = [ 0:0.01:2*pi];

𝑦1 = sin(x);

𝑦2 = cos(x);

plot(x, 𝑦1 , 𝑥 , 𝑦2, )

title(‘ graph of sinwave and coswave ‘)

x label (‘ x-axis : 0<x<2*pi’)

y label (‘ plots of sin(x) and cos(x) ‘ )

written by : S. Mahaboob shareef , Lecture in ECE


47
iii ) degend command :

it will add legend to a plot legend(‘sine wave’ , ‘cos wave’)

. x = [ 0:0.01:2*pi];

𝑦1 = sin(x);

𝑦2 = cos(x);

plot(x, 𝑦1 , 𝑥 , 𝑦2, )

legend(‘sine wave’ , ‘ cos wave ‘)

legend(‘sine wave’ , ‘ cos wave ‘, ‘location’, ‘best’)

written by : S. Mahaboob shareef , Lecture in ECE


48
legend(‘sine wave’ , ‘ cos wave ‘, ‘location’, ‘east’)

iv) ezplot:

it will plot a function in the range -2π to 2π , 2π= 2*3.14 (-6.28 to +6.28)

1. ezplot(‘sin(x)’)

written by : S. Mahaboob shareef , Lecture in ECE


49
2. ezplot(‘tan(x)’)

v) sub plot :

it will create multiple figures in a single figure window

SYNTAX :

sub plot ( m,n,p )

written by : S. Mahaboob shareef , Lecture in ECE


50
m = number of rows

n = number of coloumns

p = ( where figures should present), index of the current plot

EXAMPLE:

x = [ 0:0.01:2*pi];

𝑦1 = sin(x);

𝑦2 = cos(x);

𝑦3= x.^2;

𝑦4 = tan(x);

subplot(2,2,1),plot(x , 𝑦1), subplot(2,2,2),plot(x , 𝑦2), subplot(2,2,3),plot( x , 𝑦3),


subplot(2,2,4),plot(x, 𝑦4)

vi) pie() :

to draw a pie chart

EXAMPLE:

1. x=[ 100 200 300 400];

written by : S. Mahaboob shareef , Lecture in ECE


51
labels={‘first’, ‘second’, ‘third’, fourth’ };

pie(x,lables)

2. x=[ 100 200 300 400];

labels={‘first’, ‘second’, ‘third’, fourth’ };

cut = [ 0 1 0 0];

pie(x, cut , lables);

vii) bar() :

to draw a bar chart

EXAMPLE:

x= [ 2001 : 1 : 2005];

written by : S. Mahaboob shareef , Lecture in ECE


52
y=[ 7.6 6.8 8.9 9.8 12];

bar(x,y)

bar(x,y,’ Edge color’,’red’)

• to reduce the size of bar

bar (x,y,.40,’Edge color’ , ‘red’ )

written by : S. Mahaboob shareef , Lecture in ECE


53
SMALL QUESTIONS
UNIT-1 : C PROGRAMMING BASICS
1.List the three logical operators supported by C ?

OPERATOR MEANING
i) && : logical AND
ii) // : logical OR
iii) ! : logical not operator

2.List the relational operators supported ?

OPERATOR MEANING
i) < : less than
ii) <= : less than or equal to
iii) > : greater than
iv) >= : greater than or equal to
v) == : equal to
vi) != : Not equal to

3.List the arithmetic operators supported by C ?

OPERATOR MEANING

i) + : Addition
ii) - : Subtraction
iii) * : Multiplication
iv) / : Division
v) % : Moduls

4. List the three bitwise logical operators ?

OPERATOR MEANING

i) & : Bitwise AND

ii) / : Bitwise OR

written by : S. Mahaboob shareef , Lecture in ECE


54
iii) >> : Right shift

iv) << : Left shift

v) ^ : Bitwise X-OR

vi) ~ : one’s complement

5. List the data types used in C ?

A data type specifies the type of data that a variable can store such as integer, floating and
character etc.

TYPES OF DATA TYPES IN C:

1.Primary data types : int, flot, char, double,void

2.Derived data types : Functions, arrays, pointers

3. user defined data types: structure, union , enum , typedef

6. Define keywords and list them ?

A Keyword is a word that carries special meaning. 32 keywords available in C: the key wors are
listed below

EXAMPLE:

auto double int struct

break else long switch

case enum register typedef

char exterm returm union

const float short unsigned

continue for signed void

default goto size of volatile

do if static while

7.Mention the character set of C language ?

written by : S. Mahaboob shareef , Lecture in ECE


55
‘C’ allows valid alphabets, numbers and special symbols

1. Alphabets : A,B,C,D,…………………….,X,Y,Z

a,b,c,d,………………………,x,y,z

2.Digits : 0,1,2,3,4,5,6,7,8,9

3.Special symbols: ~ ,’,!,@,#,%,^,&,*,(),-,_,+,=,| , / , \ , { , } , [ , ] , : , ; , ”, >, <, ?, .

8.Define constant and variables ?

A constant is an entity that doesn‘t change, where as, a variable is an entity that may change

EXAMPLE:

Variables : int a,b,c;

constants : 1.#define PI 3.14

2. cons tint a=5;

9. Explain the increment and decrement operators ?

1. Incremet:

“ The increment operator” is dented by ++ symbol. It has two types, pre increment and post increment
operators

(i): Pre increment: The pre increment is used to increase the original value of the variable by 1. before
assigning it to the expression.

SYNTAX: b=++a;

EXAMPLE: a=5

b=++a;

output; a=5 b=6

Frist a is increment by 1 ( a=6) & it is assigned to b

(ii) post increment : The post increment operator is used to increment the original value of a variable by
1 after assigning it to the expression

SYNTAX: b= a++;

EXAMPLE: a=5;

b=++a;

written by : S. Mahaboob shareef , Lecture in ECE


56
First assign to the expression b=5

and then its value is incremented a=6

2. Decrement:

The decrement operator is dented by ---. IT has two types pre decrement and post decrement

(i) pre decrement: The pre decrement operator is used to decrease the original value of a variable by 1.
before assigning it to the expression

SYNTAX: b= --a;

EXAMPLE: a=5

b= --a;

output; a=5 b=4

After execution a=4 , b=4

(ii) post decrement : The post decrement operator is used to increment the original value of a variable by
1 after assigning it to the expression

SYNTAX: b= a --;

EXAMPLE: a=5;

b=--a;

After execution b=5 a=4

10. Mention the type conversion techniques and discuss them ?

In C , programing language type of conversion is a technique to convert a value from one data
type to another data type

• there are two types of data conversion : 1) implicit 2) Explicit

1. Implicit type of conversion : Implicit type conversion happens automatically by the computer , during
the execution of the program In this technique , the conversion of one data type into another data type by
the complier.

EXAMPLE: # include <stdio.h>

int main ()

written by : S. Mahaboob shareef , Lecture in ECE


57
int i =65;

char j=’A’;

if(i==j)

printf(“ cis wow\n”)

else

printf(“ c is a headache \n”)

return 0;

OUTPUT: c is wow

2.Explicit conversion : Explicit type conversion is a technique where the programmer manually converts
one data type to another. This is also called as “ Type casting”

SYNTAX: (type)expression

EXAMPLE: # include <stdio.h>

int main ()

int i=2,j=3,k,l;

float a,b; }

k= i/ j*j; OUTPUT:

l= j/i*i; k= 2/3 *3 =0*3=0

a=(flot) i/j*j; l=3/2*2 =1*2=2

b=(flot) j/i*i; a=2/3*3=0.67*3=2.00

printf(“%d\n%d\n%f\n%f”,k,l,a,b); b= 3/2*2=1.52*=3.00

return 0;
written by : S. Mahaboob shareef , Lecture in ECE
58
UNIT-II : CONDITIONAL STATEMENTS AND ARRAYS
1.Define an array?

An array is defined as the collection of similar type of data items stored at contiguous
memory locations arrays are derived data type in C

SYNTAX: data_type array_name[array_site];


EXAMPLE : int a[10];

2.List the four conditional statements supported by C ?

i) if
ii) if-else
iii) nested if-else
iv) switch

3.List three types of iterative statements supported by C ?

i) for

ii) while

iii) do-while

4. Differentiate while and do while loops ?

while do-while
1. This is entry controlled loop
2. The control statement is executed before the 1. This is exit controlled loop
loop body 2. the control statement is present after the loop
3.the loop executes when the condition becomes body
true 3. the loop body executes once, even if the
4. SYNTAX: condition is true or false
while (condition) 4. SYNTAX:
{ do
“body of the loop” {
} “ body of the loop ”
}
while(condition);

5. Differentiate break and continue statements ?

written by : S. Mahaboob shareef , Lecture in ECE


59
Break Continue

1. SYNTAX : break; 1. SYNTAX: continue;

2. THE ‘break ’ statement terminates the loop. i.e; 2. continue statement result in skipping the loop’s
exited from the loop immediately current iteration and continuing from the next
iteration
3. used in loops and switch case
3. used in loops

6. Write the syntaxes of the following conditional statements 1. if ,2. if-else and 3.nested if else ?

(i) If: (ii). if-else: (iii) nested if-else:

if(condition) if(condition) if(condition 1)

{ { {

// statement // statement // statement

} } {

else else if(condition 2)

{ {

// statement // statement

} }

else if (condition 3)

// statements

else

// statement }

written by : S. Mahaboob shareef , Lecture in ECE


60
UNIT-III : STRINGS, FUNCTIONS & POINTERS
1. Define the pointer ?

A Pointer is a variable that stores the memory address of another varaiable

SYNTAX: data-type *pointer-name;

EXAMPLE: int a;

int *p;

p= &a;

Pointer variable ‘p’ points to the memory location of ‘a’ variable.

2. State the use of function in C ?

A Function can be defined as a set of statements that collectively performs some task.

Use of function:

1. It provides code reusability (avoids repetition of codes)

2. Increases program readability

3. IT divides significant complex problems into simple ones

4. It reduces the chance of errors

5. a function can accept arguments(parameters) which can be used to pass values (OR) data between
different parts of a program

3. state the use of return statement in C ?

1. The return statement” is used to return a value from a function to the calling code.

2. When a return statement is executed, the function stops executing and control is transferred to the
calling code along with the specified return value

written by : S. Mahaboob shareef , Lecture in ECE


61
4. List any three functions used for reading & writing OF STRINGS ?

Functions used for read strings: 1.gets

2. scanf

3.getchar

Functions used for writing strings: 1. puts

2. printf

3. putchar

5. Differentiate address and dereferencing operators?

Address operator Dereferencing operator

1. The symbol of address operator is ‘&’ 1. The symbol of dereferencing operator is


2. the ‘&’ operator returns the memory address of a ‘ *’
variable 2. the * operator is used to acess the valuie stored at the
3.EXAMPLE: memory location pointed by a pointer.
int x=10; 3.EXAMPLE:
int *p; s int x=10;
p=&x; int *p;
The address of x is stored in the pointer ‘ P ’ p=&x;
The value stored at the x address can be
acceseed using ‘*p ’

written by : S. Mahaboob shareef , Lecture in ECE


62
UNIT-IV : STRUCTURES & UNIONS
1. Define a structure in C ?

A Structure is a user defined data type that allous you to group together variables of
different data types under a single name.

SYNTAX: struct structure_name

data_type member1;

data_type member 2;

data_type member3;

};

EXAMPLE: struct book

char name[20];

char author[20];

int pages;

float price;

2.Define a union ?

A union is a data type that allous string different data types in the same memory location.
union is defined by using the “ union ” key-word,followed by union name and list of members variables
enclosed in braces.

EXAMPLE: union book

char name[20];

char author[20];

int pages;

flot price;

written by : S. Mahaboob shareef , Lecture in ECE


63
}

3.List the six preprocessor directives ?

i) # include

ii) # define

iii) # if

iv) # elif

v) # undef

vi) # ifdef

vii) # ifndef

4. Differentiate between structure and union ?

Structure union

1.’ struct ’ keyword is used to declare structure 1. ‘ union ’ keyword is used to declare union
2. we can access all the members of structure at any 2. only one member of union can be accessed at any
time time
3. Memory is allocated for all members 3. allocates memory for member which member
4. the size of the structure equal to the sum of the requires more members
size of its data members 4. The size of the union is equal to the size of the
5. IN the case of a structure, a user can initialize largest member among all the data members
multiple members at the same time 5. IN the case of a union, a user can only inistiate
6. SYNTAX: the first member at a time.
struct structure_name 6.SYNTAX:
{ union union_name
data_type member1; {
data_type member 2; data_type member1;
data _type member 3; data_type member 2;
: data _type member 3;
: :
: :
data_type member n; :
}; data_type member n;
};

written by : S. Mahaboob shareef , Lecture in ECE


64
5. State the function of pre processer directives in C ?

1. “pre processor directives” in C are instructions to the c pre processer, which is a program that
processes the source code before it is complied

2. pre processer directives can be used to include header files

3. Pre processer directives can be used to define Macros or to define a constant

4. pre processer directives can be used to generate error message.

5. Pre processer directives can be used for conditional compilation.

6. A Pre processer works on the source code and creates expanded source code

6.Explain how to find size of structure ?

To find size of the structure , we can use sizeof operator

EXAMPLE :

# include < stdio.h>

struct book

char name [20];

char author[10];

int pages;

float price;

};

void main ()

struct book b1 ,b2 ;

printf(“ size of the structure = %f” sizeof( b1 ));

OUTPUT :

38 bytes

written by : S. Mahaboob shareef , Lecture in ECE


65
UNIT –V : BASICS OF MATLAB

1.State the need for MATLAB in solving engineering problems ?

1. Engineers can use MATLAB to create “ Visual representation of data “ , which is crucial for analyzing
and understanding Engineering problems.

2. Numerical Computation : MATLAB uses built in libraries for solving complex numerical problems

3.Simulation: MATLAB uses various built-in tools for simulating physical systems ,which is useful for
engineers who need to test and optimize the design

4. MATLAB can be customized to suit specific Engineering nedds

2. List the major differences between C & MATLAB ?

C MATLAB

1. C doesn’t have a built-in graphical user 1. MATLAB has a built-in graphical user interface
interface that makes it easier for users to internet with
software
2. C has a smaller range of built-in function
libraries than Matlab 2. MATLAB has extensive range of built-in libraries
for numerical computation and optimization
3. C is a general purpose language
3.MATLAB is used for solving engineering
4. C is open source, & has a faster execution speed problems
than Matlab
4. MATLAB is expensive & is slower execution
5. ‘ C ’ is a complied language speed than ‘ C ’

5.MATLAB is an interpreted language

written by : S. Mahaboob shareef , Lecture in ECE


66
3. List the common input and output functions in MATLAB ?

INPUT OUTPUT
1. input 1. disp
2. fscanf 2. plot
3. textscan 3. fprintf
4. xlsread 4. error
5. esvread 5. save
6. imread 6. warring
7. audioread

4.State the use of (i) linspace operator (ii) clc, clear , who , whos , commands ?

(i)Linspace:

‘ Linspace ’ function is used to generate a vector of linearly spaced points between two
specified end points.

SYNTAX: linspace (x1 , x2 , n ) total vectors

starting point end point


𝑥2−𝑥1
the spacing between points : 𝑛−1

6−1 5
Example: linspace (1 , 6, 6) = =1
6−1 5

= 1,2,3,4,5,6

(ii) clc: clear the command window

clear: this will clear all variables from the workspace

who: this will list all variables currectly defined in the workspace

whos: This will display a table of all variables, including their names, sizes , data types ,and memory
usage

written by : S. Mahaboob shareef , Lecture in ECE


67
5. List the Arithmetic, Relation and logical operator in MATLAB ?

Arithmetic operations :

SYMBOL MEANING

i) + Addition
ii) - Subtraction
iii) * Matrix multiplication
iv) .* Array multiplication (OR) Element wise multiplication
v) / Matrix right division
vi) ./ Element wise right division
vii) \ Matrix wise left division
viii) . \ Element wise left division
ix) ^ matrix power
x) | Transpose

Relation operator :

SYMBOL MEANING

i) == Equal to
ii) ~= Not equal to
iii) > greater than
iv) >= greater than or equal to
v) < Less than
vi) <= Less than or equal to

Logical operator:

SYMBOL MEANING

i) && logical AND


ii) || logical OR
iii) ~ logical NOT

written by : S. Mahaboob shareef , Lecture in ECE


68
10 MARKS QUESTIONS
1. consider the following C program ?

# include <stdio.h>

int main()

int i,j,k=0;

j=2*3/4 =2.0/5 =8/5;

k=--j;

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

switch(i+k)

case 1:

case 2: printf(“ \n%d”,i+k);

case 3: printf(“ \n%d”,i+k);

default : printf(“ \n%d”,i+k);

return 0;

The number of times printf is executed is ________________

ANSWER:

j=6/4+0+1

j = 1+0+1=2

k= --j = 1 ( pre decrement )

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


for i =0 switch(0+1) case1 , case2 , case3, default will be executed

written by : S. Mahaboob shareef , Lecture in ECE


69
Here 3 printf statements are executed

for i = 1: switch(1+1) case1 , case2 , case3, default will be executed

Here 3 printf statements are executed

for i = 2: switch(2+1) case3, default will be executed

Here 2 printf statements are executed

for i = 3: switch(3+1) default will be executed

for i = 4: switch(4+1) default will be executed

for loop terminates.

Total : 3+3+2+1+1 = 10 times printf will be executed.

2. Consider the following C program ?

# inculdee<stdio.h>

int f1(void);

int f2(void);

int x=10;

int main()

int x=1;

x += f1()+f2()+f3()+f4()+f2();

printf(“ %d ”,x);

return 0;

int f1() { int x=25; x++; return x; }

int f2() {static int x=50; x++; return x; }

int f3() { int x=10; x++; return x; }

written by : S. Mahaboob shareef , Lecture in ECE


70
The output of the program is _________________

ANSWER:

1. f1() calls int f1() it returns “ 26 ”

2. f2() calls int f2() it returns “ 51 ”

3. f1() calls int f3() it returns x =10*x

=10*10

= 100

4. f2() intf2() it returns 52( because x is a static variable)

x= x+f1()+f2()+f3()+f2()

= 1+26+51+100+52

=230

_______________________________________________________________________________________

3. Consider the following C program ?

# include<stdio.h>

int jumble(intx ,int y) {

x= 2*x+y;

return x;

int main() {

int x=2 , y=5;

y= jumble ( y, x); y= jumble (5,2)

x= jumble (y,x);

print f (“ %d\n”,x);

return 0;

ANSWER:

written by : S. Mahaboob shareef , Lecture in ECE


71
y= jumble ( y,x)

jumble (5,2)

int jumble (int x, inty);

x=2*y=x

return x;

x=5 , y=2

x=2*5+2

return x ;

Y=12

x= jumble (y,x); jumble(12,2)

int jumble (int x, int y)

x=12 , y=2

2*12+2=26 x=jumble ( y, x); x=26

In the main function printf(“ %d”,x); , prints 26

_______________________________________________________________________________________

4. Consider a following C program ?

# include<stdio.h>

int main()

int arrr[ ]={1,2,3,4,5,6,7,8,9,0,1,2,5}, *ip= arry+4 ;

printf(“ %d\n”, ip[1]);

return 0;

ANSWER:

arr points : starting address of array

written by : S. Mahaboob shareef , Lecture in ECE


72
a[0] a[1] a[2] a[3] a[4]

1 2 3 4 5 6 7 8 9 0 1 2 5

1000 1004 1008 1012 1016 1020 1024 1028 1032 1036 1040 1044 1048

now ip =1016 >> ip(0) ip ip+1

*(ip+1) = ip[1] =6

_______________________________________________________________________________________

5. Consider the following C program ?

# inculde <stdio.h>

void main()

int num=50;

int*1;

p= &num ;

printf(“ Address of p variable is %u\n”, p);

p = p-1 ; 1000

printf(“ After decrement : Address of p variable is %u\n ,p ”);

} 996

ANSWER:

num = 50

Assume num address is=1000

p=1000

p = p-1;

= 1000-4

= 996

[ Every integer occupies 4 bytes]

_______________________________________________________________________________________

written by : S. Mahaboob shareef , Lecture in ECE


73
6.Consider the following C program ?

# inculde<stdio.h>

int main()

int a[ ]={ 3,2,6,7,0,5,6 }

int *p; 1000

p=a;

printf(“ %d”, *p++);

return 0; post increment first it will print

ANSWER:

p=a; p=1000

_______________________________________________________________________________________

7.Consider the following C program ?

# include<stdio.h>

int counter=0;

int calc ( int a, int b ) {

int c;

counter ++;

if(b==3) return (a*a*a);

else {

c= calc (a,b/3);

return (c*c*c);

written by : S. Mahaboob shareef , Lecture in ECE


74
int main() {

calc (4,81);

printf(“%d”,counter) ;

ANSWER:

(i). calc (4,81)

calling

int calc (int a ,int b)

a=4 , b=81

counter=1

b not equal to 3,then c=calc( 4,81/3)

c=calc(4,27)

____________________________

(ii). calling same function again:

b not equal to 3,then c=calc( 4,27/3)

c=calc(4,9)

counter = 2

______________________________

(ii). calling same function again:

b not equal to 3,then c=calc( 4,9/3)

c=calc(4,3)

counter = 3

_____________________________

(iv). calling the same function again

written by : S. Mahaboob shareef , Lecture in ECE


75
b=3, counter =4

return 4*4*4

=64

printf(“ %d”,conter); , prints 4

_______________________________________________________________________________________

8. What is the output of the following program ?

# inculde <stdio.h>

int main ( int argc, char * argv [ ])

int x=1, Z[2]={10,11};

int *p=NULL;

p=x;

*p=10;

p= Z[1];

*( & Z[0]+1 ) + =3 ;

printf (“ %d,%d,%d\n”, x ,Z[0],Z[1]) ;

ANSWER:

x=1, Z[0]=10, Z[1]=11

int *p=NULL ; NULL pointer

p=&x; p is holding address of x

*p =10; 10

now p=& Z[1]; x = 1000 ( address )

p is holding address of z[1]

*( & Z[0]+1)

address of z[0] + 1 , means of address of next element z[1]

written by : S. Mahaboob shareef , Lecture in ECE


76
*(Z[1]) means value at z[1]

*( Z[1] )+3; *(z[1])=*(z[1])+3

=11+3

=14

printf(“ %d,%d,%d”, x , z[0], z[1])

10 10 14

_______________________________________________________________________________________

9. Consider the following C program ?

# inculde<stdio.h>

void main()

int a;

int *ip;

float *ip;

voids *vp;

printf(“%d,*vp”);

ANSWER:

error

we con’t dereferencing void pointer .

______________________________________________________________________________________

10. Consider the following C program ?

# inculde <stdio.h>

void main()

int a=5;

written by : S. Mahaboob shareef , Lecture in ECE


77
char c= ‘p’;

int *ip;

float *ip;

char *cp;

vp= &a;

printf(“ %d ”, *(int *)vp);

vp=&c;

printf(“ %c”,*(char *)vp);

ANSWER:

vp=&a;

void pointer holds address of integer ‘a’

void pointer is type casted to integer pointer , so points : 5

• another printf prints : 1

_______________________________________________________________________________________

written by : S. Mahaboob shareef , Lecture in ECE


78

You might also like