SlideShare a Scribd company logo
UNIT-II
In programming input means reading data from the input devices
or from a file. Output means displaying results on the screen. C provides
number of input and output functions. These input and output functions
are predefined in their respective header files. Header file for standard
input and output functions is stdio.h. These are included into program
prefixed with #include statement. Input and Output functions are
classified into 2 categories. Decision making statements (or) Control
statements are used to check the condition, if the condition is true it will
executes a block of statements.
C Formatted IO Functions
Input data or output results are formatted as per requirement.
Formatted function improves the readability of the input and
output. Formatted functions can be used to read and write data
of all data type(char, int, float, double). Formatted input and
output functions require format specifiers(%c, %d, %f, %lf) to
identify the type of data.
scanf() Function
Scanf function reads all types of data value from input devices
(or) from a file. The address operator '&' is to indicate the
memory location of the variable. This memory location is used to
store the data which is read through keyboard.
scanf() Function Program
scanf.c
#include <stdio.h> //header file section
int main()
{
int a, b, c;
printf("Enter the value of a = ");
scanf("%d",&a) ; //read value a through keyboard
printf("nEnter the value of b = ");
scanf("%d",&b); //read value b through keyboard
c = a + b;
printf("na + b = %d ",c); //Print the sum of two value a and b
return 0;
}
 Enter the value of a = 6
 Enter the value of b = 4
 a + b = 10
Width specifier program
widthspecifier.c
#include <stdio.h> //header file section
int main()
{
printf("n%.2s","abcdefg ");
printf("n%.3s","abcdefg ");
printf("n%.4s","abcdefg ");
printf("n%.5s","abcdefg ");
printf("n%.6s","abcdefg ");
return 0;
}
 ab
 abc
 abcd
 abcde
 abcdef
Note:
Output itself shows the process done by %.2s, %.3s, etc..
scanf to Get Input
getinput.c
#include <stdio.h> //header file section
int main()
{
float a, b, c, d;
printf("Enter three float numbers:n ");
scanf("n %f %f %f ",&a,&b,&c);
d = (a + b + c)/3;
printf("nAverage of given number is %f ",d);
return 0;
}
 Enter three float numbers:
 4.5
 4.5
 5.0
 Average of given number is 4.666667
Note:
Here, three float variables a, b and c read through scanf() function. The
resultant average value is stored in a variable d.
C Unformatted Functions
Unformatted input and output functions are only work with
character data type. Unformatted input and output functions do
not require any format specifiers. Because they only work with
character data type.
Character IO Functions
getchar() Function
The getchar() function reads character type data form the input.
The getchar() function reads one character at a time till the user
presses the enter key.
getchar() C Program
getchar.c
#include <stdio.h> //header file section
#include <conio.h>
int main()
{
char c;
printf("Enter a character : ");
c = getchar();
printf("nEntered character : %c ", c);
return 0;
}
 Enter a character : y
 Entered character : y
Note:
Here, getchar() reads the input from the user and display back to the
user.
getch() Function
The getch() function reads the alphanumeric character input
from the user. But, that the entered character will not be
displayed.
getch() C Program
getch.c
#include <stdio.h> //header file section
#include <conio.h>
int main()
{
printf("nHello, press any alphanumeric character to exit ");
getch();
return 0;
}
 Hello, press any alphanumeric character to exit
Note:
The above program will run until you press one of many alphanumeric
characters. The key pressed by you will not be displayed.
getche() Function
getche() function reads the alphanumeric character from the
user input. Here, character you entered will be echoed to the
user until he/she presses any key.
getche() C Program
getche.c
#include <stdio.h> //header file section
#include <conio.h>
int main()
{
printf("nHello, press any alphanumeric character or symbol to exit n ");
getche();
return 0;
}
 Hello, press any alphanumeric character or symbol to exit
 k
Note:
The above program will run until you press one of many alphanumeric
characters. The key pressed by you will be echoed.
putchar() Function
putchar() function prints only one character at a time.
putchar() C Program
putchar.c
#include <stdio.h> //header file section
#include <conio.h>
int main()
{
char c = 'K';
putchar(c);
return 0;
}
 K
