Standard Input / Output

Standard output

putchar
int putchar(int ch)
  • Writes a character to standard output.
  • Returns EOF on error.
puts
int puts(char *line)
  • Writes a string to standard output.
  • Automatically adds a newline character.
  • Returns EOF on error.
printf
int printf(char *format, ...)
  • Returns the number of characters printed.
Examples
#include <stdio.h> 
...
printf("Hello, world!\n");
#include <stdio.h> 
...
char str_value[100];
int int_value;
float float_value;
double double_value;
...
printf("String: %s\n", str_value);
printf("Integer: %d\n", int_value);
printf("Float, 2 decimal places: %.2f\n", float_value);
printf("Double, 5 decimal places: %.5f\n",
double_value);

Standard input

getchar
int getchar()
  • Reads a character from standard input.
  • Returns EOF on end-of-file.
gets
char *gets(char *line, int maxline)
  • Reads the entire current line, including the newline character.
  • Returns at most maxline-1 characters plus an end-of-string character ('{content}').
  • Returns NULL on end-of-file or error.
scanf
int scanf(char *format, ...)
  • Returns the number of input items successfully parsed.
  • It can be called multiple times with different formatting options until the item is successfully parsed.
Examples
#include <stdio.h> 
...
char name[100];
int age;
float short_pi;
double long_pi;

scanf("%s", name); /* Read a string */
scanf("%d", &age); /* Read an integer */
scanf("%f", &short_pi); /* Read a float */
scanf("%lf", &long_pi); /* Read a double */