Professional Documents
Culture Documents
03 Basic of C
03 Basic of C
03 Basic of C
A Variable B
100 Constant ‘i’
TYPES OF CONSTANTS
CONSTANTS
PRIMARY SECONDARY
CONSTANTS CONSTANTS
}
FUNCTION
function name
void main ( )
{
}
FUNCTION
Return type
void main ( )
{
}
FUNCTION
}
FUNCTION
void main ( )
{
statement(s);
}
FUNCTION
data types
printf() function
More Examples
• printf(“simple interest = %.2f”, p*r*n/100);
// expression
– output : simple interest = 3000.00
• Use of ESCAPE SEQUENCE is allowed in
format string.
in C++, we use cout object instead of printf()
function.
data types
scanf() function
• used to receive input from the user.
• prototype available in stdio.h header file.
• similar to cin object in C++.
• general form
– scanf (“format string”, list of addresses of
variables);
– e.g. scanf( “%d %f %c”, &i, &pi, &ch);
• ‘&’ is address of operator.
data types
scanf() function
• When you enter the data via keyboard,
they must be separated by either blank(s),
tab(s) or newline(s) characters.
#include
add (a<stdio.h>
, b);
#include
getch(); <conio.h>
}void main()
{
void
int add(
a, b; int x, int y)
{ void add( int , int );
clrscr();
int z;
zprintf(“\nEnter
= x + y; 2 Nos.”);
scanf(“%d %d”,&a,&b);
printf(“Answer = %d”, z);
}
How memory looks like?
function main() a b
10 20
function add() x y z
10 20 30
Functions
No Arguments/With Return Value
float add(void)
#include <stdio.h>
{#include <conio.h>
void main()
float x, y, z;
{ printf(“\nEnter 2 Nos.”);
float c;
scanf(“%f %f”, &x, &y);
float
z = x add(void);
+ y;
clrscr();
return z;
} c = add();
printf(“Answer = %f”,c);
getch();
}
How memory looks like?
function main() c
30
function add() x y z
10 20 30
Functions
With Arguments & Return Value
#include
c = add <stdio.h>
(a, b);
#include
printf(“Answer
<conio.h> = %f”,c);
voidgetch();
main()
}{
float a, b, c;
float
float
add(addfloat
(float,
x, float
float);y)
{ clrscr();
printf(“\nEnter
return (x+y); 2 Nos.”);
} scanf(“%f %f”, &a , &b);
How memory looks like?
function main() a b c
10 20 30
function add() x y
10 20
WHEN TO USE WHICH
FUNCTION
INPUT PROCESS OUTPUT
VOID FN FN FN
FN(VOID)
VOID MAIN FN FN
FN(INT)
INT FN FN MAIN
FN(VOID)
Action 2
Action 3
exit
SELECTION STRUCTURE
entry
true false
condition
Action 1 Action 2
exit
Action 3
SELECTION STRUCTURE
• if-else
• switch
if-else (various forms)
1) if (condition) e.g. if ( a > 5)
statement; printf(“%d”, a);
• ||
– if the first expression evaluates to true, the following
expression is not evaluated.
• &&
– if the first expression evaluates to false, the following
expression is not evaluated.
Examples of Short Circuit
int a, b, c;
a = 4; b = 10; c = 30;
if ( ( a < b ) || ( a < c ) )
/* (4<10)||(4<30) */
...
if ( ( a > b ) && ( a > c ) )
/* (4>10)&&(4>30) */
The Conditional Operators
• Turnery Operator: ( ) ? :
• ((Expression1) ? Expression2 : expression3);
• y = (( x > 5) ? 3 : 4);
means if x > 5, y = 3 otherwise y = 4
• int big, a, b, c;
• big = ((a>b) ? ((a>c) ? a : c) : ((b>c) ? b : c) );
Increment/Decrement Operators
• Unary
• ++ increment
• -- decrement
• Each has two flavours, pre and post
• Applies to integer and char data-types only
Pre Increment
• Value of variable is incremented and then
used in expression.
• Syntax
++variableName
Example
int a, c;
a = 10;
/* a is 11 */
/* c is 11 */
Post Increment
• Value of variable is used in expression and
later incremented.
• Syntax
variableName++
Example
int a, c;
a = 10;
/* a is 11 */
/* c is 10 */
Decrement operator
• Pre Decrement
– Similar to Pre Increment
– Syntax : --variableName
• Post Decrement
– Similar to Post Increment
– Syntax : variableName--
Operator Precedence
• Multiple operators in an expression
• Precedence decides the order of application of
operators
• Specified by a precedence table
• Operators at the top have higher precedence
• Operators in the same row have same
precedence
• () can be used to override precedence rules
Precedence of operators
() [ ] -> . (Highest precedence)
- ++ -- ~ ! & * (type) sizeof
* / %
+ -
<< >>
< <= > >=
== !=
&
^
|
&&
||
?:
= += -= *= /= (Lowest precedence)
Exercise-Write C Program for the
following.
1. Find out largest and smallest of two values.
2. Find out largest and smallest of three values.
3. Find out net salary where net salary = gross
salary + allowances – deductions.
If gross salary > 10000, allowances are 10%,
deductions are 3% of gross salary.
If gross salary > 5000, allowances are 7%,
deduction are 2% of gross salary.
Exercise-Write C Program for the
following.
4. Calculate total, average of marks of three
subjects. Give following grades to the
student.
If average >= 70, distinction, if average >=
60, first class,
if average >= 50, second, if average >= 35,
third class,
Otherwise fail. If student secures < 35 marks
in any subject then declare student fail.
Exercise-Write C Program for the
following.
5. Find out whether a given no. is divisible by 7
or not.
6. Find out net sales where net sales = gross
sales – discount.
If gross sales > 20000, discount is 15%
If gross sales > 10000, discount is 10%
otherwise 5%.
SWITCH-CASE-DEFAULT
• Control statement
• Allows us to make a decision from the
number of choices.
• switch( integer expression)
{
case constant1 : { statements1 }
break;
case constant2 : { statements2 }
default : { statement3 }
}
SWITCH-CASE-DEFAULT
expression
constant 2 statements 2
missing break
control falls through
statements 3
default
SWITCH-CASE-DEFAULT
int i = 25;
switch (i)
{
case 121: printf(“121\n”);
break;
case 7: printf(“7\n”); break;
default: printf(“ I am in default\n”);
};
SWITCH-CASE-DEFAULT
if i ==
7 printf(7);
default printf(“default”);
SWITCH-CASE-DEFAULT
char c = ‘x’;
switch (c)
{
case ‘v’ : printf(“ I am in case v.\n”);
case ‘a’ : printf(“ I am in case a.\n”);
case ‘x’ : printf(“ I am in case x.\n”);
default : printf(“ I am in default.\n”);
}
SWITCH-CASE-DEFAULT
The output will be
I am in case x.
I am in default.
why?
we have not written break statement.
SWITCH-CASE-DEFAULT
char c = ‘a’;
switch (c)
{
case ‘a’ :
case ‘A’ : printf( “I am in case a.”);
break;
case ‘1’ : printf( “ I am in case 1.”);
break;
};
LOOP STRUCTURE
entry
Let us
Understand
loop
do...while
true
condition Action 1
while
false
Action 2 for
exit
Steps for Writing Algorithm
A: Statements to be
A executed before loop begins
B B: Starting Value
E
C: Ending Value
F D: Difference
E: Control Variable (C.V.)
= Initial Value (I.V.)
D C
F: Loop Statements
G G: Statements to be
executed after loop ends.
Flow of do…while loop
initialization
Add Difference
yes no
condition
DO…WHILE LOOP STRUCTURE
do
A
{
E B
D
F
F
D C
G
} while ( C );
G
Examples of do…while loop
Print 1st 10 natural numbers. Print 1st N natural numbers.
INPUT N
I=0 1 I=0 1
PRINT I PRINT I
+1 10 +1 N
printf(“Enter a Value.”);
i = 0; scanf(“%d”,&n); i = 0;
do do
{ {
i ++; ++i;
printf(“%d\n”,i); printf(“%d\n”,i);
} while ( i < 10 ); } while ( i < n );
Flow of while loop
initialization
no
condition
loop back
yes
Add Difference
Loop statement(s)
WHILE LOOP STRUCTURE
A
A
E B
E
F
while ( C )
D C
G
{
D
F
};
G
Examples of while loop
Print 1st 10 natural numbers. Print 1st N natural numbers.
INPUT N
I=0 1 I=0 1
PRINT I PRINT I
+1 10 +1 N
printf(“Enter a Value.”);
i = 0; scanf(“%d”,&n); i = 0;
while ( i < 10 ) while ( i < n )
{ {
i = i + 1; i += 1;
printf(“%d\n”,i); printf(“%d\n”,i);
}; };
Flow of for loop
initialization
loop back no
condition
yes
Add Difference
Loop statement(s)
FOR LOOP STRUCTURE
Steps for writing Algorithm
A
A: Statements to be
E B executed before loop begins
B: Starting Value
F C: Ending Value
D: Difference
D C E: Control Variable (C.V.)
G = Initial Value (I.V.)
F: Loop Statements
G: Statements to be
executed after loop ends.
for ( C.V.= B ;C.V. <= C ; C.V. + D )
{
F
};
Examples of for loop
Print 1st 10 natural numbers. Print 1st N natural numbers.
INPUT N
I=0 1 I=0 1
PRINT I PRINT I
+1 10 +1 N
printf(“Enter a Value.”);
scanf(“%d”,&n);
for ( i = 1 ; i <= 10 ; i = i + for ( i = 1 ; i <= n ; i = i + 1 )
1) {
{ printf(“%d\n”,i);
printf(“%d\n”,i); };
};
Comparison of
while and do…while
• While and for are top-tested loop.
– Condition is checked before loop body is being
executed.
– If condition is true, then only loop gets executed.
– It may happen that loop may not be executed at all.
• do…while is bottom-tested loop.
– Condition is checked after loop body is executed.
– Even if condition is false, it will execute the loop
body once.
Keywords continue and break
• Find out the output of the following segment of
the program.
for(i=1;i<=5;i++) for(i=1;i<=5;i++)
for(j=1;j<=5;j++) for(j=1;j<=5;j++)
{ {
if ( i == j) if ( i == j)
continue; break;
printf(“%d %d\n”,i,j); printf(“%d %d\n”,i,j);
} }