
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
scanf() and fscanf() in C
Function scanf()
The function scanf() is used to read formatted input from stdin in C language. It returns the whole number of characters written in it otherwise, returns a negative value.
Here is the syntax of scanf() in C language,
int scanf(const char *characters_set)
Here is an example of scanf() in C language,
Example
#include <stdio.h> int main () { char s[20]; printf("Enter a string : "); scanf("%s", s); printf("
Entered string : %s
", s); return(0); }
Output
Enter a string : Peter! Entered string : Peter!
Function fscanf()
The function fscanf() is used to read the formatted input from the given stream in C language. It returns zero, if unsuccessful. Otherwise, it returns The input string, if successful.
Here is the syntax of fscanf() in C language,
int fscanf(FILE *stream_name, const char *set_of_characters)
Here is an example of fscanf() in C language,
Example
#include <stdio.h> #include <stdlib.h> int main () { char str1[10]; int year; FILE * fp; fp = fopen ("file.txt", "w+"); fputs("This is demo text!", fp); rewind(fp); fscanf(fp, "%s", str1); printf("First word =
%s
", str1 ); fclose(fp); return(0); }
Output
First word = This
- Related Articles
- Difference between scanf() and gets() in C
- Return values of printf() and scanf() in C
- Problem with scanf() when there is fgets()/gets()/scanf() after it in C
- fscanf() function in PHP
- What is the common error occurred while using scanf() statement in C language?
- How to simulate scanf() method using Python?
- Foreach in C++ and C#
- Comma in C and C++
- Loops in C and C++
- INT_MAX and INT_MIN in C/C++ and Applications
- isalpha() and isdigit() in C/C++
- nextafter() and nexttoward() in C/C++
- Undefined Behaviour in C and C++
- rand() and srand() in C/C++
- Floating Point Operations and Associativity in C, C++ and Java

Advertisements