SlideShare une entreprise Scribd logo
1  sur  33
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);

Contenu connexe

Tendances

Mobility Management in Wireless Communication
Mobility Management in Wireless CommunicationMobility Management in Wireless Communication
Mobility Management in Wireless Communication
Don Norwood
 
Digital t carriers and multiplexing power point (laurens)
Digital t carriers and multiplexing power point (laurens)Digital t carriers and multiplexing power point (laurens)
Digital t carriers and multiplexing power point (laurens)
Laurens Luis Bugayong
 
Digital communication
Digital communicationDigital communication
Digital communication
meashi
 
Signal modelling
Signal modellingSignal modelling
Signal modelling
Debangi_G
 
Arp and rarp
Arp and rarpArp and rarp
Arp and rarp
1991shalu
 
Digital modulation
Digital modulationDigital modulation
Digital modulation
Ibrahim Omar
 

Tendances (20)

Mobility Management in Wireless Communication
Mobility Management in Wireless CommunicationMobility Management in Wireless Communication
Mobility Management in Wireless Communication
 
Stack in 8085 microprocessor
Stack in 8085 microprocessorStack in 8085 microprocessor
Stack in 8085 microprocessor
 
Arm architecture
Arm architectureArm architecture
Arm architecture
 
Digital t carriers and multiplexing power point (laurens)
Digital t carriers and multiplexing power point (laurens)Digital t carriers and multiplexing power point (laurens)
Digital t carriers and multiplexing power point (laurens)
 
8085 microprocessor Embedded system
8085 microprocessor  Embedded system8085 microprocessor  Embedded system
8085 microprocessor Embedded system
 
Embedded Systems with ARM Cortex-M Microcontrollers in Assembly Language and ...
Embedded Systems with ARM Cortex-M Microcontrollers in Assembly Language and ...Embedded Systems with ARM Cortex-M Microcontrollers in Assembly Language and ...
Embedded Systems with ARM Cortex-M Microcontrollers in Assembly Language and ...
 
Digital base band modulation
Digital base band modulationDigital base band modulation
Digital base band modulation
 
8085 microprocessor Architecture and Pin description
8085 microprocessor Architecture and Pin description 8085 microprocessor Architecture and Pin description
8085 microprocessor Architecture and Pin description
 
Signalling Techniques & Basics of CCS
Signalling Techniques & Basics of CCSSignalling Techniques & Basics of CCS
Signalling Techniques & Basics of CCS
 
radio propagation
radio propagationradio propagation
radio propagation
 
Logical and shift micro operations
Logical and shift micro operationsLogical and shift micro operations
Logical and shift micro operations
 
Baud rate is the number of change in signal
Baud rate is the number of change in signalBaud rate is the number of change in signal
Baud rate is the number of change in signal
 
Digital communication
Digital communicationDigital communication
Digital communication
 
Error Correction And Hamming Code Ibrar
Error Correction And Hamming Code IbrarError Correction And Hamming Code Ibrar
Error Correction And Hamming Code Ibrar
 
ASk,FSK,PSK
ASk,FSK,PSKASk,FSK,PSK
ASk,FSK,PSK
 
Signal modelling
Signal modellingSignal modelling
Signal modelling
 
8086 architecture and pin description
8086 architecture and pin description 8086 architecture and pin description
8086 architecture and pin description
 
Icmp
IcmpIcmp
Icmp
 
Arp and rarp
Arp and rarpArp and rarp
Arp and rarp
 
Digital modulation
Digital modulationDigital modulation
Digital modulation
 

En vedette

Lecture7relationalandlogicaloperators 110823181038-phpapp02
Lecture7relationalandlogicaloperators 110823181038-phpapp02Lecture7relationalandlogicaloperators 110823181038-phpapp02
Lecture7relationalandlogicaloperators 110823181038-phpapp02
eShikshak
 
Algorithm chapter 11
Algorithm chapter 11Algorithm chapter 11
Algorithm chapter 11
chidabdu
 
Lecture19 unionsin c.ppt
Lecture19 unionsin c.pptLecture19 unionsin c.ppt
Lecture19 unionsin c.ppt
eShikshak
 
Html text and formatting
Html text and formattingHtml text and formatting
Html text and formatting
eShikshak
 
