SlideShare uma empresa Scribd logo
1 de 7
Baixar para ler offline
Standard I/O
Use of printf() and scanf()
 printf() is abbreviation of Print formatted
 Print out something on the monitor
     printf(“format string..”, argument list);

[Ex]
#include <stdio.h>
main() {
   int n ;
   n = 25 * 10;
   printf(“%d”, n);                      /* print 250. */
}

[Ex]
   printf(“%c%c%c”, ’a’, ’b’, ’c’ );     /* print “abc”. */
   printf(“%s”, “def” );                 /* print “def”. */



                                                              2
Use of printf() and scanf()
 Conversion characters (format string)
%c   character                  printf(“%c”, ’a’);
%d   decimal integer            printf(“%d”, 100);

%x   Hexadecimal integer        printf(“%x”, 100);
%o   Octal integer              printf(“%o”, 100);

%u   Unsigned decimal           printf(“%u”, 100);

%e   floating point number in   printf(“%e”, 1.234);
     scientific notation
%f   floating point number      printf(“%f”, 1.234);

%g   e-format or f-format       printf(“%g”, 1.234);

%s   string                     printf(“%s”, ”C-book”);


                                                          3
Use of printf() and scanf()
 The Use of printf() : integer %d
 printf(“ %md”, a );   /*print a left-justified in m width */
 printf(“ %-md”, a);   /*print a right-justified in m width */
 [Ex]
 a = 12 ;
 printf(“%5d%-5d%dn” ,a, a, a+11);
                                                  12^^^^^^1223

   – %5d : 12^^^
   – %-5d : ^^^12
   – %d : 23




                                                                 4
Use of printf() and scanf()
 The Use of printf() : float %f
 printf(“ %m.pf”, a ); /*print a left-justified in m width
                         with p numbers after decimal point*/
 printf(“ %-m.pf”, a); /*print a right-justified in m width
                         with p numbers after decimal point*/

[Ex]                       83.1260^^83.126083.126000^^^83.13
a = 83.126 ;
   printf(“%8.4f%-8.4f%f%-7.2fn”, a, a, a, a );

   –   %8.4f : 83.1260^
   –   %-8.4f : ^83.1260
   –   %f     : 83.126000
   –   %-7.2f : ^^^83.13



                                                                5
Use of printf() and scanf()
 scanf() is abbreviation of Scanf formatted
 Reads data from standard input


        scanf(“format string..”, argument list);

  %c       character                               scanf(“%c”, &a);

  %d       decimal integer                         scanf(“%d”, &a);

  %f       floating point number                   scanf(“%f”, &a);

  %lf      floating point number                   scanf(“%lf”, &a);

  %Lf      floating point number                   scanf(“%Lf”, &a);

  %s       string                                  scanf(“%s”, &a);




                                                                       6
Use of printf() and scanf()
[Ex]
#include <stdio.h>

main() {
   int n ;
   printf(“Enter number : “);
   scanf(“%d”, &n);
   printf(“You entered : %d”, n);
   return 0;
}




                                    Enter number : 10
                                    You entered : 10

                                                        7

Mais conteúdo relacionado

Mais procurados (20)

ภาษาซี
ภาษาซีภาษาซี
ภาษาซี
 
Csci101 lect05 formatted_output
Csci101 lect05 formatted_outputCsci101 lect05 formatted_output
Csci101 lect05 formatted_output
 
C file
C fileC file
C file
 
Circular queue
Circular queueCircular queue
Circular queue
 
Stack using Array
Stack using ArrayStack using Array
Stack using Array
 
Dti2143 chap 4 control statement part 2
Dti2143 chap 4 control statement part 2Dti2143 chap 4 control statement part 2
Dti2143 chap 4 control statement part 2
 
C programming BY Mazedur
C programming BY MazedurC programming BY Mazedur
C programming BY Mazedur
 
Alu all shift register
Alu all shift registerAlu all shift register
Alu all shift register
 
ภาษาซี
ภาษาซีภาษาซี
ภาษาซี
 
Simple C programs
Simple C programsSimple C programs
Simple C programs
 
Intro to c programming
Intro to c programmingIntro to c programming
Intro to c programming
 
Tu1
Tu1Tu1
Tu1
 
Computer programing w
Computer programing wComputer programing w
Computer programing w
 
Bcsl 033 data and file structures lab s1-1
Bcsl 033 data and file structures lab s1-1Bcsl 033 data and file structures lab s1-1
Bcsl 033 data and file structures lab s1-1
 
