C Language Notes-1
C Language Notes-1
IV TH SEM
DECE
Prepared BY :
S.MAHABOOB SHAREEF
LECTURE IN ECE
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
ANSWER:
void main()
int i, sum=0,n;
clrscr();
scanf (“%d”,&n) ;
sum = sum+i;
getch();
2. Write a progam to find whether the given number is Armstrong NUMBER or NOT?
Answer:
# include <stdio.h>
# include<conio.h>
int num,sum=0,rem,temp;
temp = num ;
while (num>0)
rem = num%10;
sum = sum+rem*rem*rem ;
num = num/10 ;
if ( temp == sum )
else
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();
}
#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() ;
}
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();
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);
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();
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();
ANSWER:
#include<stdio.h>
#include<conio.h>
void main()
int a[3][3],b[3][3],c[3][3],i,j,k;
clrscr();
scanf(“%d”,&a[i][j]);
{
written by : S. Mahaboob shareef , Lecture in ECE
10
for(j0; j<3; j++)
scanf(“%d”,&b[i][j]);
c[i][j]=0;
C[i][j]=c[i][j]+a[i][k]*b[k][j];
printf(“resultant matrix-C\n”);
printf(“%d\t”, c[i][j] ) ;
printf(“\n”);
Answer:
#include<stdio.h>
#include<conio.h>
void main()
int n,a[10],i,j,temp;
clrscr();
scanf(“%d”,&n);
scanf(“%d”,&a[i]);
if(a[i]>a[j])
temp=a[i];
a[ j ]=temp;
printf(“%d \t”,a[i]);
getch();
Answer:
#include<stdio.h>
#include<conio.h>
void main()
int a[20],n,i;
clrscr();
scanf(“%d”,&n);
scanf(“%d”,&a[i]);
a[0]=a[i];
getch();
ANSWER:
#include<stdio.h>
#include<conio.h>
void main()
clrscr();
scanf(“%d”,&n );
scanf(“%d”,&a[i]);
sum = sum+a[i];
getch();
ANSWER:
#include<stdio.h>
#include<conio.h>
Void main()
int a, b, c, big ;
clrscr();
scanf(“%d%d%d”,&a,&b,&c);
big=(a>b)?(a>c?a:c):(b>c?b:c);
printf(“biggest number=%d”,big);
getch();
Answer:
#include<stdio.h>
#include<conio.h>
void main()
int a,b,c;
clrscr();
scanf(“%d%d%d”,&a,&b,&c);
if((a>b)&&(a<c))
else if (b>c)
else
getch();
#include<stdio.h>
#include<conio.h>
void main()
int year;
clrscr();
scanf(“%d”,year);
else
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;
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;
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();
scanf(“%d”,&option);
switch(option)
Case 1:
break;
Case 2:
break;
Case 3:
break;
Case 4:
break;
Case 5:
break;
default :
getch();
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)
return 0;
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<conio.h }
{ {
scanf(“%d”/&n); getch();
if(n<50) }
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)
} {
{ }
// statements
EXAMPLE:
{ { printf(“\n saturday”);
{ printf(“\n Thursday”); }
} else if (n==5) }
else if (n==2) {
{ printf(“\n Friday”);
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:
// statements
EXAMPLE:
# include<stdio.h>
#include<conio.h>
void main()
int n,sum=0;
scanf(“%d”,&n);
for(i=1;1<=n;i++)
sum=sum+i;
getch();
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> {
{ i++;
int i=0; }
while(i<5) return 0;
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);
#include<stdio.h> do{
{ i++;
return 0;
ANSWER:
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
#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)
{
OUTPUT:
Before swapping a=5, b=10
After swapping a=10 ,b=5
_______________________________________________________________________________________
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();
Return 0;
OUTPUT :
getch():
EXAMPLE:
#include<stdio.h>
#include<conio.h>
int main()
char ch;
ch = getch();
return 0;
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;
return 0;
you entered h
putchar():
EXAMPLE:
{ }
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()
return 0 ;
Strcmp() :
strcmp() is a standard library function in c programming language that is used to compare two
strings
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()
int i,j,k;
i= strcmp(string1, “Jerry”);
printf(“%d%d”,i,j);
return 0;
OUTPUT: 0 4
‘strcpy () ’ is a standard libaryb function in C programming language that is used to copy a string from one
location to another
EXAMPLE:
#include<stdio.h>
#include<string.h>
int main()
return 0;
OUTPUT:
Strlel() :
EXAMPLE:
#include<stdio.h>
#include<string.h>
int main()
int len;
len = strlen(str1);
return 0;
#include<stdio.h> scanf(“%f”,&b[i].price);
#include<conio.h> }
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; }
_______________________________________________________________________________________________
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
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
#endif
3. #ifndef:
example:
#ifndef RELEASE_MODE
#endif.
4. #elif:
this directive is used with #if to test additional conditions if the previous condition is false.
example:
#if SOME_CONSTANT==42
#elif SOME_CONSTANT==43
#else
#endif
5. #else:
this directive is used with #if to include code that will executes if the condition is false
example:
#else
#enif
6.. #endif:
_______________________________________________________________________________________________
1. # define :
# define PI 3.14
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.
This code includes the standard input / output header file in the source code
3. # undef:
EXAMPLE:
int x;
x= Mul (6,7);
printf(“ %d\n”,x);
/* Marco prints 42 */
/* undefining Marco */
printf(“ c is wow”);
# endif
OUTPUT: 42
c is wow
4. #error:
# 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
>> 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
>> 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)
____________________________________________________________________________________
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
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
end
In the above example x=5, so the condition is ture, if- block will be excuted
OUTPUT : x is positive
Syntax :
else
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
else
_______________________________________________________________________________________
while loop :
Syntax:
while condition
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;
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
_______________________________________________________________________________________
i) SIMULINK : SIMULINK is a graphical programming environment in MATLAB that allows you to model,
simulate and analyze dynamic systems.
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.
Simulink provides tools for designing and simulation of control system. we can simulate PID
controllers , state space controllers etc..
4. CODE GENERATION:
Simulink supports real time simulation and rapid prototyping of embedded systems.
Simulink also used in signal processing algorithms and Image processing systems
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.
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:
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)
𝑦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:
. x = [ 0:0.01:2*pi];
y = sin(x);
plot (x,y)
. x = [ 0:0.01:2*pi];
𝑦1 = sin(x);
𝑦2 = cos(x);
plot(x, 𝑦1 , 𝑥 , 𝑦2, )
. x = [ 0:0.01:2*pi];
𝑦1 = sin(x);
𝑦2 = cos(x);
plot(x, 𝑦1 , 𝑥 , 𝑦2, )
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)’)
v) sub plot :
SYNTAX :
n = number of coloumns
EXAMPLE:
x = [ 0:0.01:2*pi];
𝑦1 = sin(x);
𝑦2 = cos(x);
𝑦3= x.^2;
𝑦4 = tan(x);
vi) pie() :
EXAMPLE:
pie(x,lables)
cut = [ 0 1 0 0];
vii) bar() :
EXAMPLE:
x= [ 2001 : 1 : 2005];
bar(x,y)
OPERATOR MEANING
i) && : logical AND
ii) // : logical OR
iii) ! : logical not operator
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
OPERATOR MEANING
i) + : Addition
ii) - : Subtraction
iii) * : Multiplication
iv) / : Division
v) % : Moduls
OPERATOR MEANING
ii) / : Bitwise OR
v) ^ : Bitwise X-OR
A data type specifies the type of data that a variable can store such as integer, floating and
character etc.
A Keyword is a word that carries special meaning. 32 keywords available in C: the key wors are
listed below
EXAMPLE:
do if static while
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
A constant is an entity that doesn‘t change, where as, a variable is an entity that may change
EXAMPLE:
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;
(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;
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;
(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;
In C , programing language type of conversion is a technique to convert a value from one data
type to another data type
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.
int main ()
char j=’A’;
if(i==j)
else
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
int main ()
int i=2,j=3,k,l;
float a,b; }
k= i/ j*j; OUTPUT:
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
i) if
ii) if-else
iii) nested if-else
iv) switch
i) for
ii) while
iii) do-while
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);
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 ?
{ { {
} } {
{ {
// statement // statement
} }
else if (condition 3)
// statements
else
// statement }
EXAMPLE: int a;
int *p;
p= &a;
A Function can be defined as a set of statements that collectively performs some task.
Use of function:
5. a function can accept arguments(parameters) which can be used to pass values (OR) data between
different parts of a program
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
2. scanf
3.getchar
2. printf
3. putchar
A Structure is a user defined data type that allous you to group together variables of
different data types under a single name.
data_type member1;
data_type member 2;
data_type member3;
};
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.
char name[20];
char author[20];
int pages;
flot price;
i) # include
ii) # define
iii) # if
iv) # elif
v) # undef
vi) # ifdef
vii) # ifndef
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;
};
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
6. A Pre processer works on the source code and creates expanded source code
EXAMPLE :
struct book
char author[10];
int pages;
float price;
};
void main ()
OUTPUT :
38 bytes
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
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 ’
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.
6−1 5
Example: linspace (1 , 6, 6) = =1
6−1 5
= 1,2,3,4,5,6
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
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
# include <stdio.h>
int main()
int i,j,k=0;
k=--j;
for (i=0;i<5;i++)
switch(i+k)
case 1:
return 0;
ANSWER:
j=6/4+0+1
j = 1+0+1=2
# 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;
ANSWER:
=10*10
= 100
x= x+f1()+f2()+f3()+f2()
= 1+26+51+100+52
=230
_______________________________________________________________________________________
# include<stdio.h>
x= 2*x+y;
return x;
int main() {
x= jumble (y,x);
print f (“ %d\n”,x);
return 0;
ANSWER:
jumble (5,2)
x=2*y=x
return x;
x=5 , y=2
x=2*5+2
return x ;
Y=12
x=12 , y=2
_______________________________________________________________________________________
# include<stdio.h>
int main()
return 0;
ANSWER:
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
*(ip+1) = ip[1] =6
_______________________________________________________________________________________
# inculde <stdio.h>
void main()
int num=50;
int*1;
p= &num ;
p = p-1 ; 1000
} 996
ANSWER:
num = 50
p=1000
p = p-1;
= 1000-4
= 996
_______________________________________________________________________________________
# inculde<stdio.h>
int main()
p=a;
ANSWER:
p=a; p=1000
_______________________________________________________________________________________
# include<stdio.h>
int counter=0;
int c;
counter ++;
else {
c= calc (a,b/3);
return (c*c*c);
calc (4,81);
printf(“%d”,counter) ;
ANSWER:
calling
a=4 , b=81
counter=1
c=calc(4,27)
____________________________
c=calc(4,9)
counter = 2
______________________________
c=calc(4,3)
counter = 3
_____________________________
return 4*4*4
=64
_______________________________________________________________________________________
# inculde <stdio.h>
int *p=NULL;
p=x;
*p=10;
p= Z[1];
*( & Z[0]+1 ) + =3 ;
ANSWER:
*p =10; 10
*( & Z[0]+1)
=11+3
=14
10 10 14
_______________________________________________________________________________________
# inculde<stdio.h>
void main()
int a;
int *ip;
float *ip;
voids *vp;
printf(“%d,*vp”);
ANSWER:
error
______________________________________________________________________________________
# inculde <stdio.h>
void main()
int a=5;
int *ip;
float *ip;
char *cp;
vp= &a;
vp=&c;
ANSWER:
vp=&a;
_______________________________________________________________________________________