Mesics lecture 4 c operators and experssions
Mesics lecture  4   c operators and experssionsMesics lecture  4   c operators and experssions
Mesics lecture 4 c operators and experssions
eShikshak
 

En vedette (20)

Lecture15 comparisonoftheloopcontrolstructures.ppt
Lecture15 comparisonoftheloopcontrolstructures.pptLecture15 comparisonoftheloopcontrolstructures.ppt
Lecture15 comparisonoftheloopcontrolstructures.ppt
 
Html phrase tags
Html phrase tagsHtml phrase tags
Html phrase tags
 
Lecture21 categoriesof userdefinedfunctions.ppt
Lecture21 categoriesof userdefinedfunctions.pptLecture21 categoriesof userdefinedfunctions.ppt
Lecture21 categoriesof userdefinedfunctions.ppt
 
Mesics lecture 3 c – constants and variables
Mesics lecture 3   c – constants and variablesMesics lecture 3   c – constants and variables
Mesics lecture 3 c – constants and variables
 
Lecture 7 relational_and_logical_operators
Lecture 7 relational_and_logical_operatorsLecture 7 relational_and_logical_operators
Lecture 7 relational_and_logical_operators
 
Lecture7relationalandlogicaloperators 110823181038-phpapp02
Lecture7relationalandlogicaloperators 110823181038-phpapp02Lecture7relationalandlogicaloperators 110823181038-phpapp02
Lecture7relationalandlogicaloperators 110823181038-phpapp02
 
Mesics lecture files in 'c'
Mesics lecture   files in 'c'Mesics lecture   files in 'c'
Mesics lecture files in 'c'
 
Algorithm
AlgorithmAlgorithm
Algorithm
 
Mesics lecture 7 iteration and repetitive executions
Mesics lecture 7   iteration and repetitive executionsMesics lecture 7   iteration and repetitive executions
Mesics lecture 7 iteration and repetitive executions
 
Mesics lecture 8 arrays in 'c'
Mesics lecture 8   arrays in 'c'Mesics lecture 8   arrays in 'c'
Mesics lecture 8 arrays in 'c'
 
Unit 1.3 types of cloud
Unit 1.3 types of cloudUnit 1.3 types of cloud
Unit 1.3 types of cloud
 
Unit 1.1 introduction to cloud computing
Unit 1.1   introduction to cloud computingUnit 1.1   introduction to cloud computing
Unit 1.1 introduction to cloud computing
 
Algorithm chapter 11
Algorithm chapter 11Algorithm chapter 11
Algorithm chapter 11
 
Lecture19 unionsin c.ppt
Lecture19 unionsin c.pptLecture19 unionsin c.ppt
Lecture19 unionsin c.ppt
 
Unit 1.2 move to cloud computing
Unit 1.2   move to cloud computingUnit 1.2   move to cloud computing
Unit 1.2 move to cloud computing
 
Html text and formatting
Html text and formattingHtml text and formatting
Html text and formatting
 
Linked list
Linked listLinked list
Linked list
 
Mesics lecture 4 c operators and experssions
Mesics lecture  4   c operators and experssionsMesics lecture  4   c operators and experssions
Mesics lecture 4 c operators and experssions
 
Lecture20 user definedfunctions.ppt
Lecture20 user definedfunctions.pptLecture20 user definedfunctions.ppt
Lecture20 user definedfunctions.ppt
 
Lecture13 control statementswitch.ppt
Lecture13 control statementswitch.pptLecture13 control statementswitch.ppt
Lecture13 control statementswitch.ppt
 

Similaire à Mesics lecture 5 input – output in ‘c’

Chapter 13.1.3
Chapter 13.1.3Chapter 13.1.3
Chapter 13.1.3
patcha535
 

Similaire à Mesics lecture 5 input – output in ‘c’ (20)

CPU INPUT OUTPUT
CPU INPUT OUTPUT CPU INPUT OUTPUT
CPU INPUT OUTPUT
 
Introduction to Input/Output Functions in C
Introduction to Input/Output Functions in CIntroduction to Input/Output Functions in C
Introduction to Input/Output Functions in C
 
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
MANAGING INPUT AND OUTPUT OPERATIONS IN C    MRS.SOWMYA JYOTHI.pdfMANAGING INPUT AND OUTPUT OPERATIONS IN C    MRS.SOWMYA JYOTHI.pdf
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
 
