·
exit() Function
·
Null Statement
·
sizeof( ) operator
·
Order of Precedence
·
Recursive functions
exit() Function
exit() Function
There is also a C library function, exit(),
that can be used to cause a program to end.
exit() function is defined in a header file,
stdlib.h, hence include the header file at the beginning of your program.
#include <stdlib.h>
#include <stdio.h>
void main()
{
printf (“Hello");
exit(0);
}
null statement
A null statement consisting of only a semicolon
and performs no operations
Finding square root of n iff n >0 ?
main() {
int n,p;
printf(“Enter a Positive Number”);
scanf(“%d”,&n);
If(n<0)
; // null statement – performs nothing if
negative number
else
p
= sqrt(n);
printf(“ Square Root is %d”,p);
}
Example
Suppose i is an integer variable and c is a
character type variable then
printf(“Size
of integer : %d”,sizeof(i));
printf(“Size
of character : %c”,sizeof(c));
Generate the following output :
Size
of integer : 2
Size of character : 1
Size of character : 1
Order of Precedence
meaning of expressions as a + b * c ???
(a + b) * c
or
a + (b * c)
All
operators have a priority, and high priority operators are evaluated before
lower priority ones.
•
Operators
of the same priority are evaluated from left to right, so that
a - b - c
is
evaluated as
( a - b ) - c
From high priority to low
priority
( ) [ ] -> .
* /
%
+ -
<
<= >= >
==
!=
&
|
&&
||
?:
Conditional Operator
•
The
general syntax is thus
condition
? expression1
: expression2;
If the result of condition is
TRUE (non-zero), expression1 is evaluated and the result of the evaluation
becomes the result of the operation.
If the condition is FALSE (zero), then expression2
is evaluated and its result becomes the result of the operation.
s = (x<0) ? -1 : x*x;
If
x is less than zero, then s=-1.
If
x is greater than or equal to zero, then s=x*x.
Bitwise Operators
1.
one’s compliment operators
2.
logical bitwise operators
3.
shift operators
(
the operand must be an integer )
- one’s
compliment operators ( ~ )
bits
of the operand is reversed/ inverted
Ex: 05FF 0000 0101
1111 1111
1111 1010
0000 0000 -à
FA00.
- logical
bitwise operators
bitwise and ( & ) ,
bitwise exclusive or ( ^ ) ,
bitwise or ( | ).
Ex: a = 0000 0101
1111 1010
b =
1111 1010 0000
0010
a | b
= 1111 1111 1111
1010
- shift operators
each operator requires to
operands.
Ex: b
= a < <
6
a
= 0000
0101 1111 1010
a << 6 is
0111
1110 1000 0000
(
filled with 0’s )
Recursive Function
Recursive function is a function that calls itself.
long factorial(int); //function prototype declaration
void main()
{
int n; long f;
printf("Enter
a number\n");
scanf("%d",
&n);
if (n < 0)
printf("Negative not allowed.\n");
else
{
f = factorial(n);
printf("%d! = %ld\n", n, f);
}
}
long factorial(int n)
{
if (n == 0) return 1;
else
return(n * factorial(n-1));
}
No comments:
Post a Comment