Input and Output in C

Output to the Console

This demonstration shows how to use in the printf procedure the format specifiers %d, %f, %c, and %s for integer, float, char and string (char array), respectively. See also use the escape sequences \n, \t, and \a for new line, tab and beep, respectively.

#include <stdio.h>

int main()
{
  int my_int = 42;
  float my_float = 1.2345f;
  char my_char = 'a';
  char my_string[20] = "This is my string.";
  printf("my_int: %d\n", my_int);
  printf("my_float to 3 dp: %5.3f\n", my_float);
  printf("my_char: %c\n", my_char);
  printf("my_string: %s\n", my_string);
  printf("words\tseparated\tby\ttabs\n");
  printf("\a"); // beep
  return 0;
}   

Output:

my_int: 42
my_float to 3 dp: 1.235
my_char: a
my_string: This is my string.
words   separated       by      tabs

Input from the Keyboard

You can conveniently input values with scanf. You need to declare a variable that will be changed by scanf (as you do with a var parameter in Pascal). In order to make it possible for a C routine to change the value of variable, you must pass to it a pointer to the variable (the variable's address). You achieve this by prefixing the identifier with an ampersand (&). Indicate the type of the input variable with the specifiers %d, %f, %s, and %c as used in printf. See the following page for validation of keyboard input including the handling of erroneous input.

#include <stdio.h>

int main()
{
  char forename[20];
  char character;
  int age;
  float float_input;
  printf("Please enter your first name: ");
  scanf("%s", &forename);
  printf("Hello, %s\n", forename);
  printf("Please enter a character: ");
  scanf(" %c", &character);  // Note the space before %c
  printf("Character entered: %c\n", character);
  printf("Please enter your age: ");
  scanf("%d", &age);
  age++; // add 1 to age
  printf("Your age next year: %d\n", age);
  printf("Please enter a float such as 1.2345: ");
  scanf("%f", &float_input);
  printf("Your float to 3dp: %.3f", float_input);
  return(0);
}
    

Input and output:

Please enter your first name: Joe
Hello, Joe
Please enter a character: a
Character entered: a
Please enter your age: 17
Your age next year: 18

Output to a Text File

This simple example prints ten lines to a text file. The useful tutorialspoint page tabulates the file access modes. Here we demonstrate write (w) and in the following section we open a file with read (r) access.

#include <stdio.h>

int main()
{
  FILE *f_ptr; // declares a pointer to a file
  f_ptr = fopen("test.txt", "w"); // open for writing
  int i;
  for (i = 1; i <= 10; i++)
    fprintf(f_ptr, "line %d\n", i);
  fclose(f_ptr);
  return 0;
}    

Input from a Text File

This demonstration requires in its project folder a copy of the text file from the previous program.

#include <stdio.h>
#include <string.h>

int main()
{
  FILE *f_ptr;
  char line[50];
  char lines[10][50];
  int i = 0;
  f_ptr = fopen("test.txt", "r");
  while(fgets(line, 50, f_ptr))
  {
    strcpy(lines[i], line);
    i++;
  }
  // Print alternate lines in array
  for (i = 0; i < 9; i += 2)
    printf(lines[i]);
  fclose(f_ptr);
  return 0;
}
    

Output:

line 1
line 3
line 5
line 7
line 9

Writing to and Reading from a Binary File

This demonstration writes ten integers (each of size 4 bytes) from an array into a file and then reads them back from the same file into a different array. Finally, it displays the integers read from the file.

#include <stdio.h>

int main()
{
  int my_ints[10] = {1, 2, 3, 5, 7, 11, 13, 17, 19, 23};
  FILE *f_ptr; // declares a pointer to a file
  f_ptr = fopen("test.dat", "w"); // open for writing
  fwrite(&my_ints, 4, 10, f_ptr);
  fclose(f_ptr);

  int my_ints_from_file[10];
  f_ptr = fopen("test.dat", "r");
  fread(&my_ints_from_file, 4, 10, f_ptr);
  fclose(f_ptr);
  int i;
  for (i = 0; i < 10; i++)
    printf("%d ", my_ints_from_file[i]);
  return 0;
}
    

Output:

1 2 3 5 7 11 13 17 19 23
Programming - a skill for life!

How to apply your knowledge of Pascal when learning C/C++