SlideShare a Scribd company logo
Input – Output in ‘C’

  www.eshikshak.co.in
Introduction
• Reading input data, processing it and displaying the
  results are the three tasks of any program.
• There are two ways to accept the data.
   – In one method, a data value is assigned to the variable with an
     assignment statement.
      • int year = 2005; char letter = ‘a’;   int x = 12345;
   – Another way of accepting the data is with functions.
• There are a number of I/O functions in C, based
  on the data type. The input/output functions are
  classified in two types.
   – Formatted functions
   – Unformatted functions
Formatted function
• With the formatted functions, the input or
  output is formatted as per our requirement.
• All the I/O function are defined as stdio.h
  header file.
• Header file should be included in the program
  at the beginning.
Input and Output Functions




Formatted Functions                                Unformatted Functions




      printf()
      scanf()
                                                       getch() putch()
                                                     getche() putchar()
                                                    getchar()      puts()
                                                            gets()
Formatted Functions             Unformatted Functions
• It read and write all types   • Works only with character
  of data values.                 data type
• Require format string to      • Do not require format
  produce formatted result        conversion for formatting
• Returns value after             data type
  execution
printf() function
• This function displays output with specified format
• It requires format conversion symbol or format string
  and variables names to the print the data
• The list of variables are specified in the printf()
  statement
• The values of the variables are printed as the
  sequence mentioned in printf()
• The format string symbol and variable name should
  be the same in number and type
printf() function
• Syntax
  printf(“control string”, varialbe1, variable2,..., variableN);


• The control string specifies the field format such as
  %d, %s, %g, %f and variables as taken by the
  programmer
void main()
{
   int NumInt = 2;
  float NumFloat=2.2;
  char LetterCh = ‘C’;

    printf(“%d %f %c”, NumInt, NumFloat, LetterCh);
}

Output :
2 2.2000 C
void main()
{
   int NumInt = 65;
   clrscr();
   printf(“%c %d”, NumInt, NumInt);
}

Output :
A 65
void main()
{
   int NumInt = 7;
   clrscr();
   printf(“%f”, NumInt);
    return 0;
}

Output :
Error Message : “Floating points formats not linked”
void main()
{
   int NumInt = 7;
   clrscr();
   printf(“%f”, NumInt);
    return 0;
}

Output :
Error Message : “Floating points formats not linked”
• All the format specification starts with % and a
  format specification letter after this symbol.
• It indicates the type of data and its format.
• If the format string does not match with the
  corresponding variable, the result will not be
  correct.
• Along with format specification use
  – Flags
  – Width
  – Precision
• Flag
  – It is used for output justification, numeric signs, decimal
    points, trailing zeros.
  – The flag (-) justifies the result. If it is not given the default
    result is right justification.
• Width
  – It sets the minimum field width for an output value.
  – Width can be specified through a decimal point or using an
    asterisk ‘*’.
void main()
{
   clrscr();
   printf(“n%.2s”,”abcdef”);
   printf(“n%.3s”,”abcdef”);
   printf(“n%.4s”,”abcdef”);
}
OUTPUT
ab
  abc
  abcd
void main()
{
  int x=55, y=33;
  clrscr();
  printf(“n %3d”, x – y);
  printf(“n %6d”, x – y);
}
OUTPUT
22
       22
void main()
{
  int x=55, y=33;
  clrscr();
  printf(“n %*d”, 15, x – y);
  printf(“n %*d”, 5,x – y);
}
OUTPUT
       22
  22
void main()
{
   float g=123.456789;
   clrscr();
   printf(“n %.1f”, g);
   printf(“n %.2f”, g);
   printf(“n %.3f”, g);
   printf(“n %.4f”, g);
}
OUTPUT
123.5
123.46
123.457
123.4568
Sr. No   Format   Meaning              Explanation


1        %wd      Format for integer   w is width in integer and d
                  output               is conversion specification
