Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 5: Data Input and Output Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills 4800153-3.

Similar presentations


Presentation on theme: "Chapter 5: Data Input and Output Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills 4800153-3."— Presentation transcript:

1 Chapter 5: Data Input and Output Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills 4800153-3 1435/1436 Place photo here

2 The Objectives and the Outcomes The Objectives: To understand how to read data as an input To understand how to print data as an output Students should be able to understand different examples using input and output statement The Outcomes: Students would be able to understand the use of Input and Output statements Students would be able to understand the formatting statements using input and output statements

3 Overview Input that means to feed some data into program. This can be given in the form of file or from command line. C programming language provides a set of built-in functions to read given input and feed it to the program as per requirement. Output that means to display some data on screen, printer or in any file. C programming language provides a set of built-in functions to output the data on the computer screen as well as you can save that data in text or binary files. ANSI standard has defined many library functions for input and output in C language. Functions printf() and scanf() are the most commonly used functions to display and receive data.

4 Code:Output: #include //This is needed to run printf() function. int main() { Printf ("C Programming"); //displays the content inside quotation return 0;} C Programming Example of printf ( ) statement: Every program starts from main() function. printf() is a library function to display output which only works if #include is included at the beginning. Here, stdio.h is a header file (standard input output header file) and #include is command to paste the code from the header file when necessary. When compiler encounters printf() function and do not find stdio.h header file, compiler shows error. Code return 0; indicates the end of program. You can ignore this statement but, it is good programming practice to use return 0;.

5 The printf( ) function: printf() function is used to print the “character, string, float, integer, octal and hexadecimal values” onto the output screen. We use printf() function with %d format specifier to display the value of an integer variable. Similarly %c is used to display character, %f for float variable, %s for string variable, %lf for double and %x for hexadecimal variable. To generate a newline, we use “\n” in C printf() statement. C language is case sensitive. For example, printf() and scanf() are different from Printf() and Scanf(). All characters in printf() and scanf() functions must be in lower case.

6 The printf( ) function: Code:Output: #include int main() { char ch = ‘A’; char str[20] = “fresh2refresh.com”; float flt = 10.234; int no = 150; double dbl = 20.123456; printf(“Character is %c \n”, ch); printf(“String is %s \n”, str); printf(“Float value is %f \n”, flt); printf(“Integer value is %d\n”, no); printf(“Double value is %lf \n”, dbl); printf(“Octal value is %o \n”, no); printf(“Hexadecimal value is %x \n”, no); return 0; } Character is A String is fresh2refresh.com Float value is 10.234000 Integer value is 150 Double value is 20.123456 Octal value is 226 Hexadecimal value is 96

7 The scanf() function: scanf( ) function is used to read character, string, numeric data from keyboard Consider below example program where user enters a character. This value is assigned to the variable “ch” and then displayed. Then, user enters a string and this value is assigned to the variable ”str” and then displayed.

8 Code:Output: #include int main() { char ch; char str[100]; printf(“Enter any character \n”); scanf(“%c”, &ch); printf(“Entered character is %c \n”, ch); printf(“Enter any string ( upto 100 character ) \n”); scanf(“%s”, &str); printf(“Entered string is %s \n”, str); } Enter any character a Entered character is a Enter any string ( upto 100 character ) hai Entered string is hai The scanf() function:

9 I/O of integers in C: Code:Output: #include int main() { int c=5; printf("Number=%d", c); return 0; } Number=5 Inside quotation of printf() there is a conversion format string "%d" (for integer). If this conversion format string matches with remaining argument for example, in this case, value of c is displayed.

10 Code:Output: #include int main() { int c; printf ("Enter a number\n"); scanf ("%d", &c); printf ("Number=%d", c); return 0; } Enter a number 4 Number=4 I/O of integers in C:

11 I/O of floats in C: Code:Output: #include int main(){ float a; printf("Enter value: "); scanf("%f",&a); printf("Value=%f",a); //%f is used for floats instead of %d return 0; } Enter value: 23.45 Value=23.450000 Conversion format string "%f" is used for floats to take input and to display floating value of a variable.

12 I/O of characters and ASCII code: Code:Output: #include int main(){ char var1; printf("Enter character: "); scanf("%c",&var1); printf("You entered %c.",var1); return 0; } Enter character: g You entered g. Conversion format string "%c" is used in case of characters.

13 ASCII code: Code:Output: #include int main(){ char var1; printf("Enter character: "); scanf("%c",&var1); printf("You entered %c.\n",var1); /* \n prints the next line(performs work of enter). */ printf("ASCII value of %d",var1); return 0; } Enter character: g 103 When character is typed in the above program, the character itself is not recorded a numeric value (ASCII value) is stored and when we display that value by using "%c", that character is displayed.

14 Code:Output: #include int main(){ int var1=69; printf("Character of ASCII value 69: %c",var1); return 0;} Character of ASCII value 69: E ASCII code: The ASCII value of 'A' is 65, 'B' is 66 and so on to 'Z' is 90. Similarly ASCII value of 'a' is 97, 'b' is 98 and so on to 'z' is 122.

15 The gets() & puts() functions: Code:Output: #include int main() { charstr[100]; printf("Enter a value :"); gets(str); printf("\nYou entered: "); puts(str); return0; } Enter a value : This is test You entered: This is test

16 The getchar() & putchar() functions: Code:Output: #include int main() { int c; printf("Enter a value :"); c =getchar(); printf("\nYou entered: "); putchar( c ); return0; } Enter a value : this is test You entered: t


Download ppt "Chapter 5: Data Input and Output Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills 4800153-3."

Similar presentations


Ads by Google