The differences between scanf, fscanf, and sscanf

scanf, fscanf, and sscanf are input functions in the C language used to respectively read data from standard input, files, and strings.

  1. The function scanf is used to read data from standard input. It follows the format scanf(“format control string”, argument list). For example, scanf(“%d”, &num) reads an integer from standard input and stores it in the num variable.
  2. fscanf is used to read data from a file. The format is fscanf(file pointer, “format control string”, argument list). For example, fscanf(fp, “%d”, &num) means to read an integer from the file pointed to by file pointer fp and store it in the variable num.
  3. sscanf: Used to read data from a string. The format is sscanf(“string”, “format control string”, parameter list). For example, sscanf(str, “%d”, &num) reads an integer from the string str and stores it in the num variable.

Summary:

  1. “scanf is used to read data from standard input;”
  2. fscanf is used to read data from a file;
  3. sscanf is used to read data from a string.
bannerAds