2        %w.cf    Format for float     w is width in integer, c
                  numbers              specifies the number of
                                       digits after decimal point
                                       and     f    specifies    the
                                       conversion specification
3        %w.cs    Format for string    w is width for total
                  output               characters, c are used
                                       displaying leading blanks
                                       and s specifies conversion
                                       specification
scanf() function
• scanf() function reads all the types of data
  values.
• It is used for runtime assignment of variables.
• The scanf() statement also requires
  conversion symbol to identify the data to be
  read during the execution of the program.
• The scanf() stops functioning when some
  input entered does not match format string.
scanf() function
Syntax :
scanf(“%d %f %c”, &a, &b, &c);
 Scanf statement requires ‘&’ operator called address
  operator
 The address operator prints the memory location of
  the variable
 scanf() statement the role of ‘&’ operator is to
  indicate the memory location of the variable, so that
  the value read would be placed at that location.
scanf() function
 The scanf() function statement also return values.
  The return value is exactly equal to the number of
  values correctly read.
 If the read value is convertible to the given format,
  conversion is made.
void main()
{
  int a;
  clrscr();
  printf(“Enter value of ‘A’ : “);
  scanf(“%c”, &a);
  printf(“A : %c”,a);
}
OUTPUT
Enter value of ‘A’ : 8
A:8
void main()
{
  char a;
  clrscr();
  printf(“Enter value of ‘A’ : “);
  scanf(“%d”, &a);
  printf(“A : %d”,a);
}
OUTPUT
Enter value of ‘A’ : 255
A : 255
Enter value of ‘A’ : 256
A : 256
Sr. No   Format   Meaning              Explanation


1        %wd      Format for integer   w is width in integer and d
                  input                is conversion specification
2        %w.cf    Format for float     w is width in integer, c
                  point input          specifies the number of
                                       digits after decimal point
                                       and     f    specifies    the
                                       conversion specification
3        %w.cs    Format for string    w is width for total
                  input                characters, c are used
                                       displaying leading blanks
                                       and s specifies conversion
                                       specification
Data Type                                      Format string
Integer                 Short Integer          %d or %i
                        Short unsigned         %u
                        Long signed            %ld
                        Long unsigned          %lu
                        Unsigned hexadecimal   %u
                        Unsigned octal         %o
Real                    Floating               %f or %g
                        Double Floating        %lf
Character               Signed Character       %c
                        Unsigned Character     %c
                        String                 %s
Octal number                                   %o
Displays Hexa decimal                          %hx
number in lowercase
Displays Hexa decimal                          %p
number in lowercase


Aborts program with                            %n
error
Escape Sequence
                                   Escape Sequence   Use               ASCII value

• printf() and scanf() statement   n                New Line          10
  follows the combination of
  characters called escape         b                Backspace         8
  sequence                         f                Form feed         12
• Escape sequence are special      ’                Single quote      39
  characters starting with ‘’                     Backslash         92
                                   0                Null              0
                                   t                Horizontal Tab    9
                                   r                Carriage Return   13
                                   a                Alert             7
                                   ”                Double Quote      34
                                   v                Variable tab      11
                                   ?                Question mark     63
void main()
{
   int a = 1, b = a + 1, c = b + 1, d = c + 1;
   clrscr();
   printf(“t A = %dnB = %d ’C = %d’”,a,b,c);
   printf(“nb***D = %d**”,d);
   printf(“n*************”);
   printf(“rA = %d B = %d”, a, b);
}

OUTPUT
         A=1
B=2      ‘C = 3’
***D=4**
A = 1 B = 2******
Unformatted Functions
• C has three types of I/O functions
  – Character I/O
  – String I/O
  – File I/O
  – Character I/O
getchar
• This function reads a character type data from
  standard input.
• It reads one character at a time till the user presses
  the enter key.
• Syntax
   VariableName = getchar();
• Example
   char c;
   c = getchar();
