Module-2 Notes
Module-2 Notes
getchar( ):
- It reads one character from the standard input.
- Syntax : variable_name = getchar( )
- Example: char c;
….
c=getchar();
- A dummy getchar( ) is used to eat unwanted characters given as input
putchar( ):
- It writes one character to the standard output (Monitor).
- Syntax : putchar(variable_name/ character constant)
-
Example: char c;
c=”A”;
putchar(c);
Formatted Input:
Scanf( )
Example: int i;
float f;
char c;
char str[10];
scanf(“%d %f %c %s”,&I,&f,&c,str);
- format string: must be a text enclosed in double quotes. It contains the information for
interpreting the entire data for connecting it into internal representation in memory.
Example: integer (%d) , float (%f) , character (%c) or string (%s).
- argument list: contains a list of variables each preceded by the address list and separated
by comma. The number of argument is not fixed;
- Inside the format string the number of argument should tally with the number of format
specifier.
- Example: if i is an integer and j is a floating point number, to input these two numbers
we may use
scanf (“%d%f”, &i, &j);
-
- To input integer numbers:
Format specification: %wd
% -> conversion character
w - > an integer number specifying the field width of the number to be read
Ex: scanf(“ %2d %3d”,&a,&b)
Input : 50 123
50 assigned to a and 123 assigned to b
Unassigned input value will be assigned to next scanf call
Input data must be separated by spaces
- To input characters:
Format specification: %wc or %ws
% -> conversion character
w - > an integer number specifying the field width of the number to be read
%[characters] -> only the characters specified within the brackets are permissible
in input data
%[^characters] -> the characters specified within the brackets are not permissible
in input data
Ex: scanf(“ %[a-z]c %[^0-9]c”,&a,&b)
a can take only one letter between a to z
b can take any character except digits 0-9
functions supported by C:
isalpha( )
checks whether given character is alphabetic or not.
Returns TRUE or FALSE accordingly
isspace()
checks whether character is space or not.
isupper()
checks whether character is an uppercase character or not.
islower()
checks whether character is a lowercase character or not.
ispunct()
checks whether character is a punctuation character or not.
Punctuation characters are , . : ; ` @ # $ % ^ & * ( ) < > [ ] \ / { } ! | ~ - _ + ? = ' "
isprint()
checks whether character is printable or not.
toupper()
returns character in upper case.
tolower()
returns character in lower case.
Simple if statement
- syntax:
if (test Example:
expression) false ..........
Test
{ if (x == 1)
exp ?
statement-block; {
} y = y +10;
statement-x; }
true
Statement Block
printf("%d", y);
.........
Statement - X
- If the test expression is true , then the true-block statement(s), immediately following the
if statement are executed;
- otherwise the false-block statement(s) are executed.
- In either case, either true-block or false-block will be executed, not both.
- Example:
........
if (c == 'm')
male = male +1;
else
fem = fem +1;
.......
Nesting of if…else statements
- When a series of decisions are involved, we may have to use more than one if.....else statement
in nested form as follows:
if (test condition1)
{
if (test condition 2)
{
statement-1;
}
else
{
statement-2;
}
}
Else
{
if (test condition 3)
{
statement-1;
}
else
{
statement-2;
}
}
statement-x;
-
- This construct is known as the else if ladder.
- The conditions are evaluated from the top (of the ladder), downwards.
- As soon as a true condition is found, the statement associated with is executed and the
control is transferred to the statement x (skipping the rest of the ladder).
- When all the n conditions become false, then the final else containing the default
statement will be executed.
The Switch statement: C has a built-in multi-way decision statement known as a switch. The
switch statement tests the value of a given variable (or expression) against a list of case values
and when a match is found, a block of statements associated with that case is executed. The
expression is an integer expression or characters.Value-1, value-2….are constants or constant
expressions and are known as case labels. Each of these values should be unique within a switch
statement. Block-1 , block-2…are statement lists and may contain zero or more statements.
There is no need to put braces around these blocks. Case labels end with a colon (: ).
When the switch is executed, the value of the expression is successively compared against the
values value-1, value-2,…If a case is found whose value matches with the value of the
expression, then the block of statements that follows the case are executed.
switch (expression)
{
case value-1;
block1
break;
……
……
default:
default-block
break;
}
statement-X;
The break statement at the end of each block signals is the end of a particular case and causes an
exit from the switch statement, transferring the control to the statement-x following the switch.
The default is an optional case. When present, it will be executed if the value of the expression
does not match with any of the case values. If not present, no action takes place if all natches fail
and the control goes to the statement X.
The conditional operator: The conditional operator is useful for making two-way decisions.
This operator is a combination of ? and : and takes three operands. The general form of use of the
conditional operator is as follows.
The conditional expression is evaluated first. If the result is nonzero, expression1 is evaluated
and is returned as the value of the conditional expression. Otherwise, expression2 is evaluated
and its value is returned. For example
the segment
if ( x < 0)
flag = 0;
else
flag =1;
can be written as flag = ( x < 0 )? 0 :1;
Example2.
Y = 1.5x + 3 for x 2
Y = 2x +5 for x >2
Y = ( x >2 ) ? (2*x +5): (1.5*x+3);
The conditional operator may be nested for evaluating more complex assignment decisions.
4x +100 for x < 40
Example: salary = 300 for x =40
4.5x + 150 for x > 40
Salary = ( x!=40)? ((x<40)? (4*x+100): (4.5*x+150):300;
The same can be evaluated using if … else statement as follows
if (x < = 40)
if ( x< 40)
salary = 4*x+100;
else
salary = 300;
else
salary = 4.5*x + 150;
List of simple programs: