printf and scanf
printf and scanf
Objective
Understand the basic structure of a C program, learn how to use an online compiler, and explore
key components like preprocessor directives, the main function, and input/output functions
(printf and scanf) with format specifiers.
Components Explained
1. Preprocessor Directives
What They Are: Preprocessor directives are lines in your code that start with a # symbol. They
are processed by the preprocessor before the actual compilation begins. These directives tell the
compiler to include files, define constants, or perform other setup tasks.
#include <stdio.h>
#include: This directive instructs the preprocessor to include the contents of a file into
your program.
<stdio.h>: This is the Standard Input-Output header file, which contains declarations for
functions like printf() and scanf(). Without it, the compiler wouldn’t recognize these
functions.
How It Works: The preprocessor replaces #include <stdio.h> with the actual code from
the stdio.h file, making its functions available to your program.
Why It’s Important: Preprocessor directives set up the environment for your program.
<stdio.h> is essential because it enables input/output operations.
What It Is: The main function is the entry point of every C program. When you run your
program, the operating system starts execution here.
Syntax:
int main() {
Page 1 of 5
Programming Fundamentals Dr. Shakir Karim Buksh
int: This specifies that main returns an integer value to the operating system. A return
value of 0 typically means "successful execution," while non-zero values indicate errors.
main(): The function name, with empty parentheses () indicating it takes no arguments
(though variations like int main(int argc, char *argv[]) exist for command-line
arguments).
{ ... }: Curly braces enclose the body of the function, where your program’s logic lives.
return 0: This statement ends the main function and sends 0 back to the system, signaling
success.
int main() {
printf("Hello, World!\n");
return 0;
}
Here, main contains a single printf call and returns 0. It’s the simplest valid C program.
Why It’s Important: Without main, the compiler doesn’t know where to start, and your
program won’t run.
What It Is: printf (print formatted) is a function from <stdio.h> that outputs text or data to the
console.
Syntax:
Format String: A string that defines what to print, including placeholders (format
specifiers) for variables.
Arguments: Optional values to insert into the format string.
printf("Hello, World!\n");
"Hello, World!\n": The format string. The \n is an escape sequence that adds a newline,
moving the cursor to the next line.
No arguments are needed here because it’s just static text.
Page 2 of 5
Programming Fundamentals Dr. Shakir Karim Buksh
Why It’s Important: printf is your primary tool for displaying output, making it essential for
debugging and user interaction.
What It Is: scanf (scan formatted) is a function from <stdio.h> that reads input from the user via
the console.
Syntax:
Format String: Specifies the type of data to read (using format specifiers).
&variable: The address of the variable where the input will be stored. The & (address-of
operator) is required for basic types like int, float, and char.
Example:
char name[20];
printf("Enter your name: ");
scanf("%s", name);
printf("Welcome, %s!\n", name);
Why It’s Important: scanf enables interactivity by allowing users to provide data to your
program.
5. Format Specifiers
What They Are: Format specifiers are placeholders in printf and scanf that define the type and
format of data to display or read. They start with %.
Page 3 of 5
Programming Fundamentals Dr. Shakir Karim Buksh
Usage in printf:
Usage in scanf:
scanf("%d", &age);
o Reads an integer into age.
Why They’re Important: Format specifiers ensure data is interpreted correctly, preventing
errors like printing an integer as a float.
Page 4 of 5
Programming Fundamentals Dr. Shakir Karim Buksh
Key Concepts
Page 5 of 5