Introduction
Every C program follows a basic structure that tells the compiler how the program should be processed. Understanding this structure makes it easier to read, write, and debug C programs. In this chapter, you will practice important parts of a C program such as header files, main(), statements, braces, semicolons, comments, printf(), and return 0. The questions start with simple programs and gradually introduce more elements of C program structure. Basic C Program Structure practice questions with solutions to help you understand the concepts.
Q1. Write the Basic Structure of a C Program
Problem Statement
Write a simple C program that demonstrates the basic structure of a C program.
C Program
#include <stdio.h>
int main()
{
printf("Hello, C!");
return 0;
}
Sample Output
Hello, C!
Concepts Covered
- Header file
main()function- Curly braces
- Statement
printf()return 0
Q2. Use the #include <stdio.h> Header File
Problem Statement
Write a C program that uses the stdio.h header file to display a message.
C Program
#include <stdio.h>
int main()
{
printf("Learning C Program Structure");
return 0;
}
Sample Output
Learning C Program Structure
Concepts Covered
- Preprocessor directive
#includestdio.hprintf()main()
Q3. Use Multiple Statements Inside main()
Problem Statement
Write a C program that displays three different messages using separate statements.
C Program
#include <stdio.h>
int main()
{
printf("Welcome to C Programming\n");
printf("I am learning C\n");
printf("Practice makes programming easier");
return 0;
}
Sample Output
Welcome to C Programming
I am learning C
Practice makes programming easier
Concepts Covered
- Multiple statements
printf()\n- Statement termination
- Program structure
Q4. Understand the Use of Semicolons
Problem Statement
Write a C program containing multiple statements, each ending with a semicolon.
C Program
#include <stdio.h>
int main()
{
int a = 10;
int b = 20;
int sum = a + b;
printf("Sum = %d", sum);
return 0;
}
Sample Output
Sum = 30
Concepts Covered
- Semicolon
; - Variable declaration
- Assignment
- Multiple statements
printf()
Q5. Use Single-Line Comments
Problem Statement
Write a C program that uses single-line comments to describe different parts of the program.
C Program
#include <stdio.h>
int main()
{
// Store two numbers
int a = 15;
int b = 25;
// Calculate the sum
int sum = a + b;
// Display the result
printf("Sum = %d", sum);
return 0;
}
Sample Output
Sum = 40
Concepts Covered
- Single-line comments
//- Variables
- Arithmetic expression
- Program documentation
Q6. Use Multi-Line Comments
Problem Statement
Write a C program using a multi-line comment to describe what the program does.
C Program
#include <stdio.h>
/*
This program calculates
the area of a rectangle.
*/
int main()
{
int length = 10;
int width = 5;
int area = length * width;
printf("Area = %d", area);
return 0;
}
Sample Output
Area = 50
Concepts Covered
- Multi-line comments
/* */- Variables
- Arithmetic operation
- Program documentation
Q7. Use return 0 in main()
Problem Statement
Write a C program that uses return 0 at the end of the main() function.
C Program
#include <stdio.h>
int main()
{
printf("Program executed successfully.");
return 0;
}
Sample Output
Program executed successfully.
Concepts Covered
main()return 0- Function return value
- Program termination
printf()
Q8. Create a Program With Variables and Output
Problem Statement
Write a C program that stores a student’s name, age, and marks and displays them.
C Program
#include <stdio.h>
int main()
{
char name[] = "Aman";
int age = 15;
float marks = 85.5;
printf("Name: %s\n", name);
printf("Age: %d\n", age);
printf("Marks: %.1f", marks);
return 0;
}
Sample Output
Name: Aman
Age: 15
Marks: 85.5
Concepts Covered
- Variables
- Character array
- Integer
- Float
printf()- Format specifiers
- Newline
Q9. Identify and Correct an Incorrect C Program Structure
Problem Statement
The following program contains structural and syntax mistakes. Find the mistakes and write the corrected program.
Incorrect C Program
#include <stdio.h>
int main()
{
printf("Welcome to C")
return 0;
Correct C Program
#include <stdio.h>
int main()
{
printf("Welcome to C");
return 0;
}
Sample Output
Welcome to C
Concepts Covered
- Curly braces
- Semicolon
main()- Syntax errors
- Basic program structure
Q10. Build a Complete Basic C Program
Problem Statement
Create a complete C program that displays a student’s information and calculates the total marks of three subjects.
C Program
#include <stdio.h>
int main()
{
char name[] = "Rahul";
int math = 80;
int science = 75;
int computer = 90;
int total;
total = math + science + computer;
printf("Student Name: %s\n", name);
printf("Math: %d\n", math);
printf("Science: %d\n", science);
printf("Computer: %d\n", computer);
printf("Total Marks: %d", total);
return 0;
}
Sample Output
Student Name: Rahul
Math: 80
Science: 75
Computer: 90
Total Marks: 245
Concepts Covered
- Complete C program structure
- Header file
main()- Variables
- Character array
- Arithmetic operation
- Multiple
printf()statements - Comments and formatting
return 0
Key Takeaways
- A basic C program commonly contains a header file,
main()function, statements, and a return statement. #include <stdio.h>provides declarations for standard input/output functions such asprintf().- Program execution begins from
main(). - Curly braces
{ }define the body of themain()function. - Most C statements end with a semicolon
;. printf()is used to display output.//is used for single-line comments./* */is used for multi-line comments.return 0;indicates successful completion of themain()function.- Proper program structure makes C code easier to read, compile, and maintain.
FAQs
1. What is the basic structure of a C program?
A basic C program commonly contains header files, the main() function, declarations, executable statements, and a return statement.
2. Why is main() important in C?
main() is the function where execution of a standard C program begins.
3. What is the purpose of #include <stdio.h>?
It includes the standard input/output header file, which provides declarations for functions such as printf() and scanf().
4. Why is a semicolon used in C?
A semicolon marks the end of most C statements.
For example:
printf("Hello");
5. What are curly braces { } used for in C?
Curly braces define a block of code. The statements belonging to a function, loop, condition, or other block are placed inside them.
6. What is the purpose of return 0;?
Inside main(), return 0; returns the value 0 to the operating system and commonly indicates that the program completed successfully.
7. What are comments in C?
Comments are notes written inside source code for programmers. They are ignored by the compiler.
Single-line comment:
// This is a comment
Multi-line comment:
/*
This is a
multi-line comment
*/
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
