31
CONSOLE INPUT OUTPUT

C language Console input output

Embed Size (px)

Citation preview

Page 1: C language Console input output

CONSOLE INPUT OUTPUT

Page 2: C language Console input output

LIBRARIES FUNCTIONS IN C

Libraries functions in C

Console Input Output function

Formatted Console Input

Output function

Unformatted Console Input

Output function

File Input Output function

Page 3: C language Console input output

CONSOLE INPUT OUTPUT FUNCTION

Functions to receive input from keyboard and write output to VDU

We know that – printf(“format string”,list of variables); The format string can contain :- 1. Characters that are simply printed as they are2. Conversion specifications that begin with a % sign3. Escape sequences that begin with a \ sign

Page 4: C language Console input output

CONSOLE INPUT OUTPUT FUNCTION

Page 5: C language Console input output

PROGRAM-1 – formatted functions

main( ){int avg = 346 ;float per = 69.2 ;printf ( "Average = %d\nPercentage = %f", avg, per );}

The output of the program would be...Average = 346Percentage = 69.200000

Page 6: C language Console input output

EXPLANATION OF PROGRAM-1

printf( ) function interpret the contents of the format string. For this it examines the format string from left to right.

So long as it doesn’t come across either a % or a \ it continues to dump the characters that it encounters, on to the screen.

In this example Average = is dumped on the screen. The moment it comes across a conversion specification in the

format string it picks up the first variable in the list of variables and prints its value

In this example, the moment %d is met the variable avg is picked up and its value is printed.

Page 7: C language Console input output

FORMAT SPECIFIERS

Page 8: C language Console input output

PROGRAM-2

#include <stdio.h> int main(){      printf("Various format for integer printing\n");      printf("-------------------------------------\n");      printf("%d\n",  455);      printf("%i\n",  455);  //i same as d in printf()      printf("%d\n",  +455);      printf("%d\n",  -455);      printf("%hd\n",  32000);      printf("%ld\n",  2000000000L);      printf("%o\n",  455);      printf("%u\n",  455);      printf("%u\n",  -455);      printf("%x\n",  455);      printf("%X\n",  455);     return 0;}

Page 9: C language Console input output

PROGRAM-3

#include <stdio.h>int main(){     printf("   Printing integers right-justified.\n");     printf("Compare the output with the source code\n");     printf("---------------------------------------\n\n");     printf("%4d\n",  1);     printf("%4d\n",  12);     printf("%4d\n",  123);     printf("%4d\n",  1234);     printf("%4d\n\n",  12345);     printf("%4d\n",   -1);     printf("%4d\n",   -12);     printf("%4d\n",   -123);     printf("%4d\n",   -1234);     printf("%4d\n",   -12345);     return 0;}

Page 10: C language Console input output

PROGRAM-4

#include <stdio.h>int main(){     printf("Printing numbers with and without the + flag.\n");     printf("   Compare the output with the source code\n");     printf("---------------------------------------------\n\n");     printf("%d\n%d\n", 786, -786);     printf("%+d\n%+d\n", 786, -786);     return 0;} 

Page 11: C language Console input output

Program -5 (Format specifier %d)

#include<stdio.h>int main(){int weight=63;printf(“weight is %d kg\n”,weight);return 0;} OUTPUTWeight is 63 kg

0 1 2 3 4 5 6 7 8 9 0 1 2 3 4W e i g h t i s 6 3 k g

Page 12: C language Console input output

Program -6 (Format specifier %d)

#include<stdio.h>int main(){int weight=63;printf(“weight is %2d kg\n”,weight); return 0;} OUTPUT 012345678901234Weight is 63 kg

0 1 2 3 4 5 6 7 8 9 0 1 2 3 4

w e i g h t i s 6 3 k g

Columns number

Page 13: C language Console input output

EXPLANATION OF PROGRAM-6

Here %2d means that can print 63 in the space of 2 columns as shown in output figure. 63 takes 2 columns shown by green color in figure output.

Column numbers are not displayed on console(output) screen.

printf(“weight is %2d kg”) this means we provide 1 space after weight and 1 space after is and 1 space after %2d. These spaces print as it is in screen. If we give 2 spaces in place of 1, then it will print 2 spaces.

Page 14: C language Console input output

Program -7 (Format specifier %d)

#include<stdio.h>int main(){int weight=63;printf(“weight is %4d kg\n”,weight); return 0;}OUTPUT

0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6w e i g h t i s 6 3 k g

Page 15: C language Console input output

EXPLANATION OF PROGRAM-7

Here %4d means that can print 63 in the space of 4 columns as shown in output figure. 63 takes 4 columns shown by green color in figure output.

Column numbers are not displayed on console(output) screen.printf(“weight is %4d kg”) this means we provide 1 space after

weight and 1 space after is and 1 space after %4d. These spaces print as it is in screen. If we give 2 spaces in place of 1, then it will print 2 spaces.

Page 16: C language Console input output

Program -8 (Format specifier %d)

#include<stdio.h>int main(){int weight=63;printf(“weight is %6d kg\n”,weight); return 0;}OUTPUT

0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8w e i g h t i s 6 3 k g

Page 17: C language Console input output

Program -9 (Format specifier %f)

#include<stdio.h>int main( ){printf ( "\n%f %f %f", 5.0, 13.5, 133.9 ) ;printf ( "\n%f %f %f", 305.0, 1200.9, 3005.3 ) ;}

And here is the output...5.000000 13.500000 133.900000305.000000 1200.900000 3005.300000

Page 18: C language Console input output

Program -10 (Format specifier %f)

main( ){printf ( "\n%10.1f %10.1f %10.1f", 5.0, 13.5, 133.9 ) ;printf ( "\n%10.1f %10.1f %10.1f", 305.0, 1200.9, 3005.3 );}

OUTPUT--

0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 05 . 0 1 3 . 5 1 3 3 . 9

3 0 5 . 0 1 2 0 0 . 9 3 0 0 5 . 3

Page 19: C language Console input output

EXPLANATION OF PROGRAM-10

%10.1f (referring to the first argument: radius) means make it 10 characters long (i.e. pad with spaces), and print it as a float with one decimal place.

One decimal is also included.If there is 10.2f in place of 10.1f ,then %10.2 (referring to the

second argument: area) means make it 10 character long and print with two decimal places.

Page 20: C language Console input output

Program -11 (Format specifier %s)

main( ){char firstname1[ ] = "Sandy" ;char surname1[ ] = "Malya" ;char firstname2[ ] = "AjayKumar" ;char surname2[ ] = "Gurubaxani" ;printf ( "\n%20s%20s", firstname1, surname1 ) ;printf ( "\n%20s%20s", firstname2, surname2 ) ;}

0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9S A n d y M a l y a

A j a y K Uma r Gu r u b a x a n i

Page 21: C language Console input output

EXPLANATION OF program-11

The printf(“%s\n”, “Hello, world!”); statement prints the string (nothing special happens.)

The printf(“%20s\n”, “Hello, world!”); statement prints the string, but print 20 characters. If the string is smaller the “empty” positions will be filled with “whitespace.”

Similarly, The printf(“%20.10s\n”, “Hello, world!”); statement prints the string, but print 20 characters.If the string is smaller the “empty” positions will be filled with “whitespace.” But it will only print a maximum of 10 characters, thus only part of new string (old string plus the whitespace positions) is printed.

Page 22: C language Console input output

PROGRAM-12

#include <stdio.h > int main(){     int    c  = 1427;     float  p  =  1427.0;     printf("o, x, X, and any floating-point specifiers\n");     printf("Compare the output with the source code\n");     printf("-----------------------------------------\n\n");     printf("%#o\n", c);     printf("%#x\n", c);     printf("%#X\n", c);     printf("\n%#g\n", p);     printf("%#G\n", p);     return 0;}

Page 23: C language Console input output

PROGRAM-13

#include <stdio.h>int main(){       printf("Printing with the 0 (zero) flag fills in leading zeros\n");       printf("       Compare the output with the source code\n");       printf("-------------------------------------------------------\n\n");       printf("%+09d\n", 762);       printf("%09d", 762);       printf("\n");       return 0;}

Page 24: C language Console input output

UNFORMATTED CONSOLE I/O FUNCTIONS

gets()putch()putchar()puts()getchar()getch()getche()

Page 25: C language Console input output

SYNTAX for gets() and putch

Syntax for gets()  in C :gets(variable_name);gets() accepts any line  of string including spaces from the

standard Input device (keyboard). gets() stops reading character from keyboard only when the enter key is pressed.

Syntax for putch in C :putch(variable_name);putch displays any alphanumeric characters to the standard

output device. It displays only one character at a time.

Page 26: C language Console input output

SYNTAX FOR putchar, puts & getchar()

Syntax for putchar in C :putchar(variable_name);putchar displays one character at a time to the Monitor.

Syntax for puts in C :puts(variable_name);puts displays a single / paragraph of text to the standard output device.

Syntax for getchar() in C :variable_name = getchar();getchar() accepts one character type data from the keyboard.

Page 27: C language Console input output

SYNTAX for getch() and getche()

Syntax for getch () in C :variable_name = getch();getch() accepts only single character from keyboard. The

character entered through getch() is not displayed in the screen (monitor).

Syntax for getche() in C :variable_name = getche();Like getch(), getche() also accepts only single character, but

unlike getch(), getche() displays the entered character in the screen.

Page 28: C language Console input output

PROGRAM-1

main( ){char footballer[40] ;puts ( "Enter name" ) ;gets ( footballer ) ; /* sends base address of array */puts ( "Happy footballing!" ) ;puts ( footballer ) ;}OUTPUT-Enter nameJonty RhodesHappy footballing!Jonty Rhodes

Page 29: C language Console input output

IMPORTANT POINTS

* There is no keyword available in C for doing input/output.All I/O in C is done using standard library functions.There are several functions available for performing consoleinput/output.The formatted console I/O functions can force the user to

receive the input in a fixed format and display the output in a fixed format.

There are several format specifiers and escape sequences available to format input and output.

Unformatted console I/O functions work faster since they do not have the overheads of formatting the input or output.

Page 30: C language Console input output

PROGRAM-2

#include<stdio.h>#include<conio.h>void main(){char a[20];gets(a);puts(a);getch();}

Page 31: C language Console input output

EXPLANATION OF PROGRAM -2

include<stdio.h> header file is included because, the C in-built statements gets and puts we used in this program comes under stdio.h header files.

#include<conio.h> is used because the C in-built function getch()comes under conio.h header files.

main () function is the place where C program execution begins.Array a[] of type char size 20 is declared.gets is used to receive user input to the array. gets stops receiving

user input only when the Newline character (Enter Key) is interrupted.

puts is used to display them back in the monitor