ALL SIR NOTES HAVE BEEN UPLOADED HERE.
Input means to provide the program with some data to be used in the program and output means to display data on the screen or write the data to a printer or a file.
Reading, processing, and writing of data are the three essential functions of a computer program. There are two methods of providing data to the program variable. One method is to assign values to variables through the assignment statements, and another method is to use the input function which can read data from a keyboard. C programming language provides standard library functions to read any given input and display data on the console.
Reading a single character can be done by using the getchar function. This can also be done with the help of the scanf function.
Var_name = getchar();
Like getchar, putchar is used for writing characters one at a time to the terminal. It takes the form as shown below:
putchar(Var_name);
Formatted input refers to an input data that has been arranged in a particular format. This is possible in C using the scanf function. The general form of scanf is:
int scanf ( “control string” , arg1, arg2, .... argn );
Control string (also known as format string) contains field specifications, which direct the interpretation of input data. It may include conversion character %, a format specifier, field width, blanks, tabs, or backslash character.
For outputting results we have used extensively the function printf which sends results out to a terminal. The printf function provides certain features that can be effectively exploited to control the alignment and spacing of print-outs on the terminals. The general form of printf statement is:
int printf ( “control string” , arg1, arg2, ...., argn );
Control string consists of following there types of items:
Characters that will be printed on the screen as they appear.
Format specifications that define the output format for display of each item.
Escape sequence characters such as \n, \t, \b etc.
Control Statements enable us to specify the flow of program control; i.e., the order in which the instructions in a program must be executed. Using control statements we can control the flow of program in such a way so that it executes certain statements based on the outcome of a condition (i.e. true or false). In C, decision control statements are:
if statement
switch statement
conditional operator statement
goto statement
These statements are popularly known as decision-making statements. Since these statements ‘control’ the flow of execution, they are also known as control statements.
The if statement in C is used to perform the operations based on some specific condition. The operations specified in if block are executed if and only if the given condition is true.
if ( expression )
{
//code to be executed
}
It allows the computer to evaluate the expression first and then, depending on whether the value of the expression is true or false.
The if statement may be implemented in different forms depending on the complexity of conditions to be tested. The different forms are:
Simple if statement
if .... else statement
Nested if .... else statement
else if ladder
The syntax of a simple if statement is
if ( expression )
{
Statements;
}
Stmt;
The ‘Statements’ may be a single statement or a group of statements. If the test expression is true, the statements will be executed, otherwise execution will jump to the ‘Stmt’.
The if...else statement is an extension of the simple if statement which, we can perform two different operations, i.e., one is for the true of that condition, and the other is for the false of the condition. The syntax of the if-else statement is:
if ( expression )
{
//code to be executed if condition is true
}
else
{
//code to be executed if condition is false
}
#include<stdio.h>
int main()
{
int number=0;
printf("enter a number:");
scanf("%d",&number);
if(number%2 == 0)
{
printf("%d is even number", number);
}
else
{
printf("%d is odd number", number);
}
return 0;
}
Output:
enter a number:4
4 is even number
enter a number:5
5 is odd number
When a series of decisions are involved, we may have to use more than one if...else statement in nested form as shown below:
if ( expression 1 )
{
if ( expression 2 )
{
Statement 1;
}
else
{
Statement 2;
}
}
else
{
Statement 3;
}
Stmt;
If the expression 1 is false, the statement 3 will be executed, otherwise it continues to perform the second test. If the expression 2 is true, the statement 1 will be evaluated, otherwise the statement 2 will be evaluated.
#include <stdio.h>
int main()
{
int year;
printf("Enter a year: ");
scanf("%d",&year);
if(year % 100 == 0)
{
if(year % 400 == 0)
printf("\nLeap Year");
else
printf("\nNot a leap year");
}
else
{
if( year % 4 == 0)
printf("\nLeap year");
else
printf("\nNot a leap year");
}
return 0;
}
Output:
Enter a year: 2020
Leap year
There is another way of putting ifs together when multipath decision are involved. A multipath decision is a chain of ifs in which the statement associated with each else is an if. It takes the following general form:
if ( expression-1 )
statement-1;
else if (expression-2 )
statement-2;
else if ( expression-3 )
statement-3;
else if ( expression-4 )
statement-4;
else
statement-5;
statement-n;
This construct is known as the else if ladder. The conditions are evaluated from the top, downwards. As soon as a true condition is found, the statement associated with it is executed and the control is transferred to the statement-n. When all the n condition become false, then the final else containing statement-5 will be executed.
C has a built-in multiway 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 general form of the switch statement is as shown below:
switch ( expression ) {
case value1:
//code to be executed;
break; //optional
case value2:
//code to be executed;
break; //optional
.......
default: //optional
default-stmts;
}
The switch expression must be of an integer or character type.
The case value must be an integer or character constant and are known as case labels which ends with a colon (:).
Duplicate case value are not allowed.
The case value can be used only inside the switch statement.
The break statement at the end of each block signals the end of a particular case and causes an exit from the switch statement. It is optional.
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.
You can have switch statements inside another switch statement (Nested switch).
The main reason for using a switch include improving clarity, by reducing otherwise repetitive coding, and also offering the potential for faster execution through easier compiler optimization in many cases.
It is good for equality comparisons but not for range comparisons.
It works well for int type data only and not good for other data types.
It works well with constants but not with variables (as case labels).
void main()
{
char operator;
float n1, n2;
printf("Enter an operator (+, -, *, /) : ");
scanf("%c", &operator);
printf("Enter two operands: ");
scanf("%f %f",&n1, &n2);
switch(operator)
{
case '+': printf("%.2f + %.2f = %.2f", n1, n2, n1+n2);
break;
case '-': printf("%.2f - %.2f = %.2f", n1, n2, n1-n2);
break;
case '*': printf("%.2f * %.2f = %.2f",n1, n2, n1*n2);
break;
case '/': printf("%.2f / %.2f = %.2f",n1, n2, n1/n2);
break;
default: printf("Sorry!! Operator is not matched");
}
}
The C language has an unusual operator, useful for making two-way decisions. This operator is a combination of ? and :, and takes three operands. This operator is known as the conditional operator. The syntax of conditional operator is:
exp1 ? exp2 : exp3 ;
The exp1 is evaluated first. If the result is true, exp2 is evaluated otherwise exp3 is evaluated and its value is returned. For example, the segment
if ( a > b )
max = a;
else
max = b;
can be written as
max = ( a > b ) ? a : b ;