putchar
• This function prints one character on the
  screen at a time, read by the standard input.
• Syntax
  – puncher(variableName)
• Example
    char c = ‘C’;
    putchar(c);
getch() and getche()
• These functions read any alphanumeric character
  from the standard input device.
• The character entered is not displayed by the getch()
  function.
• The character entered is displayed by the getche()
  function.
• Exampe
    ch = getch();
    ch = getche();
gets()
• This function is used for accepting any string through stdin
  keyword until enter key is pressed.
• The header file stdio.h is needed for implementing the
  above function.
• Syntax
   char str[length of string in number];
   gets(str);
 void main()
 {
       char ch[30];
       clrscr();
       printf(“Enter the string : “);
       gets();
       printf(“n Entered string : %s”, ch);
 }
puts()
• This function prints the string or character array.
• It is opposite to gets()

  char str[length of string in number];
  gets(str);
  puts(str);

More Related Content

PPTX
C Programming: Control Structure
PPTX
Managing input and output operations in c
PPTX
Pointers in c++
PPTX
Presentation on Function in C Programming
PPTX
Rules for Variable Declaration
PPTX
Pointers in c v5 12102017 1
PPTX
C Programming Language Tutorial for beginners - JavaTpoint
PPSX
C lecture 4 nested loops and jumping statements slideshare
C Programming: Control Structure
Managing input and output operations in c
Pointers in c++
Presentation on Function in C Programming
Rules for Variable Declaration
Pointers in c v5 12102017 1
C Programming Language Tutorial for beginners - JavaTpoint
C lecture 4 nested loops and jumping statements slideshare

What's hot (20)

PPT
Control Structures
PPTX
Decision Making and Looping
PPT
Python Control structures
PPTX
PPTX
Decision making statements in C programming
PPTX
functions in C and types
PPTX
Pointers in c language
PPTX
Function Pointer
PPT
Input And Output
PPT
Formatted input and output
PDF
Java thread life cycle
PDF
R-Language-Lab-Manual-lab-1.pdf
PPTX
Pointer in c
PPTX
Input output statement in C
PPTX
Dynamic memory allocation
PPT
Structure of a C program
PPT
File handling in c
PPTX
register
PPTX
Dynamic Memory Allocation(DMA)
PPT
358 33 powerpoint-slides_7-structures_chapter-7
Control Structures
Decision Making and Looping
Python Control structures
Decision making statements in C programming
functions in C and types
Pointers in c language
Function Pointer
Input And Output
Formatted input and output
Java thread life cycle
R-Language-Lab-Manual-lab-1.pdf
Pointer in c
Input output statement in C
Dynamic memory allocation
Structure of a C program
File handling in c
register
Dynamic Memory Allocation(DMA)
358 33 powerpoint-slides_7-structures_chapter-7
Ad

Similar to Mesics lecture 5 input – output in ‘c’ (20)

PPT
CPU INPUT OUTPUT
PDF
Introduction to Input/Output Functions in C
PDF
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
PPTX
Basic Input and Output
PPT
input
PPTX
Unit 2- Module 2.pptx
PPTX
CHAPTER 4
PPTX
Lecture-2.pptxefygefyeyyegfygcyewvwvvcvywcy
PPT
Fundamental of C Programming Language and Basic Input/Output Function
PPT
Unit 5 Foc
PDF
2 data and c
PPT
Fucntions & Pointers in C
PPSX
Concepts of C [Module 2]
PPTX
unit-5 String Math Date Time AI presentation
PDF
Cse115 lecture04introtoc programming
PDF
C programing Tutorial
PPTX
Introduction to Basic C programming 02
PPTX
Programming_in_C_language_Unit5.pptx course ATOT
PDF
Chapter 13.1.3
DOCX
C Programming
CPU INPUT OUTPUT
Introduction to Input/Output Functions in C
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
Basic Input and Output
input
Unit 2- Module 2.pptx
CHAPTER 4
Lecture-2.pptxefygefyeyyegfygcyewvwvvcvywcy
Fundamental of C Programming Language and Basic Input/Output Function
Unit 5 Foc
2 data and c
Fucntions & Pointers in C
Concepts of C [Module 2]
unit-5 String Math Date Time AI presentation
Cse115 lecture04introtoc programming
C programing Tutorial
Introduction to Basic C programming 02
Programming_in_C_language_Unit5.pptx course ATOT
Chapter 13.1.3
C Programming
Ad

