You are on page 1of 40

Function, Structure, Union, File Handling, Exception, Link

List, Stack, Queue,Graph,..etc.


1
Operator and Expression
C - Programming Language

PPT CREATED BY : Prof. Rajesh K. Jha


Basic, Control Structure, Looping, Array, Pointer
C - Programming Language

List, Stack, Queue,Graph,..etc.


Function, Structure, Union, File Handling, Exception, Link
Operators
Basic, Control Structure, Looping, Array, Pointer

Operator is a symbol that tells the computer to perform certain mathematical or


logical manipulations. Operators are used in program to manipulate data and
variables. Operators usually form a part of mathematical or logical expressions.

Types of operators :

1. Arithmetic Operator
2. Relational Operator
3. Logical Operator
4. Assignment Operator
5. Increment and Decrement Operator
6. Conditional Operators
7. Bitwise Operators
8. Special Operator

PPT CREATED BY : Prof. Rajesh K. Jha 2


C - Programming Language
Arithmetic Operator Example.

List, Stack, Queue,Graph,..etc.


Function, Structure, Union, File Handling, Exception, Link
Basic, Control Structure, Looping, Array, Pointer

# include <stdio.h>
int main ()
Operator Meaning {
int months, days;
+ Addition or printf (“Enter days \n “);
Unary Plus scanf (“%d”, & days);
months = days/30;
- Subtraction or day = days%30;
Unary Minus printf ( “ Month =%d Days =%d”, months,days );
return 0;
* Multiplication }

/ Division Note 1 : The operator % cannot be used with real


operands.
% Modulo Division
Note 2: Mixed-Mode Arithmetic :
One operand real and another is integer.

PPT CREATED BY : Prof. Rajesh K. Jha 3


C - Programming Language

List, Stack, Queue,Graph,..etc.


Function, Structure, Union, File Handling, Exception, Link
Relational Operator : Example :
Basic, Control Structure, Looping, Array, Pointer

#include <stdio.h>
Operator Meaning int main()
{
< Is less than int a,b;

<= Is less than or printf (“Enter two numbers =“);


equal to scanf(“%d%d”,&a,&b);
if (a >b)
> Is greater than {
printf (“Value of a is greater then b”);
>= Is greater than }
or equal to else
{
== Is equal to printf (“Value of b is greater then a”);
}
!= Is not equal to
return 0;
}
PPT CREATED BY : Prof. Rajesh K. Jha 4
C - Programming Language

List, Stack, Queue,Graph,..etc.


Function, Structure, Union, File Handling, Exception, Link
Logical Operator.
Basic, Control Structure, Looping, Array, Pointer

Operator Meaning X Y X && Y X||Y


1 1 1 1
&& and
1 0 0 1
|| or 0 1 0 1
0 0 0 0
! not

PPT CREATED BY : Prof. Rajesh K. Jha 5


C - Programming Language

List, Stack, Queue,Graph,..etc.


Function, Structure, Union, File Handling, Exception, Link
Logical Operator. ( && ) Example :
Basic, Control Structure, Looping, Array, Pointer

#include <stdio.h>
Example : int main()
#include <stdio.h> {
int main() int b_salary;
{ printf (“ Enter employee basic salary =“);
int a,b; scanf(“%d”, &b_salary);
printf (“ Enter value of a ,b =“);
scanf(“%d%d%d”,&a,&b); if (b_salary> 0 && b_salary <=10000)
{
if (a>50 && b <100) printf (“ Entered basic salary is correct”);
{ }
printf (“ Correct value”); else
} {
else printf (“Incorrect basic salary”);
{ }
printf (“Entered values is not correct”); }
}
} 6
PPT CREATED BY : Prof. Rajesh K. Jha
C - Programming Language
Logical Operator. ( || ) Logical Operator. ( II )

List, Stack, Queue,Graph,..etc.


Function, Structure, Union, File Handling, Exception, Link
Example :
#include<stdio.h>
Basic, Control Structure, Looping, Array, Pointer

#include <stdio.h> int main()


int main() {
{ char a='A',b='B’;
int num; if (a==b || a==‘A’)
printf (“Enter num value=“); {
scanf (“%d”, &num); printf ("A or operator rules is correct");
}
if ( num >100 || num <=500 ) else
{ {
printf (“You have entered true numbers”); printf ("Or rules is incorrect");
} }
else return 0;
{ }
printf (“ You have entered wrong number”);
}
}
PPT CREATED BY : Prof. Rajesh K. Jha 7
C - Programming Language
Assignment Operator:

List, Stack, Queue,Graph,..etc.


Function, Structure, Union, File Handling, Exception, Link
Basic, Control Structure, Looping, Array, Pointer

Operator Meaning Statement with Statement with


sample assignment shorthand operator
= Assign the result
operator
of an expression
to a variable. a=a+1 a+=1
a=a-1 a-=1

a=a*(n+1) a*=n+1
a=a/(n+1) a/=n+1
a=a%b a%=b

PPT CREATED BY : Prof. Rajesh K. Jha 8


C - Programming Language
Increment and Decrement Operator.

List, Stack, Queue,Graph,..etc.


Function, Structure, Union, File Handling, Exception, Link
Operator Meaning Rules for increment and decrement Operators :
Basic, Control Structure, Looping, Array, Pointer

++ Increment 1. Increment and decrement operators are unary operators


Operator and they require variable as their operands.

-- Decrement
Operator 2. When postfix ++ ( or - - ) is used with a variable in an
++num Pre Increment expression, the expression is evaluated first using the
original value of the variable and then the variable is
Operator
incremented (or decremented) by one.

num++ Post Increment


operator 3. When prefix ++ ( or - - ) is used in an expression, the variable
is incremented ( or decremented ) first and then one
- - num Pre decrement expression is evaluated using the new value of the variable.

num - - Post decrement 4. The precedence and associatively of ++ and - - operators


are the same as those of unary + and unary -.

PPT CREATED BY : Prof. Rajesh K. Jha 9


C - Programming Language
Increment and Decrement Operator.

List, Stack, Queue,Graph,..etc.


Function, Structure, Union, File Handling, Exception, Link
Example :
Operator Meaning
Basic, Control Structure, Looping, Array, Pointer

