1 / 26

C Programming

C Programming. Managing Input and Output Operations. Introduction. One of the essential operations performed in a  C language programs is to provide input values to the program and output the data produced by the program to a standard output device.

Download Presentation

C Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C Programming Managing Input and Output Operations

  2. Introduction One of the essential operations performed in a C language programs is to provide input values to the program and output the data produced by the program to a standard output device. We can assign values to variable through assignment statements such as x = 5; a = 0 ; and so on. Another method is to use the Input then scanfwhich can be used to read data from a key board. For outputting results we have used extensively the functionprintfwhich sends results out to a terminal.

  3. Introduction (II) There exists several functions in ‘C’ language that can carry out input output operations. These functions are collectively known as standard Input/Output Library. Each program that uses standard input /output function must contain the statement #include <stdio.h> at the beginning.

  4. Single character input & output The basic operation done in input output is to read a characters from the standard input device such as the keyboard and to output or writing it to the output unit usually the screen. The getchar function can be used to read a character from the standard input device. The scanf can also be used to achieve the function. The getchar has the following form variable_name= getchar(); Variable name is a valid ‘C’ variable, that has been declared already and that possess the type char.

  5. Example program - 1 # include < stdio.h > // assigns stdio-h header file to your program main ( ) // Indicates the starting point of the program. { char C; // variable declaration printf(“Type one character:”) ; // message to user C = getchar () ; // get a character from key board and Stores it // in variable C. printf (”The character you typed is = %c”, C) ; // output Statement // which displays value of C on Standard screen. }

  6. Single character input & output Theputcharfunction which in analogusto getchar function can be used for writing characters one at a time to the output terminal. The general form is putchar(variable_name); Where variable is a valid C type variable that has already been declared Ex:- putchar( ); Displays the value stored in variable C to the standard screen. Program shows the use of getchar function in an interactive environment.

  7. Example program - 2 #include < stdio.h > // Inserts stdio.h header file into the Pgm main ( ) // Beginning of main function. { char in; // character declaration of variable in. printf(” please enter one character”); // message to user in = getchar ( ); // assign the keyboard input value to in. putchar(in); // out put ‘in’ value to standard screen. }

  8. String input and output Thegets function relieves the string from standard input device while put S outputs the string to the standard output device. A strong is an array or set of characters. The function gets accepts the name of the string as a parameter, and fills the string with characters that are input from the keyboard till newline character is encountered. (That is  till we press the enter key). All the end function gets appends a null terminator as must be done to any string and returns.

  9. String input and output (II) The puts function displays the contents stored in its parameter on the standard screen. The standard form of the gets function is gets (str) Herestris a string variable. The standard form for the puts character is puts (str) Wherestris a string variable.

  10. Example program -3 (Involving both gets and puts) # include <stdio.h > main ( ) { char s [80]; printf(“Type a string less than 80 characters:”); gets (s); printf(“The string types is:”); puts(s); }

  11. Formatted Input The formatted input refers to input data that has been arranged in a particular format. Input values are generally taken by using the scanf function. The scanffunction has the general form. scanf(“control string”, arg1, arg2, arg3, …………., argn); The format field is specified by the control string and the arguments arg1, arg2, ……………., argnspecifies the addrss of location where address is to be stored. The control string specifies the field format which includes format specifications andoptional number specifying field width and the conversion character % and also blanks, tabsand newlines.

  12. Formatted Input(II) The formatted input refers to input data that has been arranged in a particular format. Input values are generally taken by using the scanf function. The scanffunction has the general form. scanf(“control string”, arg1, arg2, arg3, …………., argn); The format field is specified by the control string and the arguments arg1, arg2, ……………., argnspecifies the addrss of location where address is to be stored. The control string specifies the field format which includes format specifications andoptional number specifying field width and the conversion character % and also blanks, tabsand newlines.

  13. Formatted Input (III) The Blanks tabs and newlines are ignored by compiler. The conversion character % is followed by the type of data that is to be assigned to variable of the assignment. The field width specifier is optional. The general format for reading a integer number is % w d Here percent sign (%) denotes that a specifier for conversion follows and wis an integer number which specifies the width of the field of the number that is being read. The data type character d indicates that the number should be read in integer mode.

  14. Example scanf(“%3d %4d”, &sum1, &sum2); If the values input are 175 and 1342 here value 175 is assigned to sum1 and 1342 to sum2. Suppose the input data was follows 1342 and 175. The number 134 will be assigned to sum1 and sum2 has the value 2 because of %3d the number 1342 will be cut to 134 and the remaining part is assigned to second variable sum2. If floating point numbers are assigned then the decimal or fractional part is skipped by the computer. To read the long integer data type we can use conversion specifier% ld & % hd for short integer.

  15. Input specifications for real number Field specifications are not to be use while representing a real number therefore real numbers are specified in a straight forward manner using %f specifier. The general format of specifying a real number input is scanf(“%f”, &variable);

  16. Example scanf(“%f %f %f”, &a, &b, &c); With the input data 321.76, 4.321, 678. The values 321.76is assigned to a , 4.321 to b & 678 to c. If the number input is a double data type then the format specifier should be % lf instead of %f.

  17. Commonly used scanfFormate Codes

  18. Example program - 4(exam4.c) Here is a simple program that makes use of the printf function. #include <stdio.h>#include <math.h>main() / * print several f l o a t i n g - p o i n t numbers * / {float i= 2.0, j = 3.0;printf( " %f %f %f %f", i, j, i + j , s q r t ( i + j ) ); } Executing the program produces the following output:2.000000 3.000000 5.000000 2.236068

  19. Example program – 5 (exam5.c) The following program generates the same floating-point output in two different forms. #include <stdio.h>main() / * display f l o a t i n g - p o i n t output 2 d i f f e r e n t ways * /{ double x = 5000.0, y = 0.0025;p r i n t f ("%f %f %f %f \n\n" , x, y, x*y, x / y );p r i n t f ("%e %e %e %e", x, y, x*y, x / y );} When the program is executed, the following output is generated.5000.000000 0.002500 12.500000 2000000.000000 5.000000e+03 2.500000e-03 1.250000e+01 2.000000e+06

  20. Example program – 6 (exam6.c) The following C program illustrates the use of the minimum field width feature. #include <stdio.h>main ( ) / * minimum field width specifications * /. { int 1 = 12345; float x = 345.678; printf ("%3d %5d %8d \n\n",1, l, 1);printf("%3f %10f %13f \n\n”, x , x , x ) ;printf("%3e %13e %16e", x , x , x ) ; } When the program is executed, the following output is generated.12345 12345 12345345.678000 345.678000 345.678000 3.456780e+02 3.456780e+023.456780e+02

  21. Example program – 7 (exam7.c) Here is a program that illustrates the use of the precision feature with floating-point numbers. #include <stdio.h>main() / * display a floating-point number with several different precisions * /{ float x = 123.456;printf("%7f %7.3f %7.lf\n\n", x, x, x ) ;printf ("%12e %12.5 e %12.3e", x, x, x ) ; }

  22. Example program – 7 (exam7.c) When this program is executed, the following output is generated.123.456000 123.456 123.5 1.234560e+02 1.23456e+02 1.235e+02

  23. You guise will be read the following topics Formatted Output • Output of Integer Numbers (p-95) • Output of Real Numbers (p-96) • Printing of a Single Character (p-99) • Printing of Strings (p-99)

  24. Home work • Review Questions (4.1 to 4.10)

More Related