More from eShikshak (20)

PDF
Modelling and evaluation
PDF
Operators in python
PDF
Datatypes in python
PDF
Introduction to python
PPT
Introduction to e commerce
PDF
Chapeter 2 introduction to cloud computing
PDF
Unit 1.4 working of cloud computing
PDF
Unit 1.3 types of cloud
PDF
Unit 1.2 move to cloud computing
PDF
Unit 1.1 introduction to cloud computing
PPT
Mesics lecture files in 'c'
PPT
Mesics lecture 8 arrays in 'c'
PPT
Mesics lecture 7 iteration and repetitive executions
PPT
Mesics lecture 5 input – output in ‘c’
PPT
Mesics lecture 6 control statement = if -else if__else
PPT
Mesics lecture 4 c operators and experssions
PPT
Mesics lecture 3 c – constants and variables
PDF
Lecture 7 relational_and_logical_operators
PDF
Lecture21 categoriesof userdefinedfunctions.ppt
PDF
Lecture20 user definedfunctions.ppt
Modelling and evaluation
Operators in python
Datatypes in python
Introduction to python
Introduction to e commerce
Chapeter 2 introduction to cloud computing
Unit 1.4 working of cloud computing
Unit 1.3 types of cloud
Unit 1.2 move to cloud computing
Unit 1.1 introduction to cloud computing
Mesics lecture files in 'c'
Mesics lecture 8 arrays in 'c'
Mesics lecture 7 iteration and repetitive executions
Mesics lecture 5 input – output in ‘c’
Mesics lecture 6 control statement = if -else if__else
Mesics lecture 4 c operators and experssions
Mesics lecture 3 c – constants and variables
Lecture 7 relational_and_logical_operators
Lecture21 categoriesof userdefinedfunctions.ppt
Lecture20 user definedfunctions.ppt

Recently uploaded (20)

PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
AI And Its Effect On The Evolving IT Sector In Australia - Elevate
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Sensors and Actuators in IoT Systems using pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
Telecom Fraud Prevention Guide | Hyperlink InfoSystem
PDF
Electronic commerce courselecture one. Pdf
PPTX
Big Data Technologies - Introduction.pptx
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PPTX
breach-and-attack-simulation-cybersecurity-india-chennai-defenderrabbit-2025....
PPTX
Cloud computing and distributed systems.
PDF
madgavkar20181017ppt McKinsey Presentation.pdf
PDF
Empathic Computing: Creating Shared Understanding
PDF
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
HCSP-Presales-Campus Network Planning and Design V1.0 Training Material-Witho...
Diabetes mellitus diagnosis method based random forest with bat algorithm
AI And Its Effect On The Evolving IT Sector In Australia - Elevate
Dropbox Q2 2025 Financial Results & Investor Presentation
Sensors and Actuators in IoT Systems using pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
Telecom Fraud Prevention Guide | Hyperlink InfoSystem
Electronic commerce courselecture one. Pdf
Big Data Technologies - Introduction.pptx
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
breach-and-attack-simulation-cybersecurity-india-chennai-defenderrabbit-2025....
Cloud computing and distributed systems.
madgavkar20181017ppt McKinsey Presentation.pdf
Empathic Computing: Creating Shared Understanding
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...
Review of recent advances in non-invasive hemoglobin estimation
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
Understanding_Digital_Forensics_Presentation.pptx
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Spectral efficient network and resource selection model in 5G networks
HCSP-Presales-Campus Network Planning and Design V1.0 Training Material-Witho...

