Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • C
  • C Basics
  • C Data Types
  • C Operators
  • C Input and Output
  • C Control Flow
  • C Functions
  • C Arrays
  • C Strings
  • C Pointers
  • C Preprocessors
  • C File Handling
  • C Programs
  • C Cheatsheet
  • C Interview Questions
  • C MCQ
  • C++
Open In App
Next Article:
Return values of printf() and scanf() in C/C++
Next article icon

Return values of printf() and scanf() in C/C++

Last Updated : 29 Nov, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report

What values do the printf() and scanf() functions return ? 
 

printf() : It returns total number of Characters Printed, Or negative value if an output error or an encoding error 
Example 1: The printf() function in the code written below returns 6. As 'CODING' contains 6 characters.
 

CPP
// C/C++ program to demonstrate return value // of printf() #include <stdio.h>  int main() {     char st[] = "CODING";      printf("While printing ");     printf(", the value returned by printf() is : %d",                                     printf("%s", st));      return 0; } 

Output
While printing CODING, the value returned by printf() is : 6


Example 2: The printf() function in the code written below returns 9. As '123456789' contains 9 characters.
 

CPP
// C/C++ program to demonstrate return value // of printf()  #include <stdio.h> int main() {     long int n = 123456789;      printf("While printing ");     printf(", the value returned by printf() is : %d",                                      printf("%ld", n));      return 0; } 

Output
While printing 123456789, the value returned by printf() is : 9


 


 

scanf() : It returns total number of Inputs Scanned successfully, or EOF if input failure occurs before the first receiving argument was assigned.
Example 1: The first scanf() function in the code written below returns 1, as it is scanning 1 item. Similarly second scanf() returns 2 as it is scanning 2 inputs and third scanf() returns 3 as it is scanning 3 inputs.
 

CPP
// C/C++ program to demonstrate return value // of printf()  #include <stdio.h> int main() {     char a[100], b[100], c[100];      // scanf() with one input     printf("\n First scanf() returns : %d",                             scanf("%s", a));      // scanf() with two inputs     printf("\n Second scanf() returns : %d",                        scanf("%s%s", a, b));      // scanf() with three inputs     printf("\n Third scanf() returns : %d",                    scanf("%s%s%s", a, b, c));      return 0; } 
Input: Hey! welcome to geeks for geeks   Output:  First scanf() returns : 1  Second scanf() returns : 2  Third scanf() returns : 3


 


Next Article
Return values of printf() and scanf() in C/C++

K

kartik
Improve
Article Tags :
  • C Language
  • GFacts
  • cpp-input-output
  • C-Input and Output Quiz

Similar Reads

    Cin-Cout vs Scanf-Printf
    Regular competitive programmers face common challenge when input is large and the task of reading such an input from stdin might prove to be a bottleneck. Such problem is accompanied with “Warning: large I/O data”. Let us create a dummy input file containing a line with 16 bytes followed by a newlin
    3 min read
    Use of & in scanf() but not in printf()
    Why there is need of using '&' in case of scanf function while not in case of printf function. Examples: scanf("%d %d", &a, &b);printf("%d %d", a, b);As a and b above are two variable and each has their own address assigned but instead of a and b, we send the address of a and b respectiv
    1 min read
    Print an Integer Value in C
    Printing an Integer in C is one of the most basic tasks. Input and output of the elements are essential for taking input from the user and then giving the output to the user. Here we are going to take an integer as input from the user with the help of the scanf() function and print that integer with
    2 min read
    C Function Arguments and Function Return Values
    Prerequisite: Functions in CA function in C can be called either with arguments or without arguments. These functions may or may not return values to the calling functions. All C functions can be called either with arguments or without arguments in a C program. Also, they may or may not return any v
    5 min read
    What does main() return in C and C++?
    C According to coding standards, a good return program must exit the main function with 0. Although we are using void main() in C, In which we have not suppose to write any kind of return statement but that doesn't mean that C code doesn't require 0 as exit code. Let's see one example to clear our t
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences