Programming Fundamentals Dr.
Shakir Karim Buksh
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.
Tutorial: Detailed Explanation
Tasks
1. Write a program to print "Hello, World!" to the console.
2. Modify it to print your name and a welcome message (e.g., "Welcome, [Your Name]!").
3. Add comments explaining what each line does.
4. Extend the program to take user input (e.g., your name) and print a personalized message.
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.
2. The main Function
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
// Code goes here
return 0;
}
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.
3. The printf Function
What It Is: printf (print formatted) is a function from <stdio.h> that outputs text or data to the
console.
Syntax:
printf("format string", arguments);
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
Extended Example with Variables:
int age = 20;
printf("Age: %d\n", age);
Age: %d\n: The format string, where %d is a placeholder for an integer.
age: The variable whose value (20) replaces %d.
Why It’s Important: printf is your primary tool for displaying output, making it essential for
debugging and user interaction.
4. The scanf Function
What It Is: scanf (scan formatted) is a function from <stdio.h> that reads input from the user via
the console.
Syntax:
scanf("format string", &variable);
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);
"Enter your name: ": Prompts the user.
scanf("%s", name): Reads a string (no & needed for arrays like name because the array
name is already a pointer).
"Welcome, %s!\n": Prints the input with %s replaced by the user’s 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
Common Format Specifiers:
Specifier Data Type Description Example Output/Input
%d int Decimal integer 42
%f float Floating-point number 3.14
%c char Single character A
%s char[] (string) String of characters Hello
%lf double Double-precision float 3.14159
Usage in printf:
printf("Age: %d, Height: %.1f\n", 20, 5.9);
o %d: Displays 20.
o %.1f: Displays 5.9 with one decimal place (.1 controls precision).
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.
Complete Sample Code
#include <stdio.h> // Preprocessor directive to include input/output
functions
int main() { // Main function: program starts here
// Task 1: Print "Hello, World!"
printf("Hello, World!\n"); // printf displays text with a newline
// Task 2: Print a personalized message
printf("Welcome, Alex!\n"); // Static welcome message
// Task 4: Take user input and print dynamically
char name[20]; // Array to store the user's name
printf("Enter your name: "); // Prompt for input
scanf("%s", name); // Read a string into name
printf("Welcome, %s!\n", name); // Display the user's name
Page 4 of 5
Programming Fundamentals Dr. Shakir Karim Buksh
return 0; // End program successfully
}
Key Concepts
Preprocessor Directives: #include <stdio.h> prepares the program with necessary
functions.
Main Function: int main() is where execution begins and ends with return 0.
printf: Outputs formatted text or data (e.g., printf("Text: %d", 123);).
scanf: Reads user input into variables (e.g., scanf("%s", name);).
Format Specifiers: %d, %f, %c, %s control how data is displayed or read.
Page 5 of 5