1.Input output using printf() and scanf() function.
/* C Language basic program that shows working of I/O printf() and scanf() Functions */
#include <stdio.h>
int main()
{
char name[30];
char ch;
printf("\nEnter Your Name");
scanf("%s", &name);
printf("Your Name is= %s ", name);
printf("Enter any character ");
scanf("%c", &ch);
printf("Entered character is =%c", ch);
}
Output:
Enter Your Name
James
Your name is=James
Enter any character
C
Entered character is =C
Program Explanation:
In the program given above The two character variables are declared i.e. name and ch.
char name[30];--> name is declared as character array of 30 character which can store 30 alphabates or character.
Memory allocated by name is 30 bytes.
char ch; here ch is declared as character which can store single character and occupy single byte of memory.
next we have the statement.
printf("\nEnter Your Name");
scanf("%s", &name);
The printf() shows message "Enter Your name" and program waits until name is not entered.
We have Enterd the name : "James".
The Entered name get scaned by scanf("%s", &name); and stores it in name variable.
next the printf("Your Name is= %s ", name); shows the output "Your name is =James".
%s in printf() is the format specifiers used to place string value.
The format specifier %s is subtituted by the value of name variable i.e "James"
similarly We have Entered character 'C' and it has stored in character variable ch , which is processed by
printf("Enter any character ");
scanf("%c", &ch);
and finaly output shown is "Entered character is=C"