What is a input and output


C Output

In C programming, printf() is one of the main output function. The function sends a formatted output to the screen. For example,

Example 1: C Output

  1. #include <stdio.h>
  2. int main()
  3. {
  4. // Displays the string inside quotations
  5. printf("C Programming");
  6. return 0;
  7. }
Output
C Programming
How does this program work?
  • All valid C programs must contain the main() function. The code execution begins from the start of the main() function.
  • The printf() is a library function to send formatted output to the screen. The function prints the string inside quotations.
  • To use printf() in our program, we need to include stdio.h header file using the #include <stdio.h> statement.
  • The return 0; statement inside the main() function is the "Exit status" of the program. It's optional.

Example 2: Integer Output

  1. #include <stdio.h>
  2. int main()
  3. {
  4. int testInteger = 5;
  5. printf("Number = %d", testInteger);
  6. return 0;
  7. }
Output
Number = 5
We use %d format specifier to print int types. Here, the %d inside the quotations will be replaced by the value of testInteger.

Example 3: float and double Output

  1. #include <stdio.h>
  2. int main()
  3. {
  4. float number1 = 13.5;
  5. double number2 = 12.4;
  6. printf("number1 = %f\n", number1);
  7. printf("number2 = %lf", number2);
  8. return 0;
  9. }
Output
number1 = 13.500000
number2 = 12.400000
To print float, we use %f format specifier. Similarly, we use %lf to print double values.

Example 4: Print Characters

  1. #include <stdio.h>
  2. int main()
  3. {
  4. char chr = 'a';
  5. printf("character = %c.", chr);
  6. return 0;
  7. }
Output
character = a

Post a Comment

0 Comments