Mesics lecture 5 input – output in ‘c’

  • 1. Input – Output in ‘C’ www.eshikshak.co.in
  • 2. Introduction • Reading input data, processing it and displaying the results are the three tasks of any program. • There are two ways to accept the data. – In one method, a data value is assigned to the variable with an assignment statement. • int year = 2005; char letter = ‘a’; int x = 12345; – Another way of accepting the data is with functions. • There are a number of I/O functions in C, based on the data type. The input/output functions are classified in two types. – Formatted functions – Unformatted functions
  • 3. Formatted function • With the formatted functions, the input or output is formatted as per our requirement. • All the I/O function are defined as stdio.h header file. • Header file should be included in the program at the beginning.
  • 4. Input and Output Functions Formatted Functions Unformatted Functions printf() scanf() getch() putch() getche() putchar() getchar() puts() gets()
  • 5. Formatted Functions Unformatted Functions • It read and write all types • Works only with character of data values. data type • Require format string to • Do not require format produce formatted result conversion for formatting • Returns value after data type execution
  • 6. printf() function • This function displays output with specified format • It requires format conversion symbol or format string and variables names to the print the data • The list of variables are specified in the printf() statement • The values of the variables are printed as the sequence mentioned in printf() • The format string symbol and variable name should be the same in number and type
  • 7. printf() function • Syntax printf(“control string”, varialbe1, variable2,..., variableN); • The control string specifies the field format such as %d, %s, %g, %f and variables as taken by the programmer
  • 8. void main() { int NumInt = 2; float NumFloat=2.2; char LetterCh = ‘C’; printf(“%d %f %c”, NumInt, NumFloat, LetterCh); } Output : 2 2.2000 C
  • 9. void main() { int NumInt = 65; clrscr(); printf(“%c %d”, NumInt, NumInt); } Output : A 65
  • 10. void main() { int NumInt = 7; clrscr(); printf(“%f”, NumInt); return 0; } Output : Error Message : “Floating points formats not linked”
  • 11. void main() { int NumInt = 7; clrscr(); printf(“%f”, NumInt); return 0; } Output : Error Message : “Floating points formats not linked”
  • 12. • All the format specification starts with % and a format specification letter after this symbol. • It indicates the type of data and its format. • If the format string does not match with the corresponding variable, the result will not be correct. • Along with format specification use – Flags – Width – Precision
  • 13. • Flag – It is used for output justification, numeric signs, decimal points, trailing zeros. – The flag (-) justifies the result. If it is not given the default result is right justification. • Width – It sets the minimum field width for an output value. – Width can be specified through a decimal point or using an asterisk ‘*’.
  • 14. void main() { clrscr(); printf(“n%.2s”,”abcdef”); printf(“n%.3s”,”abcdef”); printf(“n%.4s”,”abcdef”); } OUTPUT ab abc abcd
  • 15. void main() { int x=55, y=33; clrscr(); printf(“n %3d”, x – y); printf(“n %6d”, x – y); } OUTPUT 22 22
  • 16. void main() { int x=55, y=33; clrscr(); printf(“n %*d”, 15, x – y); printf(“n %*d”, 5,x – y); } OUTPUT 22 22
  • 17. void main() { float g=123.456789; clrscr(); printf(“n %.1f”, g); printf(“n %.2f”, g); printf(“n %.3f”, g); printf(“n %.4f”, g); } OUTPUT 123.5 123.46 123.457 123.4568
  • 18. Sr. No Format Meaning Explanation 1 %wd Format for integer w is width in integer and d output is conversion specification 2 %w.cf Format for float w is width in integer, c numbers specifies the number of digits after decimal point and f specifies the conversion specification 3 %w.cs Format for string w is width for total output characters, c are used displaying leading blanks and s specifies conversion specification
  • 19. scanf() function • scanf() function reads all the types of data values. • It is used for runtime assignment of variables. • The scanf() statement also requires conversion symbol to identify the data to be read during the execution of the program. • The scanf() stops functioning when some input entered does not match format string.
  • 20. scanf() function Syntax : scanf(“%d %f %c”, &a, &b, &c);  Scanf statement requires ‘&’ operator called address operator  The address operator prints the memory location of the variable  scanf() statement the role of ‘&’ operator is to indicate the memory location of the variable, so that the value read would be placed at that location.
  • 21. scanf() function  The scanf() function statement also return values. The return value is exactly equal to the number of values correctly read.  If the read value is convertible to the given format, conversion is made.
  • 22. void main() { int a; clrscr(); printf(“Enter value of ‘A’ : “); scanf(“%c”, &a); printf(“A : %c”,a); } OUTPUT Enter value of ‘A’ : 8 A:8
  • 23. void main() { char a; clrscr(); printf(“Enter value of ‘A’ : “); scanf(“%d”, &a); printf(“A : %d”,a); } OUTPUT Enter value of ‘A’ : 255 A : 255 Enter value of ‘A’ : 256 A : 256
  • 24. Sr. No Format Meaning Explanation 1 %wd Format for integer w is width in integer and d input is conversion specification 2 %w.cf Format for float w is width in integer, c point input specifies the number of digits after decimal point and f specifies the conversion specification 3 %w.cs Format for string w is width for total input characters, c are used displaying leading blanks and s specifies conversion specification
  • 25. Data Type Format string Integer Short Integer %d or %i Short unsigned %u Long signed %ld Long unsigned %lu Unsigned hexadecimal %u Unsigned octal %o Real Floating %f or %g Double Floating %lf Character Signed Character %c Unsigned Character %c String %s Octal number %o Displays Hexa decimal %hx number in lowercase Displays Hexa decimal %p number in lowercase Aborts program with %n error
  • 26. Escape Sequence Escape Sequence Use ASCII value • printf() and scanf() statement n New Line 10 follows the combination of characters called escape b Backspace 8 sequence f Form feed 12 • Escape sequence are special ’ Single quote 39 characters starting with ‘’ Backslash 92 0 Null 0 t Horizontal Tab 9 r Carriage Return 13 a Alert 7 ” Double Quote 34 v Variable tab 11 ? Question mark 63
  • 27. void main() { int a = 1, b = a + 1, c = b + 1, d = c + 1; clrscr(); printf(“t A = %dnB = %d ’C = %d’”,a,b,c); printf(“nb***D = %d**”,d); printf(“n*************”); printf(“rA = %d B = %d”, a, b); } OUTPUT A=1 B=2 ‘C = 3’ ***D=4** A = 1 B = 2******
  • 28. Unformatted Functions • C has three types of I/O functions – Character I/O – String I/O – File I/O – Character I/O
  • 29. getchar • This function reads a character type data from standard input. • It reads one character at a time till the user presses the enter key. • Syntax VariableName = getchar(); • Example char c; c = getchar();
  • 30. putchar • This function prints one character on the screen at a time, read by the standard input. • Syntax – puncher(variableName) • Example char c = ‘C’; putchar(c);
  • 31. getch() and getche() • These functions read any alphanumeric character from the standard input device. • The character entered is not displayed by the getch() function. • The character entered is displayed by the getche() function. • Exampe  ch = getch();  ch = getche();
  • 32. gets() • This function is used for accepting any string through stdin keyword until enter key is pressed. • The header file stdio.h is needed for implementing the above function. • Syntax char str[length of string in number]; gets(str); void main() { char ch[30]; clrscr(); printf(“Enter the string : “); gets(); printf(“n Entered string : %s”, ch); }
  • 33. puts() • This function prints the string or character array. • It is opposite to gets() char str[length of string in number]; gets(str); puts(str);