int main()
{
++ Increment int a,b,c,d;
Operator
a = 15;
-- Decrement
b = 10;
Operator c = ++ a - b;
++num Pre Increment
printf ( " a = %d b=%d c= %d \n ", a, b,c);
Operator
d =b++ + a;

num++ Post Increment printf ( " a = %d b=%d d= %d \n ", a, b,d);


operator printf ( " a/b = %d\n",a/b);
printf ("a%%d =%d\n",a%b);
printf ("a *= b = %d\n", a*=b);
- - num Pre decrement printf ("%d\n", (c>b)?1:0);
printf ("%d\n", (c<b)?1:0);

num - - Post decrement return ;

PPT CREATED BY : Prof. Rajesh K. Jha 10


C - Programming Language

Conditional or

List, Stack, Queue,Graph,..etc.


Function, Structure, Union, File Handling, Exception, Link
Ternary Operator.
Basic, Control Structure, Looping, Array, Pointer

PPT CREATED BY : Prof. Rajesh K. Jha 11


C - Programming Language
Ex:1:

List, Stack, Queue,Graph,..etc.


Function, Structure, Union, File Handling, Exception, Link
CONDITIONAL OR TERNARY OPERATOR: # include <stdio.h>
Basic, Control Structure, Looping, Array, Pointer

Conditional operator is a ternary operator because it int main ( )


works on three operands. {
int a, b , max=0;
printf (“ Enter two numbers : “);
The ternary operator behave like an if-else scanf ( “ %d%d”, &a,&b);
statement. max = (a>b) ? a : b ;
printf (“ Max number=%d” , max );
return 0;
Syntax : }
Ex:2:
expression-1 expression-2 expression-3
# include <stdio.h>
condition ? True : False int main ( )
{
int a, b, c , min = 0 ;
printf (" Enter three numbers : ");
scanf ( "%d%d%d", &a,&b,&c);
min = (a<b) ? (a<c ? a:c) : (b<c ? b:c ) ;
printf ("%d" , min );
return 0;
}

PPT CREATED BY : Prof. Rajesh K. Jha 12


C - Programming Language

List, Stack, Queue,Graph,..etc.


Function, Structure, Union, File Handling, Exception, Link
CONDITIONAL OR TERNARY OPERATOR: Ex:5:
Basic, Control Structure, Looping, Array, Pointer

Ex:3:
#include <stdio.h>
int main() Example :
{
int a = 11, b = 20, even=0; #include <stdio.h>
even= a%2 == 0 ? printf ("Even") : printf ("Odd"); int main()
return 0; {
} int num;
Ex: 4. printf("Enter your number=");
#include<stdio.h> scanf("%d",&num);
int main() (num>0)? (printf("True")) : (printf("False"));
{ return 0;
int a=10,b=30,z; }
z=(0)?a:b;
printf ("%d",z);
return 0;
}
Note : Zero always return false value.
Non-zero always return true values.
PPT CREATED BY : Prof. Rajesh K. Jha 13
C - Programming Language

List, Stack, Queue,Graph,..etc.


Function, Structure, Union, File Handling, Exception, Link
CONDITIONAL OR TERNARY OPERATOR Example :
Basic, Control Structure, Looping, Array, Pointer

Ex:6:
#include <stdio.h>
int main()
{
int a = 10, b = 20;
(a>b) ? printf("a is greater than b that is %d > %d", a, b)
: printf("a is greater than b that is %d > %d", a, b);
return 0;
}

PPT CREATED BY : Prof. Rajesh K. Jha 14


C - Programming Language

List, Stack, Queue,Graph,..etc.


Function, Structure, Union, File Handling, Exception, Link
BITWISE OPERATORS: Example : X Y X&Y X|Y
Basic, Control Structure, Looping, Array, Pointer

Operator Meaning Bitwise and (&)


7 -> 0 1 1 1
& Bitwise AND
4 -> & 0 1 0 0
-------------------
| Bitwise OR 4 <- 0 1 0 0 1 1 1 1

^ Bitwise exclusive 7&4=4 1 0 0 1


OR
Bitwise or (|) 0 1 0 1
<< Left shift 7 -> 0 1 1 1
4 -> | 0 1 0 0 0 0 0 0
>> Right shift -------------------
~ Not (Bitwise 7 <- 0 1 1 1
complement
7|4 = 7
operator)
PPT CREATED BY : Prof. Rajesh K. Jha 15
C - Programming Language

List, Stack, Queue,Graph,..etc.


Function, Structure, Union, File Handling, Exception, Link
BITWISE OPERATORS: Example :
Basic, Control Structure, Looping, Array, Pointer

X ~x
Operator Meaning
~ (not) : 0 1
& Bitwise AND
7 -> 0 1 1 1 1 0
-------------------
| Bitwise OR 8 <- 1 0 0 0
~7 = 8
~ Not (Bitwise
complement Note : not bitwise operator job is to
operator) complement each bit one by one.
<< Left shift

>> Right shift


^ Bitwise exclusive
OR
PPT CREATED BY : Prof. Rajesh K. Jha 16
C - Programming Language

List, Stack, Queue,Graph,..etc.


Function, Structure, Union, File Handling, Exception, Link
1. BITWISE AND ( & ) OPERATORS: X Y X&Y
Example :
Basic, Control Structure, Looping, Array, Pointer

Ex:1: Ex:2:
1 1 1
#include <stdio.h> #include <stdio.h>
int main() int main() 1 0 0
{ {
int a=7, b=4; int a, b;
printf("Output = %d", a&b); printf (" Enter two number"); 0 1 0
return 0; scanf ("%d%d",&a,&b);
} 0 0 0
printf("Output = %d", a&b);
return 0;
Bitwise And (&) Operator: }
7 -> 0 1 1 1
4 -> & 0 1 0 0 Output :
-------------------
4 <- 0 1 0 0

Output : 7 & 4 = 4
PPT CREATED BY : Prof. Rajesh K. Jha 17
C - Programming Language

List, Stack, Queue,Graph,..etc.


Function, Structure, Union, File Handling, Exception, Link
2. BITWISE OR ( | ) OPERATORS: X Y X|Y
Example :
Basic, Control Structure, Looping, Array, Pointer

Ex:1: Ex:2:

#include <stdio.h> #include <stdio.h>


int main() int main()
{ { 1 1 1
int a=7, b=4; int a, b;
printf("Output = %d", a|b); printf (" Enter two number=");
scanf ("%d%d",&a,&b); 1 0 1
return 0;
}
printf("Output = %d", a|b); 0 1 1
return 0;
Bitwise or (|) }
7 -> 0 1 1 1 0 0 0
4 -> | 0 1 0 0 Output :
-------------------
4 <- 0 1 1 1

Output : 7|4 = 7

PPT CREATED BY : Prof. Rajesh K. Jha 18


C - Programming Language

List, Stack, Queue,Graph,..etc.


Function, Structure, Union, File Handling, Exception, Link
3. Bitwise XOR (exclusive OR) operator ^ Example :
Basic, Control Structure, Looping, Array, Pointer

Ex:1: Ex:2:
#include <stdio.h>
int main()
{ #include <stdio.h>
int a = 12, b = 25; int main()
{
printf("Output = %d", a^b); int a, b;
return 0; printf (" Enter two number=");
} scanf ("%d%d",&a,&b);

00001100 printf("Output = %d", a^b);


^00011001 return 0;
________ }
00010101 = 21 (In decimal)
Note : Output :
if both bit is same then result will be 0
if both bit is not same then result will be 1

Note : short – 1 byte, int – 2 byte


PPT CREATED BY : Prof. Rajesh K. Jha 19
C - Programming Language

List, Stack, Queue,Graph,..etc.


Function, Structure, Union, File Handling, Exception, Link
4. Bitwise complement operator ~ Example :
Basic, Control Structure, Looping, Array, Pointer

Ex:1: Ex:2: X ~x
0 1
35 = 00100011 (In Binary) #include <stdio.h>
1 0
int main()
{
Bitwise complement Operation of 35
int a;
~ 00100011 printf (" Enter value of a=“);
________ scanf ("%d",&a);
11011100 = 220 (In decimal)
printf("Output = %d",~b);
return 0;
}

Output :

PPT CREATED BY : Prof. Rajesh K. Jha 20


C - Programming Language

List, Stack, Queue,Graph,..etc.


Function, Structure, Union, File Handling, Exception, Link
BITWISE Operator: Ex :2:
Basic, Control Structure, Looping, Array, Pointer

<< (Left shift operator ) :


When bits are shifted left then trailing #include <stdio.h>
position are filled with zeros. {
char a;
printf (“Enter any char value=“)l
Ex :1: scanf(“%d”, &a);
#include <stdio.h> printf (“%d”, a << 1 ) ;
{ return 0;
char a=3; }
printf (“%d”, a << 1 ) ;
return 0;
}

0000 0011
0000 0110
Output : 6. formula follow : [3 * 21]

PPT CREATED BY : Prof. Rajesh K. Jha 21


C - Programming Language

List, Stack, Queue,Graph,..etc.


Function, Structure, Union, File Handling, Exception, Link
BITWISE Operator: BITWISE Operator:
Basic, Control Structure, Looping, Array, Pointer

>> (right shift operator ) :


>> (right shift operator ) : When bits are shifted right then leading
position are filled with zeros.
When bits are shifted right then leading
position are filled with zeros. Ex:2:
Ex:1: #include <stdio.h>
{
char a;
#include <stdio.h> printf (“Enter value of a=“);
{ printf (“%d”, a >> 1 ) ;
char a=3; return 0;
printf (“%d”, a >> 1 ) ; }
return 0;
}
0000 0011 Output : 1. formula follow : [3 / 21]
0000 0001
Output : 1. formula follow : [3 / 21]

PPT CREATED BY : Prof. Rajesh K. Jha 22


C - Programming Language

List, Stack, Queue,Graph,..etc.


Function, Structure, Union, File Handling, Exception, Link
#include <stdio.h> #include <stdio.h>
Basic, Control Structure, Looping, Array, Pointer

int main() int main()


Arithmetic Expression :
{ {
Evaluation of Expression : float a,b,c,x,y,z; int a ;
Expressions are evaluated using an a=9; a= 5<=8 && 6!5 ;
assignment statement of the form. b=12; printf (“%d”, a);
c=3; return 0
variable = expression; }
x=a-b / 3 + c * 2 – 1;
y=a-b / (3+c) * (2-1);
#include <stdio.h> z=a- (b/ (3+c) * 2 ) -1 Output : 1
{
printf (“x =%f\n”, x );
short int a=5,b=7; printf (“y =%f\n”, y);
printf (“%d”, a^b ) ; printf (“z =%f\n”,z);
return 0;
return 0;
}
} Output : x = 10.
y=7
z= 4
PPT CREATED BY : Prof. Rajesh K. Jha 23
C - Programming Language

List, Stack, Queue,Graph,..etc.


Function, Structure, Union, File Handling, Exception, Link
Special Operators : Special Operators :
Basic, Control Structure, Looping, Array, Pointer

, (comma operator) :
The comma operator can be used to link the related sizeof operator :
expressions together. A comma-linked list of The sizeof is a compile time operator and, when
expressions are evaluated left to right and the value of used with an operand, it returns the numbers of bytes
right-most expression is the value of the combined the operand occupies.
expression.
value = (x=10, y=5, x+y);
The operand may be a variable, a constant or a data
Note : Comma operator has the lowest precedence of type qualifier.
all operators. That is the reason parentheses
are necessary. Ex:
m=sizeof (sum);
Below topic will be discuss in the chapter of pointer n= sizeof (long int);
and structure.
( & and * ) pointer operator : Pointer
(. And - > ) member selection operator : Structure

PPT CREATED BY : Prof. Rajesh K. Jha 24


C - Programming Language

List, Stack, Queue,Graph,..etc.


Function, Structure, Union, File Handling, Exception, Link
Special Operators : Output :
Basic, Control Structure, Looping, Array, Pointer

#include <stdio.h>
int main() { Size of variable a : 4
int a = 16; Size of int data type : 4
printf("Size of variable a : %d\n",sizeof(a)); Size of char data type : 1
printf("Size of int data type : %d\n",sizeof(int)); Size of float data type : 4
printf("Size of char data type : %d\n",sizeof(char)); Size of double data type :8
printf("Size of float data type : %d\n",sizeof(float));
printf("Size of double data type : %d\n",sizeof(double));
return 0;
}

PPT CREATED BY : Prof. Rajesh K. Jha 25


C - Programming Language
Precedence of Arithmetic Operator Precedence of Arithmetic Operator

List, Stack, Queue,Graph,..etc.


Function, Structure, Union, File Handling, Exception, Link
Ex:2:
An arithmetic expression without parentheses will be However, the order of evaluation can be changed by
Basic, Control Structure, Looping, Array, Pointer

evaluated from left to right using the rules of precedence of introducing parentheses into an expression. Consider the
operators. There are two distinct priority levels of arithmetic same expression with parentheses as shown below :
operators in C.
9 – 12/3 (3+3) * ( 2 -1 )
1. High Priority : * / %
2. Low priority : + - Whenever parentheses are used, the expressions within
parentheses assume highest priority. It two or more sets of
Ex:1: x = a – b /3 + c * 2 - 1 parentheses appear one after another as shown above, the
expression contained in the left most set is evaluated first
Statement becomes and the right-most in the last.

x = 9 – 12 / 3 + 3 * 2 -1 First pass
Step 1: 9-12/6 * (2-1)
Solution : Step 2: 9 -12/6 * 1
First Pass
Second pass
step 1 : x = 9 - 4 + 3 * 2 – 1
step 2 : x = 9 – 4 + 6 – 1 Step 3: 9 - 2 * 1
Step 4: 9 - 2
Second Pass Third pass

step 3 : x = 5 + 6 - 1 Step 5: 7
step 4 : x = 11 - 1
step 5 : x = 10

PPT CREATED BY : Prof. Rajesh K. Jha 26


C - Programming Language

List, Stack, Queue,Graph,..etc.


Function, Structure, Union, File Handling, Exception, Link
Rules for Evaluation of Expression : Make a C program for the following expression :
Basic, Control Structure, Looping, Array, Pointer

a=5<=8 && 6! =5
1. First parenthesized sub expression from left
to right are evaluated. #include <stdio.h>
int main()
2. If parentheses are nested, the evaluation {
begins with the innermost sub-expression. int a;
3. The precedence rules is applied in a=5<=8 && 6! =5
determining the order of application of printf (“%d”, a );
operators in evaluating sub-expressions. return 0;
}
4. The associativity rule is applied when two
or more operators of the same precedence
level appears in a sub-expression. Output : 1
5. Arithmetic expressions are evaluated from
left to right using the rules of precedence.
6. When parentheses are used, the
expressions within parentheses assume
highest priority.

PPT CREATED BY : Prof. Rajesh K. Jha 27


C - Programming Language
Type Conversion in Expressions

List, Stack, Queue,Graph,..etc.


Function, Structure, Union, File Handling, Exception, Link
Types of conversions in expression :
Basic, Control Structure, Looping, Array, Pointer

1. Implicit Type Conversion :


1. The automatic conversion is known as implicit conversion.
2. The lower data types is automatically converted into higher data type before the operation proceeds.
3. The result will come of higher data type.
Rules :
1. All short and char are automatically converted to int, then
2. If one of the operands is long double, the other will be converted to long double and the result will be long double.
3. else, if one of the operands is double, the other will be converted to double and the result will be double.
4. else, if one of the operands is float, the other will be converted to float and the result will be float.
5. else , if one of the operands is unsigned long int, the other will be converted to unsigned long int and the result will be unsigned
long int.
6. else, if one of the operands is long int and the other is unsigned int, then
a. If unsigned int can be converted to long int, the unsigned int operand will be converted as such and the result will be long int.
b. Else, both operands will be converted to unsigned long int and the result will be unsigned long int.
7. else, if of the operands is long int, the other will be converted to long int and the result will be long int.
8. else, if one of the operands is unsigned int, the other will be converted to unsigned int and the result will be
unsigned int.
PPT CREATED BY : Prof. Rajesh K. Jha 28
C - Programming Language
Type Conversion in Expression

List, Stack, Queue,Graph,..etc.


Function, Structure, Union, File Handling, Exception, Link
Conversions Hierarchy :
Basic, Control Structure, Looping, Array, Pointer

Note 1: float to int cause truncation of the fractional part.


2: double to float causes rounding of digits.
3: long int to int causes dropping of the excess higher order bits.

PPT CREATED BY : Prof. Rajesh K. Jha 29


C - Programming Language
Implicit Type Conversion program.

List, Stack, Queue,Graph,..etc.


Function, Structure, Union, File Handling, Exception, Link
Implicit Type Conversion program. Ex:2:
Basic, Control Structure, Looping, Array, Pointer

Ex:1: #include <stdio.h>


int main()
include<stdio.h> {
int main() int num = 20;
{ char c = 'k'; /* ASCII value is 107 */
short a=50; //initializing variable of short data type float sum;
int b; //declaring int variable sum = num + c;
printf("sum = %f\n", sum );
b=a; //implicit type casting }
printf("%d\n",a);
printf("%d\n",b);
} Ex:3:
#include<stdio.h>
int main()
{
char a = 'A’;
//int b = a; //Compiler will throw an error for this
int b = (int)a + 1;
printf("Value of a is %c\n", a);
printf("Value of b is %d\n",b);
return 0;
}
PPT CREATED BY : Prof. Rajesh K. Jha 30
C - Programming Language
Type Conversion in Expression

List, Stack, Queue,Graph,..etc.


Function, Structure, Union, File Handling, Exception, Link
2. Explicit Conversion :
Basic, Control Structure, Looping, Array, Pointer

Example Action

x = (int) 7.5 7.5 is converted to integer by truncation

a=(int) 21.3/(int) 4.5 Evaluated as 21/4 and the result would be 5

b=(double) sum /n Division is done in floating point mode

y=(int) (a+b) The result of a+b is converted to integer

z=(int) a+b a is converted to integer and then added to b

p=cos ((double)x) Converts x to double before using it.

PPT CREATED BY : Prof. Rajesh K. Jha 31


C - Programming Language
2. Explicit Conversion : 2. Explicit Conversion :

List, Stack, Queue,Graph,..etc.


Function, Structure, Union, File Handling, Exception, Link
Ex.1. Ex.3.
#include<stdio.h>
Basic, Control Structure, Looping, Array, Pointer

int main() #include<stdio.h>


{ int main()
float a = 5.2; {
int b = (int)a + 1; int a = 65;
printf("Value of a is %f\n", a); char b = (char)a ;
printf("Value of b is %d\n",b); printf("Value of a is %d\n", a);
return 0; printf("Value of b is %c\n",b);
} return 0;
}
Ex.2.
#include<stdio.h> Ex.4.
int main()
{
double a = 5.2;
//int b = a; //Compiler will throw an error for this
int b = (int)a + 1;
printf("Value of a is %ld\n", a);
printf("Value of b is %d\n",b);
return 0;
}

PPT CREATED BY : Prof. Rajesh K. Jha 32


Function, Structure, Union, File Handling, Exception, Link
List, Stack, Queue,Graph,..etc.
33
Mathematical Function in C
C - Programming Language

PPT CREATED BY : Prof. Rajesh K. Jha


Basic, Control Structure, Looping, Array, Pointer
Function, Structure, Union, File Handling, Exception, Link
List, Stack, Queue,Graph,..etc.
34
C - Programming Language

PPT CREATED BY : Prof. Rajesh K. Jha


Basic, Control Structure, Looping, Array, Pointer
C - Programming Language

List, Stack, Queue,Graph,..etc.


Function, Structure, Union, File Handling, Exception, Link
Note:
Basic, Control Structure, Looping, Array, Pointer

1. x and y should be declared as double.


2. In trigonometric and hyperbolic function x and y
are in radians.
3. All the function return a double.
4. C99 has added float and long double version of
these function.

PPT CREATED BY : Prof. Rajesh K. Jha 35


C - Programming Language

List, Stack, Queue,Graph,..etc.


Function, Structure, Union, File Handling, Exception, Link
Program:1: sqrt() Program:2: pow()
Basic, Control Structure, Looping, Array, Pointer

#include<stdio.h> #include<stdio.h>

#include<conio.h> #include<conio.h>

int main() int main()

{ {

float x,y; int x,y,z;

printf ("Enter value of x="); printf ("Enter value of x and y=");

scanf ("%f",&x); scanf ("%d%d",&x,&y);

y=sqrt(x); z=pow(x,y);

printf("\n Value of y=%f",y); printf("\n Value of z=%d",z);

} }

PPT CREATED BY : Prof. Rajesh K. Jha 36


C - Programming Language

List, Stack, Queue,Graph,..etc.


Function, Structure, Union, File Handling, Exception, Link
Program:1: sqrt()
Program:3: ceil()
Basic, Control Structure, Looping, Array, Pointer

#include<stdio.h>
#include<conio.h>
#include <stdio.h>
int main()
#include <math.h>
{
float x,y;
int main()
printf ("Enter value of x=");
{
scanf ("%f",&x);
double num = 2.66;
y=sqrt(x);
int z;
printf("\n Value of y=%f",y);
}
z = ceil(num);
printf("Ceiling integer of %.2f = %d", num, z);
Program:2: pow()
return 0;
#include<stdio.h>
}
#include<conio.h>
int main()
{
int x,y,z;
printf ("Enter value of x and y=");
scanf ("%d%d",&x,&y);
z=pow(x,y);
printf("\n Value of z=%d",z);
}

PPT CREATED BY : Prof. Rajesh K. Jha 37


C - Programming Language

List, Stack, Queue,Graph,..etc.


Function, Structure, Union, File Handling, Exception, Link
Note:
Basic, Control Structure, Looping, Array, Pointer

1. x and y should be declared as double.


2. In trigonometric and hyperbolic function x and y
are in radians.
3. All the function return a double.
4. C99 has added float and long double version of
these function.

PPT CREATED BY : Prof. Rajesh K. Jha 38


C - Programming Language

List, Stack, Queue,Graph,..etc.


Function, Structure, Union, File Handling, Exception, Link
What is #define preprocessor directive in C:
Basic, Control Structure, Looping, Array, Pointer

 In the C Programming Language, the #define directive allows the definition of macros within your source code.
These macro definitions allow constant values to be declared for use throughout your code.
 Macro definitions are not variables and cannot be changed by your program code like variables. You generally
use this syntax when creating constants that represent numbers, strings or expressions.
 #define directive line can not be terminate with ;

Syntax :
#define constant_name value_of_constant_name
#define num 10
#define name “OP Solution”

PPT CREATED BY : Prof. Rajesh K. Jha 39


C - Programming Language
Program example of #define preprocessor directive :

List, Stack, Queue,Graph,..etc.


Function, Structure, Union, File Handling, Exception, Link
1.
Basic, Control Structure, Looping, Array, Pointer

2.
#include <stdio.h>
#define company "OM Solution" #include <stdio.h>
#define year 2005
#define num1 10
int main() #define num2 20
{
printf("%s is company which is established on %d \n", company, int main()
year); {
return 0; int sum;
} sum =num1+num2;

printf(“Sum of two number=%d”, sum);


return 0;
}

PPT CREATED BY : Prof. Rajesh K. Jha 40

You might also like