

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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("\nEntered string : %s\n", 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 = \n%s\n", str1 ); fclose(fp); return(0); }
Output
First word = This
- Related Questions & Answers
- Difference between scanf() and gets() in C
- Return values of printf() and scanf() in C
- fscanf() function in PHP
- Problem with scanf() when there is fgets()/gets()/scanf() after it in C
- How to simulate scanf() method using Python?
- What is the common error occurred while using scanf() statement in C language?
- INT_MAX and INT_MIN in C/C++ and Applications
- Foreach in C++ and C#
- Comma in C and C++
- Loops in C and C++
- strdup() and strdndup() in C/C++
- isalpha() and isdigit() in C/C++
- nextafter() and nexttoward() in C/C++
- Undefined Behaviour in C and C++
- rand() and srand() in C/C++
Advertisements