BASIC INPUT - OUTPUT
main():Consider a very simple program, given below.
/* EX-1: SAMPLE PROGRAM TO PRINT A TEXT */ |
#include main() { printf("NATIONAL INSTITUTE"); } |
OUTPUT |
OUPTPUT : NATIONAL INSTITUTE |
Note:
1. In C everything is written in lowercase letters. However uppercase letters used for symbolic names representing constants. Ex: # define MAX 80
2. The line beginning with /* and ending with */ are known as comment line. It is used in a program to enhance its readability and understanding. Comment lines are not executable statements and therefore anything between /* and */ is ignored by the compiler.
3. #nclude
4. The main() is a special function used by the C system to tell the computer where the program starts. Every program must have exactly one main function. The empty pair of parentheses immediately following main() indicates that the function main has no arguments(or parameters). The opening brace ‘{‘ in the second line marks the beginning of the function main() and the close brace ‘}’ in the last line indicates the end of the function. Sometimes it may be an end of the program. All the statements between these two braces form the body of the function.
printf():
The statement printf() is used to print the text that contains within two double quotes.
syn: 1. printf(“text”);
2. printf(“access_specifier”,variable);
/* EX-2: AN EXAMPLE ILLUSTRATING INTEGER AND FLOATING POINT OUTPUT */ |
#include main() { int ivar1,ivar2,ivar3; /* Declaration statement */ float fvar; clrscr(); /* Clear the screen */ ivar1=4; /* Assignment statement */ ivar2=6; fvar=18.235235; ivar3=ivar1+ivar2; printf("\n Sum of %d and %d is %d", ivar1,ivar2,ivar3); printf("\n\t Assigned float value = %f", fvar); getch(); /* Wait statement */ } |
OUTPUT |
Sum of 4 and 6 is 10 Assigned float value = 18.235235 |
Note: In above program the first printf statement having three format specifier and three parameters. The order of format specifier should be same to the order of parameter in a printf statement.
scanf(): The scanf function reads the values of variables from the standard input device(keyboard) and stores them in variables. syn: scanf(“access_specifier”, &variable);
/* EX-3: ILLUSTRATING THE SCANF() WITH NUMBERS */ |
#include main() { float years,secs; clrscr(); printf("\n\t Enter your age: "); scanf("%f",&years); secs=years*365*24*60*60; printf("\n\n\t CONGRATULATION: \n\t\t You have lived for %f seconds",secs); getch(); } |
OUTPUT |
CONGRATULATION:
You have lived for 662256000.0 seconds |
|
main()
{ char x; clrscr(); printf("\n Enter a character: "); scanf("%c",&x); printf("\n Given character is %c",x); getch(); } |
Input : Enter a character: B |
Given character is B |
/* EX-5: ILLUSTRATING SCANF() WITH STRINGS */ |
#include main() { char str1[20],str2[20]; clrscr(); printf("\n\tEnter any two strings:\n"); scanf("%s",str1); scanf("%s",&str2); printf("\tGiven two Strings are: %s and %s", str1,str2); getch(); } |
INPUT : Enter any two strings:
National Institute |
OUTPUT : Given two Strings are: National and Institute |
NOTE : 1) In the declaration statement char str1[20], str2[20], str1 and str2 can hold the width of 20 characters, the string wouldn’t accept the blank space.
2) %s is a format specifier for string [Array of character].
3) The two statements
scanf(“%s”, str1);
scanf(“%s”, str2);
can be replaced by a single statement
scanf(“%s %s”, str1, str2) with exactly same result.
getchar() and putchar():
As the indicates getchar reads a character from the standard input device, while putchar writes a character to the standard
output device.
/* EX-6: EXAMPLE INVOLVING BOTH GETCHAR( ) AND PUTCHAR( ) */ |
#include { char c; clrscr(); printf("\nInput one Character: "); c=getchar(); printf("\nThe character you have typed : "); putchar(c); getch(); } |
INPUT : Input one Character: s |
OUTPUT : The character you have typed : s |
gets() and puts():
The function gets receives the string from the standard input device, while puts outputs the string to the standard
output devices.
/* EX-7: EXAMPLE INVOLVING BOTH GETS( ) AND PUTS( ) */ |
#include clrscr(); printf("\n Input a String (maximum of 80 character): "); gets(s); printf("\n\t The String you have typed: "); puts(s); getch(); } |
INPUT : Input a String (maximum of 80 character): Sri Baskaran |
OUTPUT : The String you have typed: Sri Baskaran |
Note: The function gets accepts more than one word including blank space.
Assignment Suppression Character: An assignment suppression character tells scanf() that the input should only be scanned and not assigned to any argument. The assignment suppression character is asterisk(*), and it must be specified immediately after the % sign in the format specifier. So no need to declare a variable for assignment suppression character.
/* EX-8: ILLUSTRATING SCANF() USING ASSIGNMENT SUPPRESSION */ |
#include main() { int date,month,year; clrscr(); printf("\n Input the date (dd.mm.yyyy) with any seperator(. / - *):\n\t\t\t"); scanf("%d %*[/-.*] %d %*[/-.*] %d", &date, &month, &year); printf("Date : %d\n",date); printf("Month: %d\n",month); printf("Year : %d\n",year); getch(); } |
Note: Here, there are 5 format specifiers, but only 3 arguments following the format string in a scanf() statement.
Input : Input the date (dd.mm.yyyy) with any seperator(. / - *): 01.01.2002 |
Output : Date : 1 Month : 1 Year : 2002 |