Input & Output on Console

C Programming Language / Overview of C Language

1839

Program:

#include <stdio.h>
int main() {
	int n = 10;
	float m = 1.245;
	char c = 'A';
	printf("%d \n",n);
	printf("%f \n",m);
	printf("%.2f \n",m);
	printf("%.3f \n",m);
	printf("%c \n",c);
	return 0;
}

Output:

10
1.245000
1.25
1.245
A
Press any key to continue . . .

Explanation:

You can see on %f that wrote floating data with 6 decimal digits. You can specify the number of decimal digit by passing numeric after dot (.), for instance, %.2f and %.3f.


The given code is a C program that demonstrates how to print values of variables of different data types to the console using the printf() function with different format specifiers.

Here's how it works:

  • The #include <stdio.h> line includes the standard input/output library in the program.
  • The main() function is the entry point of the program.
  • Three variables n, m, and c are declared and initialized with values of integer, float, and character data types respectively.
  • The printf() function is used to print the values of the variables to the console using different format specifiers:
    • %d format specifier is used to print the integer value of n.
    • %f format specifier is used to print the floating-point value of m.
    • %.2f format specifier is used to print the floating-point value of m with two decimal places.
    • %.3f format specifier is used to print the floating-point value of m with three decimal places.
    • %c format specifier is used to print the character value of c.
  • The \n character is used to insert a newline after each output is printed.
  • Finally, the return 0; statement terminates the program and returns 0 to the operating system indicating that the program executed successfully.

This Particular section is dedicated to Programs only. If you want learn more about C Programming Language. Then you can visit below links to get more depth on this subject.