Basic Input and Output
Basic Input and OutputBasic Input and Output
Basic Input and Output
 
input
inputinput
input
 
Input And Output
 Input And Output Input And Output
Input And Output
 
Unit 2- Module 2.pptx
Unit 2- Module 2.pptxUnit 2- Module 2.pptx
Unit 2- Module 2.pptx
 
CHAPTER 4
CHAPTER 4CHAPTER 4
CHAPTER 4
 
Fundamental of C Programming Language and Basic Input/Output Function
  Fundamental of C Programming Language and Basic Input/Output Function  Fundamental of C Programming Language and Basic Input/Output Function
Fundamental of C Programming Language and Basic Input/Output Function
 
Unit 5 Foc
Unit 5 FocUnit 5 Foc
Unit 5 Foc
 
2 data and c
2 data and c2 data and c
2 data and c
 
Fucntions & Pointers in C
Fucntions & Pointers in CFucntions & Pointers in C
Fucntions & Pointers in C
 
Concepts of C [Module 2]
Concepts of C [Module 2]Concepts of C [Module 2]
Concepts of C [Module 2]
 
Input output statement in C
Input output statement in CInput output statement in C
Input output statement in C
 
Cse115 lecture04introtoc programming
Cse115 lecture04introtoc programmingCse115 lecture04introtoc programming
Cse115 lecture04introtoc programming
 
C programing Tutorial
C programing TutorialC programing Tutorial
C programing Tutorial
 
Introduction to Basic C programming 02
Introduction to Basic C programming 02Introduction to Basic C programming 02
Introduction to Basic C programming 02
 
Chapter 13.1.3
Chapter 13.1.3Chapter 13.1.3
Chapter 13.1.3
 
C Programming
C ProgrammingC Programming
C Programming
 
C programming language for beginners
C programming language for beginners C programming language for beginners
C programming language for beginners
 

Plus de eShikshak

Mesics lecture 5 input – output in ‘c’
Mesics lecture 5   input – output in ‘c’Mesics lecture 5   input – output in ‘c’
Mesics lecture 5 input – output in ‘c’
eShikshak
 
Lecturer23 pointersin c.ppt
Lecturer23 pointersin c.pptLecturer23 pointersin c.ppt
Lecturer23 pointersin c.ppt
eShikshak
 
Language processors
Language processorsLanguage processors
Language processors
eShikshak
 

Plus de eShikshak (15)

Modelling and evaluation
Modelling and evaluationModelling and evaluation
Modelling and evaluation
 
Operators in python
Operators in pythonOperators in python
Operators in python
 
Datatypes in python
Datatypes in pythonDatatypes in python
Datatypes in python
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
 
Introduction to e commerce
Introduction to e commerceIntroduction to e commerce
Introduction to e commerce
 
Chapeter 2 introduction to cloud computing
Chapeter 2   introduction to cloud computingChapeter 2   introduction to cloud computing
Chapeter 2 introduction to cloud computing
 
Unit 1.4 working of cloud computing
Unit 1.4 working of cloud computingUnit 1.4 working of cloud computing
Unit 1.4 working of cloud computing
 
Mesics lecture 6 control statement = if -else if__else
Mesics lecture 6   control statement = if -else if__elseMesics lecture 6   control statement = if -else if__else
Mesics lecture 6 control statement = if -else if__else
 
Mesics lecture 5 input – output in ‘c’
Mesics lecture 5   input – output in ‘c’Mesics lecture 5   input – output in ‘c’
Mesics lecture 5 input – output in ‘c’
 
Lecture18 structurein c.ppt
Lecture18 structurein c.pptLecture18 structurein c.ppt
Lecture18 structurein c.ppt
 
Lecture17 arrays.ppt
Lecture17 arrays.pptLecture17 arrays.ppt
Lecture17 arrays.ppt
 
Lecturer23 pointersin c.ppt
Lecturer23 pointersin c.pptLecturer23 pointersin c.ppt
Lecturer23 pointersin c.ppt
 
Program development cyle
Program development cyleProgram development cyle
Program development cyle
 
Language processors
Language processorsLanguage processors
Language processors
 
Computer programming programming_langugages
Computer programming programming_langugagesComputer programming programming_langugages
Computer programming programming_langugages
 

Dernier

IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Dernier (20)

A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.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);