printf and scanf

In C, the printf() and scanf() functions are commonly used for input and output operations. Here's a brief explanation of each:

printf() Function

The printf() function is used to print formatted output to the console. It allows you to display text and values of variables.

#include 

int main() {
    printf("Text to be displayed\n");
    printf("Format specifiers", variable1, variable2, ...);
    return 0;
}

Example:

#include 

int main() {
    int age = 25;
    printf("My age is %d years old.\n", age);
    return 0;
}

Output:

My age is 25 years old.

scanf() Function

The scanf() function is used to read input from the user or a file. It allows you to accept values and store them in variables.

Syntax:

#include 

int main() {
    scanf("Format specifiers", &variable1, &variable2, ...);
    return 0;
}

Example:

#include 

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

Output:

Enter your age: 25
You entered: 25

Note:

  • The & (address-of operator) is used in scanf() to pass the memory address of the variable where the input should be stored.
  • Format specifiers in printf() and scanf() are placeholders for the values that are being displayed or read.

These functions are just a starting point for working with input and output in C. There are many more formatting options and features available. Join our Youtube channel for a more in-depth understanding.