Avl tree
Avl treeAvl tree
Avl tree
 
C program to implement linked list using array abstract data type
C program to implement linked list using array abstract data typeC program to implement linked list using array abstract data type
C program to implement linked list using array abstract data type
 
programa suma y multiplica
programa suma y multiplicaprograma suma y multiplica
programa suma y multiplica
 
week-6x
week-6xweek-6x
week-6x
 
C Programming Language Part 8
C Programming Language Part 8C Programming Language Part 8
C Programming Language Part 8
 
C programming
C programmingC programming
C programming
 

Semelhante a 2 3. standard io

Semelhante a 2 3. standard io (20)

input
inputinput
input
 
Unit2 C
Unit2 C Unit2 C
Unit2 C
 
Unit2 C
Unit2 CUnit2 C
Unit2 C
 
Introduction to Basic C programming 02
Introduction to Basic C programming 02Introduction to Basic C programming 02
Introduction to Basic C programming 02
 
06 1 조건문
06 1 조건문06 1 조건문
06 1 조건문
 
C lab manaual
C lab manaualC lab manaual
C lab manaual
 
Najmul
Najmul  Najmul
Najmul
 
Input output functions
Input output functionsInput output functions
Input output functions
 
C tutoria input outputl
C tutoria input outputlC tutoria input outputl
C tutoria input outputl
 
C- Programming Assignment 4 solution
C- Programming Assignment 4 solutionC- Programming Assignment 4 solution
C- Programming Assignment 4 solution
 
C basics
C basicsC basics
C basics
 
Input output statement in C
Input output statement in CInput output statement in C
Input output statement in C
 
LET US C (5th EDITION) CHAPTER 2 ANSWERS
LET US C (5th EDITION) CHAPTER 2 ANSWERSLET US C (5th EDITION) CHAPTER 2 ANSWERS
LET US C (5th EDITION) CHAPTER 2 ANSWERS
 
C Programming Exam problems & Solution by sazzad hossain
C Programming Exam problems & Solution by sazzad hossainC Programming Exam problems & Solution by sazzad hossain
C Programming Exam problems & Solution by sazzad hossain
 
C file
C fileC file
C file
 
The solution manual of programming in ansi by Robin
The solution manual of programming in ansi by RobinThe solution manual of programming in ansi by Robin
The solution manual of programming in ansi by Robin
 
C programms
C programmsC programms
C programms
 
Input And Output
 Input And Output Input And Output
Input And Output
 
Practical File of C Language
Practical File of C LanguagePractical File of C Language
Practical File of C Language
 
Unit 5 Foc
Unit 5 FocUnit 5 Foc
Unit 5 Foc
 

Mais de 웅식 전

15 3. modulization
15 3. modulization15 3. modulization
15 3. modulization웅식 전
 
15 2. arguement passing to main
15 2. arguement passing to main15 2. arguement passing to main
15 2. arguement passing to main웅식 전
 
12 2. dynamic allocation
12 2. dynamic allocation12 2. dynamic allocation
12 2. dynamic allocation웅식 전
 
12 1. multi-dimensional array
12 1. multi-dimensional array12 1. multi-dimensional array
12 1. multi-dimensional array웅식 전
 
11. array & pointer
11. array & pointer11. array & pointer
11. array & pointer웅식 전
 
10. pointer & function
10. pointer & function10. pointer & function
10. pointer & function웅식 전
 
7. variable scope rule,-storage_class
7. variable scope rule,-storage_class7. variable scope rule,-storage_class
7. variable scope rule,-storage_class웅식 전
 
5 2. string processing
5 2. string processing5 2. string processing
5 2. string processing웅식 전
 
5 1. character processing
5 1. character processing5 1. character processing
5 1. character processing웅식 전
 
15 1. enumeration, typedef
15 1. enumeration, typedef15 1. enumeration, typedef
15 1. enumeration, typedef웅식 전
 
3 2. if statement
3 2. if statement3 2. if statement
3 2. if statement웅식 전
 
3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib웅식 전
 
2 3. standard io
2 3. standard io2 3. standard io
2 3. standard io웅식 전
 
2 2. operators
2 2. operators2 2. operators
2 2. operators웅식 전
 
2 1. variables & data types
2 1. variables & data types2 1. variables & data types
2 1. variables & data types웅식 전
 

