Programming in C Unit II
Programming in C Unit II
Introduction to C Programming
Unit-II Notes
1. I/O Statements:-
➢ C language has standard libraries that allow input and output in a program.
➢ The stdio.h or standard input output library in C has methods for input and output.
➢ The printf() and scanf() functions are used for input and output in C language. Both functions are
inbuilt library functions, defined in stdio.h (header file).
a) scanf()
➢ The scanf() method, in C, reads the value from the console as per the type specified and store it in the
given address.
Syntax:
scanf("%X", &variableOfXType);
where %X is the format specifier in C. It is a way to tell the compiler what type of data is in a variable and & is
the address operator in C, which tells the compiler to change the real value of variableOfXType, stored at
this address in the memory.
b) printf()
➢ The printf() method, in C, prints the value passed as the parameter to it, on the console screen.
Syntax:
printf("%X", variableOfXType);
where %X is the format specifier in C. It is a way to tell the compiler what type of data is in a variable
and variableOfXType is the variable to be printed.
%i signed integer
%lf Double
%o Octal representation
%p Pointer
%s String
%u Unsigned int
%x or %X Hexadecimal representation
%n Prints nothing
%% Prints % character
Step 3: Click on Compile menu and then on Compile option, or press the keys and press Alt + F9 to compile
the code.
Step 4: Click on Run or press Ctrl + F9 to run the code. Yes, C programs are first compiled to generate the
object code and then that object code is Run.
Step 5: Alt+F5 to view the output of the program at the output screen.
Step 6: Save the program using F2 (OR file > Save), remember the extension should be “.c”.
4. Escape Sequence Characters:-
➢ Programming languages like C have escape sequences as a standard feature.
➢ They enable the inclusion of special characters and control patterns in strings and character constants
that are difficult to represent or write directly.
➢ An escape sequence in the C programming language consists of a backslash () and a character that
stands in for a special character or control sequence.
➢ There are several escape sequence in C programming languages as mentioned in below table:-
Escape Meaning
Sequence
\a Alarm or Beep
\b Backspace
\f Form Feed
\n New Line
\r Carriage Return
\t Tab (Horizontal)
\v Vertical Tab
\\ Backslash
\? Question Mark
hexadecimal
\xhh
number
\0 Null
i) Alarm or Beep (\a):
The alarm or beep escape sequence (a) produces an audible alert or beep sound.
Syntax of if Statement:-
if(condition)
{
// Statements to execute if
// condition is true
}
Flowchart of if Statement:-
Example:
int main()
{
int i = 10;
if (i > 15) {
printf("10 is greater than 15");
}
Syntax:-
if (condition)
{
// Executes this block if
// condition is true
}
else
{
// Executes this block if
// condition is false
}
Example:-
int main()
{
int i = 20;
if (i < 15) {
if (condition1)
{
// Executes when condition1 is true
if (condition_2)
{
// statement 1
}
else
{
// Statement 2
}
}
else {
if (condition_3)
{
// statement 3
}
else
{
// Statement 4
}
}
int main()
{
int i = 10;
if (i == 10) {
// First if statement
if (i < 15)
printf("i is smaller than 15\n");
// Nested - if statement
// Will only be executed if statement above
// is true
if (i < 12)
printf("i is smaller than 12 too\n");
else
printf("i is greater than 15");
}
else {
if (i == 20) {
// Nested - if statement
// Will only be executed if statement above
// is true
if (i < 22)
printf("i is smaller than 22 too\n");
else
printf("i is greater than 25");
}
}
return 0;
}
if (condition)
statement;
else if (condition)
statement;
.
.
else
statement;
Example:-
int main()
{
int i = 20;
if (i == 10)
printf("i is 10");
else if (i == 15)
printf("i is 15");
else if (i == 20)
printf("i is 20");
else
printf("i is not present");
}
5. switch Statement in C:-
➢ The switch case statement is an alternative to the if else if ladder that can be used to
execute the conditional code based on the value of the variable specified in the switch
statement.
➢ The switch block consists of cases to be executed based on the value of the switch
variable.
Syntax of switch:-
switch (expression) {
case value1:
statements;
case value2:
statements;
....
....
....
default:
statements;
}
Flowchart of switch:-
Flowchart of switch in C
Example of switch Statement:-
int main()
{
// variable to be used in switch statement
int var = 2;
return 0;
}
// driver code
int main()
{
int var;
int flag = 0;
return 0;
}
A) break:-
➢ This loop control statement is used to terminate the loop.
➢ As soon as the break statement is encountered from within a loop, the loop iterations
stop there, and control returns from the loop immediately to the first statement after the
loop.
Syntax of break:-
break;
➢ Basically, break statements are used in situations when we are not sure about the actual number of
iterations for the loop or we want to terminate the loop based on some condition.
Flowchart:-
Example of break:-
// C program to illustrate
// to show usage of break
// statement
#include <stdio.h>
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6 };
// no of elements
int n = 6;
// key to be searched
int key = 3;
return 0;
}
B) continue :-
➢ This loop control statement is just like the break statement.
➢ The continue statement is opposite to that of the break statement, instead of terminating the loop, it
forces to execute the next iteration of the loop.
➢ As the name suggests the continue statement forces the loop to continue or execute the next
iteration.
➢ When the continue statement is executed in the loop, the code inside the loop following the
continue statement will be skipped and the next iteration of the loop will begin.
Syntax of continue:-
continue;
Flowchart of Continue:
Example:-
int main()
{
// loop from 1 to 10
for (int i = 1; i <= 10; i++) {
// If i is equals to 6,
// continue to next iteration
// without printing
if (i == 6)
continue;
else
// otherwise print the value of i
printf("%d ", i);
}
return 0;
}
C) goto:-
➢ The goto statement in C also referred to as the unconditional jump statement can be used to jump
from one point to another within a function.
Syntax of goto:-
Syntax1 | Syntax2
----------------------------
goto label; | label:
. | .
. | .
. | .
label: | goto label;
➢ In the above syntax, the first line tells the compiler to go to or jump to the statement marked as a
label.
➢ Here, a label is a user-defined identifier that indicates the target statement.
➢ The statement immediately followed after ‘label:’ is the destination statement.
➢ The ‘label:’ can also appear before the ‘goto label;’ statement in the above syntax.
Flowchart:-
Example:-
D) return:-
➢ The return in C returns the flow of the execution to the function from where it is called.
➢ This statement does not mandatorily need any conditional statements.
➢ As soon as the statement is executed, the flow of the program stops immediately and
returns the control from where it was called.
➢ The return statement may or may not return anything for a void function, but for a non-
void function, a return value must be returned.
Flowchart of return:-
Syntax of return:-
return [expression];
Example:-
// C code to illustrate return
// statement
#include <stdio.h>
// returns void
// function to print
void Print(int s2)
{
printf("The sum is %d", s2);
return;
}
int main()
{
int num1 = 10;
int num2 = 10;
int sum_of = SUM(num1, num2);
Print(sum_of);
return 0;
}
for loop first Initializes, then condition check, then executes the body and at last, the update is done.
first Initializes, then condition checks, and then executes the body, and updating can be
while loop
inside the body.
do-while
do-while first executes the body and then the condition check is done.
loop
a) for Loop:-
➢ for loop in C programming is a repetition control structure that allows programmers to write a
loop that will be executed a specific number of times.
➢ for loop enables programmers to perform n number of steps together in a single line.
Syntax:
Example:
Example:-
// C program to illustrate for loop
#include <stdio.h>
// Driver code
int main()
{
int i = 0;
b) While Loop:-
➢ While loop does not depend upon the number of iterations.
➢ In for loop the number of iterations was previously known to us but in the While loop, the
execution is terminated on the basis of the test condition.If the test condition will become false
then it will break from the while loop else body will be executed.
Syntax:
initialization_expression;
while (test_expression)
{
// body of the while loop
update_expression;
}
// Driver code
int main()
{
// Initialization expression
int i = 2;
// Test expression
while(i < 10)
{
// loop body
printf( "Hello World\n");
// update expression
i++;
}
return 0;
}
c) do-while Loop:-
The do-while loop is similar to a while loop but the only difference lies in the do-while loop test condition
which is tested at the end of the body.
In the do-while loop, the loop body will execute at least once irrespective of the test condition.
Syntax:
initialization_expression;
do
{
// body of do-while loop
update_expression;
} while (test_expression);
Flowchart:-
Example:-
#include <stdio.h>
int main()
{
int m = 5, n = 4;
return 0;
}
Example 2: C Program to check whether a year is a leap year using ternary operator.
Code:-
#include <stdio.h>
int main()
{
int yr = 1900;