Note:
Here, variable c is assigned to a character 'K'. The variable c is displayed
by the putchar(). Use Single quotation mark ' ' for a character.
putch() Function
The putch() function prints any alphanumeric character.
putch() C Program
putch.c
#include <stdio.h> //header file section
#include <conio.h>
int main()
{
char c;
printf("Press any key to continuen ");
c = getch();
printf("input : ");
putch(c);
return 0;
}
 Press any key to continue
 input : d
Note:
The getch() function will not echo a character. The putch() function
displays the input you pressed.
String IO Functions
gets() Function
The gets() function can read a full string even blank spaces
presents in a string. But, the scanf() function leave a string after
blank space space is detected. The gets() function is used to get
any string from the user.
gets() C Program
gets.c
#include <stdio.h> //header file section
#include <conio.h>
int main()
{
char c[25];
printf("Enter a string : ");
gets(c);
printf("n%s is awesome ",c);
return 0;
}
 Enter a string : Randy Orton
 Randy Orton is awesome
Note:
The gets() function reads a string from through keyboard and stores it in
character array c[25]. The printf() function displays a string on the
console.
puts() Function
The puts() function prints the charater array or string on the
console. The puts() function is similar to printf() function, but we
cannot print other than characters using puts() function.
puts() C Program
puts.c
#include <stdio.h> //header file section
#include <conio.h>
int main()
{
char c[25];
printf("Enter your Name : ");
gets(c);
puts(c);
return 0;
}
 Enter your Name: john
 john
clrscr() in C
clrscr() is an inbuilt library function which is used to clear the
previous output displayed in a screen. clrscr() is defined
in #include <conio.h> header file.
clrscr() Function Program
clrscr.c
#include <stdio.h> //header file section
#include <conio.h>
int main()
{
printf("Before clrscr");
clrscr();
printf("clrscr() will clear the screen");
return 0;
}
 clrscr() will clear the screen
Note:
clrscr(); works in Turbo C/C++ compiler.
exit() in C
exit() is an inbuilt library function which is used to terimate the
program irrespective of the statements followed by it. exit() is
defined in #include <stdlib.h> header file.
exit() Function Program
exit.c
#include <stdio.h> //header file section
#include <stdlib.h>
int main()
{
printf("This statement is before exit(); function ");
exit(0);
printf("It will not display ");
return 0;
}
 This statement is before exit(); function
Note:
exit (some numeric value); will exit the program.
sleep() in C
sleep() is an inbuilt library function which is used to delay the
program's output. sleep() is defined in #include <unistd.h>
header file.
sleep() Function Program
sleep.c
#include <stdio.h>
#include <unistd.h>
int main()
{
printf("Countdown... ");
printf("n 3");
sleep(1);
printf("n 2");
sleep(1);
printf("n 1");
sleep(1);
printf("n Celebration Time ");
return 0;
}
 Countdown...
 3
 2
 1
 Celebration Time
Note:
sleep (seconds); will delay the program's output with respect to seconds
which you mentioned.
C Control Statements
C Programs that we encountered up to now were executed in
the same order which they appeared in it. In practical
applications, there is numerous situations where we have to
neglect some parts of program codes. For this purpose, C
Programming uses the control statements to control the flow of a
program. If the condition is satisfied the code followed by that
condition will execute. If not simply that the code will be
neglected by the compiler.
Types of Control statements
 if
 if else
 if-else-if control statement
 switch() case control statement
C if statement
Why if Statement?
All programming languages enable you to make decisions. They
enable the program to follow a certain course of action
depending on whether a particular condition is met. This is
what gives programming language their intelligence.
C if Syntax And Definition
 C uses the keyword if to execute a set of statements when logical
condition is true.
 When the logical condition is false, the compiler simply skips the
statement within the block.
 The "if" statement is also known as one way decision statement.
Syntax if statement
Syntax
if(condition)
{
here goes statements;
.
.
.
.
here goes statements;
}
if Statement Uses
if statements are commonly used in following scenarios
 Is A bigger than B?
 Is X equal to Y?
 Is M not equal to N?
Realtime Time if Statement Uses
Arduino microcontroller make use C programming, where you
need to blink a warning light(red light) when certain condition
met. Example program is as below:
realtimeif
if(x = 123)
digitalWrite(LEDpin, high)
Note:
x is a variable which get its input from a sensor continuously.
if Statement Rules
 The expression (or) condition is always enclosed within pair of
parenthesis. e.g.) if ( a > b )
 The if statement should not be terminated with a semicolon. If it happens,
the block of statements will not execute even the condition is true.
 The statements following the if condition is normally enclosed between 2
braces (in curly braces).
if statement Flow Chart
Following flow chart will clearly explain how if statement works
if statement C Program
ifstatement.c
#include <stdio.h> //header file section
#include <conio.h>
int main()
{
int age = 18;
if(age > 17){
printf("you are eligible for voting ");
}
printf("nThis is normal flow ");
return 0;
}
 you are eligible for voting
 This is normal flow
Note:
If the user input is greater than 17, then the condition will be true and the
statements under if condition gets executed. Otherwise, it executes next
to the if block.

More Related Content

PPT
Input And Output
Ghaffar Khan
 
PDF
C programming Workshop
neosphere
 
PPTX
Introduction to Basic C programming 02
Wingston
 
PPSX
Concepts of C [Module 2]
Abhishek Sinha
 
PPTX
What is c
Nitesh Saitwal
 
PPT
Mesics lecture 5 input – output in ‘c’
eShikshak
 
PPTX
Managing input and output operations in c
niyamathShariff
 
PPSX
Programming in C [Module One]
Abhishek Sinha
 
Input And Output
Ghaffar Khan
 
C programming Workshop
neosphere
 
Introduction to Basic C programming 02
Wingston
 
Concepts of C [Module 2]
Abhishek Sinha
 
What is c
Nitesh Saitwal
 
Mesics lecture 5 input – output in ‘c’
eShikshak
 
Managing input and output operations in c
niyamathShariff
 
Programming in C [Module One]
Abhishek Sinha
 

What's hot (20)

PDF
7 functions
MomenMostafa
 
PPT
CPU INPUT OUTPUT
Aditya Vaishampayan
 
PPTX
C programming(Part 1)
Dr. SURBHI SAROHA
 
PDF
1 introducing c language
MomenMostafa
 
DOCX
Important C program of Balagurusamy Book
Abir Hossain
 
PPT
Control Statements, Array, Pointer, Structures
indra Kishor
 
DOC
C lab-programs
Tony Kurishingal
 
PPTX
Input Output Management In C Programming
Kamal Acharya
 
PPTX
C Programming Language Part 6
Rumman Ansari
 
PDF
4 operators, expressions &amp; statements
MomenMostafa
 
PDF
Input and output in c
Rachana Joshi
 
PPTX
C Programming Language Part 7
Rumman Ansari
 
PDF
C Prog. - Decision & Loop Controls
vinay arora
 
PPTX
1 introduction to c program
NishmaNJ
 
PDF
Programming with c language practical manual
Anil Bishnoi
 
PPTX
C Programming Language Step by Step Part 2
Rumman Ansari
 
PDF
6 c control statements branching &amp; jumping
MomenMostafa
 
PDF
Deep C
Olve Maudal
 
PPTX
COM1407: Input/ Output Functions
Hemantha Kulathilake
 
7 functions
MomenMostafa
 
CPU INPUT OUTPUT
Aditya Vaishampayan
 
C programming(Part 1)
Dr. SURBHI SAROHA
 
1 introducing c language
MomenMostafa
 
Important C program of Balagurusamy Book
Abir Hossain
 
Control Statements, Array, Pointer, Structures
indra Kishor
 
C lab-programs
Tony Kurishingal
 
Input Output Management In C Programming
Kamal Acharya
 
C Programming Language Part 6
Rumman Ansari
 
4 operators, expressions &amp; statements
MomenMostafa
 
Input and output in c
Rachana Joshi
 
C Programming Language Part 7
Rumman Ansari
 
C Prog. - Decision & Loop Controls
vinay arora
 
