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

PPT
Operator overloading in C++
PDF
Lesson 03 python statement, indentation and comments
PPTX
PPTX
System Programing Unit 1
PPTX
Csma cd and csma-ca
PPTX
Managing input and output operations in c
PPT
Computer architecture register transfer languages rtl
PPT
Operator Overloading
Operator overloading in C++
Lesson 03 python statement, indentation and comments
System Programing Unit 1
Csma cd and csma-ca
Managing input and output operations in c
Computer architecture register transfer languages rtl
Operator Overloading

What's hot (20)

PDF
Unit 2
PPTX
MACRO PROCESSOR
PPTX
Method overloading
PPTX
Need of object oriented programming
PPTX
Operators in C Programming
PPT
PDF
Character set in c
DOCX
Control Units : Microprogrammed and Hardwired:control unit
PPT
Macros in system programing
PPT
Basic concepts of object oriented programming
PPTX
ALOHA Protocol (in detail)
PDF
Problem Solving Techniques and Introduction to C
PPT
Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...
PPT
Sliding window protocol
PPTX
Datapath Design of Computer Architecture
PDF
Unit 3
PPTX
Dynamic Programming Code-Optimization Algorithm (Compiler Design)
PPSX
Complete C programming Language Course
PPT
Function overloading(c++)
PDF
Unit 2_ Flow & Error Control in computer networks
Unit 2
MACRO PROCESSOR
Method overloading
Need of object oriented programming
Operators in C Programming
Character set in c
Control Units : Microprogrammed and Hardwired:control unit
Macros in system programing
Basic concepts of object oriented programming
ALOHA Protocol (in detail)
Problem Solving Techniques and Introduction to C
Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...
Sliding window protocol
Datapath Design of Computer Architecture
Unit 3
Dynamic Programming Code-Optimization Algorithm (Compiler Design)
Complete C programming Language Course
Function overloading(c++)
Unit 2_ Flow & Error Control in computer networks
Ad

Viewers also liked (20)

PDF
Lecture15 comparisonoftheloopcontrolstructures.ppt
PDF
Html phrase tags
PDF
Lecture21 categoriesof userdefinedfunctions.ppt
PPT
Mesics lecture 3 c – constants and variables
PDF
Lecture 7 relational_and_logical_operators
PDF
Lecture7relationalandlogicaloperators 110823181038-phpapp02
PPT
Mesics lecture files in 'c'
PDF
Algorithm
PPT
Mesics lecture 7 iteration and repetitive executions
PPT
Mesics lecture 8 arrays in 'c'
PDF
Unit 1.3 types of cloud
PDF
Unit 1.1 introduction to cloud computing
PDF
Algorithm chapter 11
PDF
Lecture19 unionsin c.ppt
PDF
Unit 1.2 move to cloud computing
PDF
Html text and formatting
PPT
Linked list
PPT
Mesics lecture 4 c operators and experssions
PDF
Lecture20 user definedfunctions.ppt
PDF
Lecture13 control statementswitch.ppt
Lecture15 comparisonoftheloopcontrolstructures.ppt
Html phrase tags
Lecture21 categoriesof userdefinedfunctions.ppt
Mesics lecture 3 c – constants and variables
Lecture 7 relational_and_logical_operators
Lecture7relationalandlogicaloperators 110823181038-phpapp02
Mesics lecture files in 'c'
Algorithm
Mesics lecture 7 iteration and repetitive executions
Mesics lecture 8 arrays in 'c'
Unit 1.3 types of cloud
Unit 1.1 introduction to cloud computing
Algorithm chapter 11
Lecture19 unionsin c.ppt
Unit 1.2 move to cloud computing
Html text and formatting
Linked list
Mesics lecture 4 c operators and experssions
Lecture20 user definedfunctions.ppt
Lecture13 control statementswitch.ppt
Ad

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

PDF
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
PDF
Introduction to Input/Output Functions in C
PPTX
c_pro_introduction.pptx
PPT
CPU INPUT OUTPUT
PDF
C basics
PPT
C tutorial
PDF
C SLIDES PREPARED BY M V B REDDY
PPTX
Input and Output In C Language
PPTX
Basic Input and Output
DOCX
C cheat sheet for varsity (extreme edition)
PPTX
C language
PPT
input
PDF
Chapter 5
PPT
Cbasic
PPT
Introduction to C Programming
PPT
Unit 4 Foc
PDF
C Programming
PPTX
Introduction to C Programming language Chapter02.pptx
PPTX
Programming in C Basics
PPT
T03 a basicioprintf
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
Introduction to Input/Output Functions in C
c_pro_introduction.pptx
CPU INPUT OUTPUT
C basics
C tutorial
C SLIDES PREPARED BY M V B REDDY
Input and Output In C Language
Basic Input and Output
C cheat sheet for varsity (extreme edition)
C language
input
Chapter 5
Cbasic
Introduction to C Programming
Unit 4 Foc
C Programming
Introduction to C Programming language Chapter02.pptx
Programming in C Basics
T03 a basicioprintf

More from eShikshak (15)

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
PPT
Mesics lecture 6 control statement = if -else if__else
PPT
Mesics lecture 5 input – output in ‘c’
PDF
Lecture18 structurein c.ppt
PDF
Lecture17 arrays.ppt
PDF
Lecturer23 pointersin c.ppt
PDF
Program development cyle
PDF
Language processors
PDF
Computer programming programming_langugages
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
Mesics lecture 6 control statement = if -else if__else
Mesics lecture 5 input – output in ‘c’
Lecture18 structurein c.ppt
Lecture17 arrays.ppt
Lecturer23 pointersin c.ppt
Program development cyle
Language processors
Computer programming programming_langugages

Recently uploaded (20)

PDF
Electronic commerce courselecture one. Pdf
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Empathic Computing: Creating Shared Understanding
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Encapsulation theory and applications.pdf
PDF
Modernizing your data center with Dell and AMD
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
KodekX | Application Modernization Development
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
NewMind AI Monthly Chronicles - July 2025
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Electronic commerce courselecture one. Pdf
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Empathic Computing: Creating Shared Understanding
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
NewMind AI Weekly Chronicles - August'25 Week I
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Encapsulation theory and applications.pdf
Modernizing your data center with Dell and AMD
Building Integrated photovoltaic BIPV_UPV.pdf
Review of recent advances in non-invasive hemoglobin estimation
Digital-Transformation-Roadmap-for-Companies.pptx
KodekX | Application Modernization Development
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Per capita expenditure prediction using model stacking based on satellite ima...
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
NewMind AI Monthly Chronicles - July 2025
Understanding_Digital_Forensics_Presentation.pptx
How UI/UX Design Impacts User Retention in Mobile Apps.pdf

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);