×

Advanced Coding & Software Engineering Program

Duration: 1 Year (12 Months)

Join our premium 1-year program to master cutting-edge technologies and become an industry-ready Software Engineer!

Course Coverage

  • Languages: C, C++, Java, JavaScript, Python
  • Web Technologies: HTML, CSS, Bootstrap 5, MERN Stack, Full Stack Development
  • Databases: MySQL, MongoDB
  • Data Science Libraries: Pandas, NumPy
  • Development Tools: Visual Studio Code, IntelliJ IDEA, PyCharm, Postman, Git, GitHub
  • Cloud Platforms: Vercel, MongoDB Atlas

Program Highlights

  • Live Classes: Interactive sessions with real-time doubt resolution
  • Hands-On Sessions: Practical coding exercises to build real-world skills
  • Industry Experts: Learn from professionals with years of experience
  • Live Project: Work on real-world projects to apply your skills
  • Get Certificate: Earn a professional certificate upon program completion

Course Fee: Only ₹1020 / month
Limited Period Offer!

printf() and scanf() method in C



Last Updated on: 24th Jul 2025 18:22:10 PM

In C programming, we use printf() to display output on the screen and scanf() to take input from the user.

These functions are part of the stdio.h (Standard Input Output) library, so you must include this header at the top of your program.

 

#include <stdio.h>

 

1. printf() – Output Function

 

The printf() function is used to print or display text, variables, or results to the screen.

 

Syntax:

printf("format string", variables);

 

Example:

#include <stdio.h>

int main() {
    int age = 20;
    printf("My age is %d", age);
    return 0;
}

 

Output:

My age is 20

 

Common Format Specifiers for printf()

| Specifier | Meaning                     | Example                    |
| --------- | --------------------------- | -------------------------- |
| `%d`      | Integer                     | `printf("%d", 10);`        |
| `%f`      | Float                       | `printf("%f", 3.14);`      |
| `%.2f`    | Float with 2 decimal places | `printf("%.2f", 3.14159);` |
| `%c`      | Character                   | `printf("%c", 'A');`       |
| `%s`      | String                      | `printf("%s", "Hello");`   |

 

2. scanf() – Input Function

 

The scanf() function is used to accept input from the user during program execution.

 

Syntax:

scanf("format string", &variable);

 

Note: We use the & (address-of) operator to store the user input in the variable.

 

Example:

#include <stdio.h>

int main() {
    int age;
    printf("Enter your age: ");
    scanf("%d", &age);
    printf("You entered: %d", age);
    return 0;
}

 

Sample Input/Output:

 

Enter your age: 25
You entered: 25
 

 

Common Format Specifiers for scanf()

| Specifier | Meaning            |
| --------- | ------------------ |
| `%d`      | Integer            |
| `%f`      | Float              |
| `%c`      | Character          |
| `%s`      | String (no spaces) |

 

NOTE : To accept strings with spaces, use fgets() instead of scanf().

 

Syntax of fgets()

fgets(variable_name, size, stdin);

 

  • variable_name → where to store the input string

  • size → maximum number of characters to read (including the \0 null terminator)

  • stdin → standard input (keyboard)

 

Example: Read Full Name

#include <stdio.h>

int main() {
    char fullName[100];

    printf("Enter your full name: ");
    fgets(fullName, sizeof(fullName), stdin);  // Reads input with spaces

    printf("Your full name is: %s", fullName);
    return 0;
}

 

Output:

Enter your full name: Ramesh Kumar Yadav
Your full name is: Ramesh Kumar Yadav

 

When to Use fgets() Instead of scanf():

  • Use fgets() when:

    • You want to read strings with spaces (like full name, address).

    • You want to avoid buffer overflow.

    • You need safe input handling.

 

Multiple Input Example :

#include <stdio.h>

int main() {
    int a, b;
    printf("Enter two numbers: ");
    scanf("%d %d", &a, &b);
    printf("Sum = %d", a + b);
    return 0;
}

 

Sample Input/Output:


Enter two numbers: 4 5
Sum = 9
 

Key Points to Remember :

 

  • printf() is for output, scanf() is for input.

  • Always use & before variable names in scanf() (except for strings).

  • Both functions are part of the <stdio.h> library.

  • scanf() cannot read spaces in strings — for that, use fgets().

 

 


Online - Chat Now
Let’s Connect

Inquiry Sent!

Your message has been successfully sent. We'll get back to you soon!

iKeySkills Logo