on
Italy
- Get link
- X
- Other Apps
printf() and scanf() are the most commonly used to display out and take input respectively. Let us consider an example:
#include //This is needed to run printf() function.
int main()
{
printf("C Programming"); //displays the content inside quotation
return 0;
}
C ProgrammingExplanation of How this program works
printf() is a library function to display output which only works if #includeis included at the beginning.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 doesn't find stdio.h header file, compiler shows error.return 0; indicates the end of program. You can ignore this statement but, it is good programming practice to use return 0;.
#include
int main()
{
int c=5;
printf("Number=%d",c);
return 0;
}
OutputNumber=5Inside quotation of
printf() there, is a conversion format string "%d" (for integer). If this conversion format string matches with remaining argument,i.e, c in this case, value of c is displayed.
#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
scanf() function is used to take input from user. In this program, the user is asked a input and value is stored in variable c. Note the '&' sign before c. &c denotes the address of c and value is stored in that address.
#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;
}
OutputEnter value: 23.45 Value=23.450000Conversion format string
"%f" is used for floats to take input and to display floating value of a variable.#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."%c", that character is displayed.
#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;
}
OutputEnter character: g 103
#include
int main(){
int var1=69;
printf("Character of ASCII value 69: %c",var1);
return 0;
}
OutputCharacter of ASCII value 69: EThe 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.
#include
int main(){
printf("Case 1:%6d\n",9876);
/* Prints the number right justified within 6 columns */
printf("Case 2:%3d\n",9876);
/* Prints the number to be right justified to 3 columns but, there are 4 digits so number is not right justified */
printf("Case 3:%.2f\n",987.6543);
/* Prints the number rounded to two decimal places */
printf("Case 4:%.f\n",987.6543);
/* Prints the number rounded to 0 decimal place, i.e, rounded to integer */
printf("Case 5:%e\n",987.6543);
/* Prints the number in exponential notation(scientific notation) */
return 0;
}
Case 1: 9876 Case 2:9876 Case 3:987.65 Case 4:988 Case 5:9.876543e+002
#include
int main(){
int a,b;
float c,d;
printf("Enter two intgers: ");
/*Two integers can be taken from user at once as below*/
scanf("%d%d",&a,&b);
printf("Enter intger and floating point numbers: ");
/*Integer and floating point number can be taken at once from user as below*/
scanf("%d%f",&a,&c);
return 0;
}
Comments
Post a Comment