1 introduction to c program
NishmaNJ
 
Programming with c language practical manual
Anil Bishnoi
 
C Programming Language Step by Step Part 2
Rumman Ansari
 
6 c control statements branching &amp; jumping
MomenMostafa
 
Deep C
Olve Maudal
 
COM1407: Input/ Output Functions
Hemantha Kulathilake
 
Ad

Similar to UNIT-II CP DOC.docx (20)

PPTX
Input and Output In C Language
Adnan Khan
 
PPTX
20220823094225_PPT02-Formatted Input and Output.pptx
putrielisabeth3
 
PPTX
Introduction to C Unit 1
Dr. SURBHI SAROHA
 
PPTX
Managing input and output operation in c
yazad dumasia
 
PPTX
C basics
thirumalaikumar3
 
PPT
Lec 10
kapil078
 
PPTX
Basics of c Nisarg Patel
TechNGyan
 
PPT
Lecture#5 c lang new
Zeeshan Ahmad
 
PPTX
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
Dr. Chandrakant Divate
 
PPT
C_Language_PS&PC_Notes.ppt
ganeshkarthy
 
PPTX
C language (1).pptxC language (1C language (1).pptx).pptx
RutviBaraiya
 
PPT
Session 3
Shailendra Mathur
 
PDF
Module 1_Chapter 2_PPT (1)sasaddsdsds.pdf
anilcsbs
 
PPTX
Sample for Simple C Program - R.D.Sivakumar
Sivakumar R D .
 
PPTX
Chap 2 input output dti2143
alish sha
 
PPTX
computer programming Control Statements.pptx
eaglesniper008
 
PDF
CP Handout#2
trupti1976
 
PPTX
Data Input and Output
Sabik T S
 
PPTX
Programming in C
DrPrabakaranPerumal
 
PPTX
Input Output function in c programing language.pptx
amit0815q
 
Input and Output In C Language
Adnan Khan
 
20220823094225_PPT02-Formatted Input and Output.pptx
putrielisabeth3
 
Introduction to C Unit 1
Dr. SURBHI SAROHA
 
Managing input and output operation in c
yazad dumasia
 
Lec 10
kapil078
 
Basics of c Nisarg Patel
TechNGyan
 
Lecture#5 c lang new
Zeeshan Ahmad
 
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
Dr. Chandrakant Divate
 
C_Language_PS&PC_Notes.ppt
ganeshkarthy
 
C language (1).pptxC language (1C language (1).pptx).pptx
RutviBaraiya
 
Module 1_Chapter 2_PPT (1)sasaddsdsds.pdf
anilcsbs
 
Sample for Simple C Program - R.D.Sivakumar
Sivakumar R D .
 
Chap 2 input output dti2143
alish sha
 
computer programming Control Statements.pptx
eaglesniper008
 
CP Handout#2
trupti1976
 
Data Input and Output
Sabik T S
 
Programming in C
DrPrabakaranPerumal
 
Input Output function in c programing language.pptx
amit0815q
 
Ad

More from JavvajiVenkat (6)

PPTX
CONTROL STMTS.pptx
JavvajiVenkat
 
DOCX
itretion.docx
JavvajiVenkat
 
PPTX
unit2 C-ProgrammingChapter 2 Control statements.pptx
JavvajiVenkat
 
DOCX
C Control Statements.docx
JavvajiVenkat
 
DOCX
loops and iteration.docx
JavvajiVenkat
 
DOCX
Nested Loops in C unit2.docx
JavvajiVenkat
 
CONTROL STMTS.pptx
JavvajiVenkat
 
itretion.docx
JavvajiVenkat
 
unit2 C-ProgrammingChapter 2 Control statements.pptx
JavvajiVenkat
 
C Control Statements.docx
JavvajiVenkat
 
loops and iteration.docx
JavvajiVenkat
 
Nested Loops in C unit2.docx
JavvajiVenkat
 

Recently uploaded (20)

PDF
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
PDF
settlement FOR FOUNDATION ENGINEERS.pdf
Endalkazene
 
PDF
Machine Learning All topics Covers In This Single Slides
AmritTiwari19
 