Mais de 웅식 전 (20)

15 3. modulization
15 3. modulization15 3. modulization
15 3. modulization
 
15 2. arguement passing to main
15 2. arguement passing to main15 2. arguement passing to main
15 2. arguement passing to main
 
14. fiile io
14. fiile io14. fiile io
14. fiile io
 
13. structure
13. structure13. structure
13. structure
 
12 2. dynamic allocation
12 2. dynamic allocation12 2. dynamic allocation
12 2. dynamic allocation
 
12 1. multi-dimensional array
12 1. multi-dimensional array12 1. multi-dimensional array
12 1. multi-dimensional array
 
11. array & pointer
11. array & pointer11. array & pointer
11. array & pointer
 
10. pointer & function
10. pointer & function10. pointer & function
10. pointer & function
 
9. pointer
9. pointer9. pointer
9. pointer
 
7. variable scope rule,-storage_class
7. variable scope rule,-storage_class7. variable scope rule,-storage_class
7. variable scope rule,-storage_class
 
6. function
6. function6. function
6. function
 
5 2. string processing
5 2. string processing5 2. string processing
5 2. string processing
 
5 1. character processing
5 1. character processing5 1. character processing
5 1. character processing
 
15 1. enumeration, typedef
15 1. enumeration, typedef15 1. enumeration, typedef
15 1. enumeration, typedef
 
4. loop
4. loop4. loop
4. loop
 
3 2. if statement
3 2. if statement3 2. if statement
3 2. if statement
 
3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib
 
2 3. standard io
2 3. standard io2 3. standard io
2 3. standard io
 
2 2. operators
2 2. operators2 2. operators
2 2. operators
 
2 1. variables & data types
2 1. variables & data types2 1. variables & data types
2 1. variables & data types
 

2 3. standard io

  • 2. Use of printf() and scanf()  printf() is abbreviation of Print formatted  Print out something on the monitor printf(“format string..”, argument list); [Ex] #include <stdio.h> main() { int n ; n = 25 * 10; printf(“%d”, n); /* print 250. */ } [Ex] printf(“%c%c%c”, ’a’, ’b’, ’c’ ); /* print “abc”. */ printf(“%s”, “def” ); /* print “def”. */ 2
  • 3. Use of printf() and scanf()  Conversion characters (format string) %c character printf(“%c”, ’a’); %d decimal integer printf(“%d”, 100); %x Hexadecimal integer printf(“%x”, 100); %o Octal integer printf(“%o”, 100); %u Unsigned decimal printf(“%u”, 100); %e floating point number in printf(“%e”, 1.234); scientific notation %f floating point number printf(“%f”, 1.234); %g e-format or f-format printf(“%g”, 1.234); %s string printf(“%s”, ”C-book”); 3
  • 4. Use of printf() and scanf()  The Use of printf() : integer %d printf(“ %md”, a ); /*print a left-justified in m width */ printf(“ %-md”, a); /*print a right-justified in m width */ [Ex] a = 12 ; printf(“%5d%-5d%dn” ,a, a, a+11); 12^^^^^^1223 – %5d : 12^^^ – %-5d : ^^^12 – %d : 23 4
  • 5. Use of printf() and scanf()  The Use of printf() : float %f printf(“ %m.pf”, a ); /*print a left-justified in m width with p numbers after decimal point*/ printf(“ %-m.pf”, a); /*print a right-justified in m width with p numbers after decimal point*/ [Ex] 83.1260^^83.126083.126000^^^83.13 a = 83.126 ; printf(“%8.4f%-8.4f%f%-7.2fn”, a, a, a, a ); – %8.4f : 83.1260^ – %-8.4f : ^83.1260 – %f : 83.126000 – %-7.2f : ^^^83.13 5
  • 6. Use of printf() and scanf()  scanf() is abbreviation of Scanf formatted  Reads data from standard input scanf(“format string..”, argument list); %c character scanf(“%c”, &a); %d decimal integer scanf(“%d”, &a); %f floating point number scanf(“%f”, &a); %lf floating point number scanf(“%lf”, &a); %Lf floating point number scanf(“%Lf”, &a); %s string scanf(“%s”, &a); 6
  • 7. Use of printf() and scanf() [Ex] #include <stdio.h> main() { int n ; printf(“Enter number : “); scanf(“%d”, &n); printf(“You entered : %d”, n); return 0; } Enter number : 10 You entered : 10 7