110 likes | 283 Views
INTERACTIVE OUTPUT FUNCTIONS printf-The computer (i.e. the program) sends its processed output to the standard output device – monitor screen. other standard commands : sprintf, putchar, puts. FORMATTED INPUT & OUTPUT. INTERACTIVE OUTPUT FUNCTIONS
E N D
INTERACTIVE OUTPUT FUNCTIONS printf-The computer (i.e. the program) sends its processed output to the standard output device – monitor screen. other standard commands:sprintf, putchar, puts.
FORMATTED INPUT & OUTPUT INTERACTIVE OUTPUT FUNCTIONS printf-The computer (i.e. the program) sends its processed output to the standard output device – monitor screen. other standard commands:sprintf, putchar, puts.
OUTPUT FUNCTION OF printf GENERAL SYNTAX printf (“FORMAT_CONTROL_STRING”, PRINT_LIST); EXAMPLE: printf (“The sum of %lf and %d is\n”, TAX, INTEREST); FORMAT_CONTROL_STRING– may vary from ordinary characters, escape sequence, format specifiers; with double quote/un-quote sign. PRINT_LIST – the actual parameter that printf function class, i.e. constants, variables, expressions or a function call. Format Specifier– the sign specification that converts an internal representation for a data type field to readable data value.
EXAMPLE:- double height = 147.2578 printf (“The value of %f printed in scientific notation is %e is.\n”, height, height); OUTPUT:- The value of 147.257800 printed in scientific notation is 1.472578e+02. Press any key when ready_ OUTPUT FUNCTION OF printf GENERAL SYNTAX printf (“FORMAT_CONTROL_STRING”, PRINT_LIST); EXAMPLE: printf (“The sum of %lf and %d is\n”, TAX, INTEREST); FORMAT_CONTROL_STRING– may vary from ordinary characters, escape sequence, format specifiers; with double quote/un-quote sign. PRINT_LIST – the actual parameter that printf function class, i.e. constants, variables, expressions or a function call. Format Specifier– the sign specification that converts an internal representation for a data type field to readable data value.
Recall format specifiers as the followings:- %d for integers %c for characters %f for floating-points values in conventional notation. (Variations ~ %lf and %Lf) %s for strings
INTERACTIVE INPUT FUNCTIONS scanf -A programmer enters data values for variables while the program is executing. Other standard command : getchar, gets
Syntax of the scanf: Scanf(“FormatControlString”,InputList); Example: scanf(“%lf”,&radius); scanf(“%c%c%c”,&letter_1,&letter_2,&letter_3);
void main(void) { char answer; printf(" Do you like to stay up until 2.00am in the morning? y or n\n"); scanf(“%c”, &answer); printf("%c\n",answer); if (answer == 'y') { printf(" You are hardworking\n"); } else if (answer == 'n') { printf(" You are very lazy\n"); } else putchar(answer); }
CHARACTER INPUT & OUTPUT SINGLE CHARACTER INPUT FROM KEYBOARD (getchar) Part of the standard I/O library that reads single character from standard input device. Initial declaration is a must. For example:- char ch; Illustration The Statement : ch = getchar ( ); The equivalent statement : scanf(“%c”, &ch);
SINGLE CHARACTER OUTPUT TO SCREEN (putchar) • Writes one character to the standard output unit. • General function reference is written as prototype • statement as the following:- • putchar (character variable) • char ch; • Illustration • The Statement :putchar (ch) • The equivalent statement :printf (“%c”, ch);
TUTORIAL (can be download at : http://fakhrulrozi.com/ 1. Write a C program that print the message “This is a C program.” on one line. 2. Write a C program that print the message that can ask user to input their first name and last name (scanf) produce output that display their full name example outputs for question 2: enter your first name : enter your last name : thank you, your full name is :