PDF
The Effect of Artifact Removal from EEG Signals on the Detection of Epileptic...
Partho Prosad
 
DOCX
SAR - EEEfdfdsdasdsdasdasdasdasdasdasdasda.docx
Kanimozhi676285
 
PDF
LEAP-1B presedntation xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
hatem173148
 
PDF
Cryptography and Information :Security Fundamentals
Dr. Madhuri Jawale
 
PPTX
quantum computing transition from classical mechanics.pptx
gvlbcy
 
PPTX
22PCOAM21 Session 2 Understanding Data Source.pptx
Guru Nanak Technical Institutions
 
PPTX
Information Retrieval and Extraction - Module 7
premSankar19
 
PPTX
sunil mishra pptmmmmmmmmmmmmmmmmmmmmmmmmm
singhamit111
 
PDF
Advanced LangChain & RAG: Building a Financial AI Assistant with Real-Time Data
Soufiane Sejjari
 
PPTX
Chapter_Seven_Construction_Reliability_Elective_III_Msc CM
SubashKumarBhattarai
 
PDF
Introduction to Ship Engine Room Systems.pdf
Mahmoud Moghtaderi
 
PDF
EVS+PRESENTATIONS EVS+PRESENTATIONS like
saiyedaqib429
 
PPTX
Online Cab Booking and Management System.pptx
diptipaneri80
 
PPT
1. SYSTEMS, ROLES, AND DEVELOPMENT METHODOLOGIES.ppt
zilow058
 
PPTX
MSME 4.0 Template idea hackathon pdf to understand
alaudeenaarish
 
PDF
Biodegradable Plastics: Innovations and Market Potential (www.kiu.ac.ug)
publication11
 
PDF
Unit I Part II.pdf : Security Fundamentals
Dr. Madhuri Jawale
 
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
settlement FOR FOUNDATION ENGINEERS.pdf
Endalkazene
 
Machine Learning All topics Covers In This Single Slides
AmritTiwari19
 
The Effect of Artifact Removal from EEG Signals on the Detection of Epileptic...
Partho Prosad
 
SAR - EEEfdfdsdasdsdasdasdasdasdasdasdasda.docx
Kanimozhi676285
 
LEAP-1B presedntation xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
hatem173148
 
Cryptography and Information :Security Fundamentals
Dr. Madhuri Jawale
 
quantum computing transition from classical mechanics.pptx
gvlbcy
 
22PCOAM21 Session 2 Understanding Data Source.pptx
Guru Nanak Technical Institutions
 
Information Retrieval and Extraction - Module 7
premSankar19
 
sunil mishra pptmmmmmmmmmmmmmmmmmmmmmmmmm
singhamit111
 
Advanced LangChain & RAG: Building a Financial AI Assistant with Real-Time Data
Soufiane Sejjari
 
Chapter_Seven_Construction_Reliability_Elective_III_Msc CM
SubashKumarBhattarai
 
Introduction to Ship Engine Room Systems.pdf
Mahmoud Moghtaderi
 
EVS+PRESENTATIONS EVS+PRESENTATIONS like
saiyedaqib429
 
Online Cab Booking and Management System.pptx
diptipaneri80
 
1. SYSTEMS, ROLES, AND DEVELOPMENT METHODOLOGIES.ppt
zilow058
 
MSME 4.0 Template idea hackathon pdf to understand
alaudeenaarish
 
Biodegradable Plastics: Innovations and Market Potential (www.kiu.ac.ug)
publication11
 
Unit I Part II.pdf : Security Fundamentals
Dr. Madhuri Jawale
 

