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.
- 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.
- 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.
- 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:
- “scanf is used to read data from standard input;”
- fscanf is used to read data from a file;
- sscanf is used to read data from a string.