UNIT-II CP DOC.docx

  • 1. UNIT-II In programming input means reading data from the input devices or from a file. Output means displaying results on the screen. C provides number of input and output functions. These input and output functions are predefined in their respective header files. Header file for standard input and output functions is stdio.h. These are included into program prefixed with #include statement. Input and Output functions are classified into 2 categories. Decision making statements (or) Control statements are used to check the condition, if the condition is true it will executes a block of statements.
  • 2. C Formatted IO Functions Input data or output results are formatted as per requirement. Formatted function improves the readability of the input and output. Formatted functions can be used to read and write data of all data type(char, int, float, double). Formatted input and output functions require format specifiers(%c, %d, %f, %lf) to identify the type of data. scanf() Function Scanf function reads all types of data value from input devices (or) from a file. The address operator '&' is to indicate the memory location of the variable. This memory location is used to store the data which is read through keyboard. scanf() Function Program scanf.c
  • 3. #include <stdio.h> //header file section int main() { int a, b, c; printf("Enter the value of a = "); scanf("%d",&a) ; //read value a through keyboard printf("nEnter the value of b = "); scanf("%d",&b); //read value b through keyboard c = a + b; printf("na + b = %d ",c); //Print the sum of two value a and b return 0; }  Enter the value of a = 6  Enter the value of b = 4  a + b = 10 Width specifier program widthspecifier.c #include <stdio.h> //header file section int main() { printf("n%.2s","abcdefg "); printf("n%.3s","abcdefg "); printf("n%.4s","abcdefg "); printf("n%.5s","abcdefg "); printf("n%.6s","abcdefg "); return 0; }
  • 4.  ab  abc  abcd  abcde  abcdef Note: Output itself shows the process done by %.2s, %.3s, etc.. scanf to Get Input getinput.c #include <stdio.h> //header file section int main() { float a, b, c, d; printf("Enter three float numbers:n "); scanf("n %f %f %f ",&a,&b,&c); d = (a + b + c)/3; printf("nAverage of given number is %f ",d); return 0; }  Enter three float numbers:  4.5  4.5  5.0  Average of given number is 4.666667 Note: Here, three float variables a, b and c read through scanf() function. The resultant average value is stored in a variable d.
  • 5. C Unformatted Functions Unformatted input and output functions are only work with character data type. Unformatted input and output functions do not require any format specifiers. Because they only work with character data type. Character IO Functions getchar() Function The getchar() function reads character type data form the input. The getchar() function reads one character at a time till the user presses the enter key. getchar() C Program getchar.c #include <stdio.h> //header file section #include <conio.h> int main() { char c; printf("Enter a character : "); c = getchar(); printf("nEntered character : %c ", c); return 0; }  Enter a character : y  Entered character : y Note:
  • 6. Here, getchar() reads the input from the user and display back to the user. getch() Function The getch() function reads the alphanumeric character input from the user. But, that the entered character will not be displayed. getch() C Program getch.c #include <stdio.h> //header file section #include <conio.h> int main() { printf("nHello, press any alphanumeric character to exit "); getch(); return 0; }  Hello, press any alphanumeric character to exit Note: The above program will run until you press one of many alphanumeric characters. The key pressed by you will not be displayed. getche() Function getche() function reads the alphanumeric character from the user input. Here, character you entered will be echoed to the user until he/she presses any key. getche() C Program
  • 7. getche.c #include <stdio.h> //header file section #include <conio.h> int main() { printf("nHello, press any alphanumeric character or symbol to exit n "); getche(); return 0; }  Hello, press any alphanumeric character or symbol to exit  k Note: The above program will run until you press one of many alphanumeric characters. The key pressed by you will be echoed. putchar() Function putchar() function prints only one character at a time. putchar() C Program putchar.c #include <stdio.h> //header file section #include <conio.h> int main() { char c = 'K'; putchar(c); return 0;
  • 8. }  K Note: Here, variable c is assigned to a character 'K'. The variable c is displayed by the putchar(). Use Single quotation mark ' ' for a character. putch() Function The putch() function prints any alphanumeric character. putch() C Program putch.c #include <stdio.h> //header file section #include <conio.h> int main() { char c; printf("Press any key to continuen "); c = getch(); printf("input : "); putch(c); return 0; }  Press any key to continue  input : d Note: The getch() function will not echo a character. The putch() function displays the input you pressed.
  • 9. String IO Functions gets() Function The gets() function can read a full string even blank spaces presents in a string. But, the scanf() function leave a string after blank space space is detected. The gets() function is used to get any string from the user. gets() C Program gets.c #include <stdio.h> //header file section #include <conio.h> int main() { char c[25]; printf("Enter a string : "); gets(c); printf("n%s is awesome ",c); return 0; }  Enter a string : Randy Orton  Randy Orton is awesome Note: The gets() function reads a string from through keyboard and stores it in character array c[25]. The printf() function displays a string on the console. puts() Function
  • 10. The puts() function prints the charater array or string on the console. The puts() function is similar to printf() function, but we cannot print other than characters using puts() function. puts() C Program puts.c #include <stdio.h> //header file section #include <conio.h> int main() { char c[25]; printf("Enter your Name : "); gets(c); puts(c); return 0; }  Enter your Name: john  john clrscr() in C clrscr() is an inbuilt library function which is used to clear the previous output displayed in a screen. clrscr() is defined in #include <conio.h> header file. clrscr() Function Program clrscr.c #include <stdio.h> //header file section
  • 11. #include <conio.h> int main() { printf("Before clrscr"); clrscr(); printf("clrscr() will clear the screen"); return 0; }  clrscr() will clear the screen Note: clrscr(); works in Turbo C/C++ compiler. exit() in C exit() is an inbuilt library function which is used to terimate the program irrespective of the statements followed by it. exit() is defined in #include <stdlib.h> header file. exit() Function Program exit.c #include <stdio.h> //header file section #include <stdlib.h> int main() { printf("This statement is before exit(); function "); exit(0); printf("It will not display "); return 0; }
  • 12.  This statement is before exit(); function Note: exit (some numeric value); will exit the program. sleep() in C sleep() is an inbuilt library function which is used to delay the program's output. sleep() is defined in #include <unistd.h> header file. sleep() Function Program sleep.c #include <stdio.h> #include <unistd.h> int main() { printf("Countdown... "); printf("n 3"); sleep(1); printf("n 2"); sleep(1); printf("n 1"); sleep(1); printf("n Celebration Time "); return 0; }  Countdown...  3  2  1  Celebration Time
  • 13. Note: sleep (seconds); will delay the program's output with respect to seconds which you mentioned. C Control Statements C Programs that we encountered up to now were executed in the same order which they appeared in it. In practical applications, there is numerous situations where we have to neglect some parts of program codes. For this purpose, C Programming uses the control statements to control the flow of a program. If the condition is satisfied the code followed by that condition will execute. If not simply that the code will be neglected by the compiler. Types of Control statements  if  if else  if-else-if control statement
  • 14.  switch() case control statement C if statement Why if Statement? All programming languages enable you to make decisions. They enable the program to follow a certain course of action depending on whether a particular condition is met. This is what gives programming language their intelligence. C if Syntax And Definition  C uses the keyword if to execute a set of statements when logical condition is true.  When the logical condition is false, the compiler simply skips the statement within the block.  The "if" statement is also known as one way decision statement. Syntax if statement Syntax if(condition)
  • 15. { here goes statements; . . . . here goes statements; } if Statement Uses if statements are commonly used in following scenarios  Is A bigger than B?  Is X equal to Y?  Is M not equal to N? Realtime Time if Statement Uses Arduino microcontroller make use C programming, where you need to blink a warning light(red light) when certain condition met. Example program is as below: realtimeif if(x = 123)
  • 16. digitalWrite(LEDpin, high) Note: x is a variable which get its input from a sensor continuously. if Statement Rules  The expression (or) condition is always enclosed within pair of parenthesis. e.g.) if ( a > b )  The if statement should not be terminated with a semicolon. If it happens, the block of statements will not execute even the condition is true.  The statements following the if condition is normally enclosed between 2 braces (in curly braces). if statement Flow Chart Following flow chart will clearly explain how if statement works if statement C Program ifstatement.c #include <stdio.h> //header file section #include <conio.h> int main() {
  • 17. int age = 18; if(age > 17){ printf("you are eligible for voting "); } printf("nThis is normal flow "); return 0; }  you are eligible for voting  This is normal flow Note: If the user input is greater than 17, then the condition will be true and the statements under if condition gets executed. Otherwise, it executes next to the if block.