Introduction
Before running a C program, you need a C compiler to convert your source code into an executable program. In this chapter, you will practice the basic steps involved in setting up a C programming environment. The questions cover checking GCC, creating C files, compiling programs, running executables, using compiler options, and identifying simple compilation errors. These examples are designed for beginners who are setting up C for the first time. Installing C Compiler Setup Practice Questions with Solutions to help you build concepts.
Q1. Check Whether GCC Is Installed
Problem Statement
Check whether the GCC compiler is installed and available on your computer.
C Command
Open Command Prompt on Windows or Terminal on Linux/macOS:
gcc --version
Expected Output
You should see GCC version information similar to:
gcc (GCC) 13.2.0
The version number can be different depending on your installation.
Concepts Covered
- C compiler
- GCC
- Command Prompt
- Terminal
- Compiler version
Q2. Create a C Source File
Problem Statement
Create a C source file named hello.c and write a simple program that displays a message.
C Program
#include <stdio.h>
int main()
{
printf("Hello, C Programming!");
return 0;
}
Save the file as:
hello.c
Expected Output
Hello, C Programming!
Concepts Covered
- C source file
.cextensionmain()printf()- Basic C program
Q3. Compile a C Program Using GCC
Problem Statement
Compile hello.c using GCC and create an executable named hello.
C Command
gcc hello.c -o hello
Run the Program
Windows:
hello
Linux/macOS:
./hello
Expected Output
Hello, C Programming!
Concepts Covered
- Compilation
- GCC
-ooption- Executable file
- Running a program
Q4. Compile a C Program Without -o
Problem Statement
Compile a C program without specifying a custom executable name.
C Program
#include <stdio.h>
int main()
{
printf("My first compiled C program.");
return 0;
}
Save it as:
first.c
C Command
gcc first.c
Expected Result
GCC creates a default executable name.
On many Windows GCC installations:
a.exe
On Linux/macOS:
a.out
Run the Program
Windows:
a
Linux/macOS:
./a.out
Expected Output
My first compiled C program.
Concepts Covered
- GCC compilation
- Default executable
a.exea.out
Q5. Compile a Program With a Custom Executable Name
Problem Statement
Create a program that calculates the sum of two numbers and compile it with the executable name sum.
C Program
#include <stdio.h>
int main()
{
int a = 15;
int b = 25;
printf("Sum = %d", a + b);
return 0;
}
Save the file as:
sum.c
C Command
gcc sum.c -o sum
Run the Program
Windows:
sum
Linux/macOS:
./sum
Expected Output
Sum = 40
Concepts Covered
- Custom executable name
-o- Compilation
- Running executable
- Arithmetic expression
Q6. Compile a Program With -Wall
Problem Statement
Compile a C program with GCC’s -Wall option to enable a broad set of useful compiler warnings.
C Program
#include <stdio.h>
int main()
{
int number = 100;
printf("Number = %d", number);
return 0;
}
Save the file as:
warning.c
C Command
gcc -Wall warning.c -o warning
Run the Program
Windows:
warning
Linux/macOS:
./warning
Expected Output
Number = 100
Concepts Covered
- Compiler warnings
-Wall- GCC options
- Code checking
Q7. Find a Missing Semicolon Error
Problem Statement
The following C program contains a compilation error. Identify and fix the error.
Incorrect C Program
#include <stdio.h>
int main()
{
printf("Learning C")
return 0;
}
C Command
gcc error.c -o error
Expected Compiler Result
The compiler will report an error because the printf() statement is missing a semicolon.
The exact error message depends on the compiler version.
Correct C Program
#include <stdio.h>
int main()
{
printf("Learning C");
return 0;
}
Expected Output
Learning C
Concepts Covered
- Compilation error
- Syntax error
- Semicolon
- Debugging
- Compiler error messages
Q8. Compile a C Program From a Different Folder
Problem Statement
Suppose your C program is stored inside a folder named CPrograms. Navigate to that folder using Command Prompt and compile the program.
C Program
#include <stdio.h>
int main()
{
printf("C Programming Practice");
return 0;
}
Save the file as:
practice.c
C Commands
Move into the folder:
cd CPrograms
Compile the program:
gcc practice.c -o practice
Run it on Windows:
practice
Expected Output
C Programming Practice
Concepts Covered
- Folder navigation
cd- GCC compilation
- Executable files
- Command Prompt
Q9. Compile and Run a Program With User Input
Problem Statement
Create a C program that accepts two numbers from the user and displays their sum. Compile and run the program using GCC.
C Program
#include <stdio.h>
int main()
{
int a, b;
printf("Enter first number: ");
scanf("%d", &a);
printf("Enter second number: ");
scanf("%d", &b);
printf("Sum = %d", a + b);
return 0;
}
Save the file as:
addition.c
C Command
gcc addition.c -o addition
Run the Program
Windows:
addition
Linux/macOS:
./addition
Sample Output
Enter first number: 20
Enter second number: 30
Sum = 50
Concepts Covered
- Compilation
- Executable
scanf()printf()- User input
- Arithmetic operation
Q10. Verify the Complete C Compiler Setup
Problem Statement
Create a small C program that uses user input, compile it, run it, and verify that your complete C programming environment is working correctly.
C Program
#include <stdio.h>
int main()
{
int number;
printf("Enter a number: ");
scanf("%d", &number);
printf("You entered: %d", number);
return 0;
}
Save the file as:
test.c
C Command
gcc test.c -o test
Run the Program
Windows:
test
Linux/macOS:
./test
Sample Output
Enter a number: 75
You entered: 75
If the program compiles without errors and produces the expected output, your basic C compiler setup is working correctly.
Concepts Covered
- C compiler
- Source file
- Compilation
- Executable
- User input
scanf()printf()- Running a C program
Key Takeaways
- A C compiler converts C source code into executable code.
- GCC is a widely used compiler for C programming.
- C source files normally use the
.cextension. gcc --versioncan be used to check GCC availability.gcc file.ccompiles a C source file.-oallows you to specify the executable name.-Wallenables many useful compiler warnings.- Compilation errors must be corrected before a program can run.
cdis used to change directories in Command Prompt or Terminal.- Compiling and running a small program is a good way to verify your C setup.
FAQs
1. What is a C compiler?
A C compiler is software that converts C source code into executable code that a computer can run.
2. What is GCC in C programming?
GCC stands for GNU Compiler Collection. It provides a compiler that can compile C programs.
3. How do I check whether GCC is installed?
Open Command Prompt or Terminal and run:
gcc --version
If GCC is installed and correctly configured, its version information will be displayed.
4. What does the .c extension mean?
The .c extension identifies a file containing C source code.
For example:
program.c
5. What does gcc program.c -o program do?
It compiles program.c and creates an executable with the name program.
6. What is the purpose of -Wall?
-Wall enables a broad set of commonly useful compiler warnings. These warnings can help you find potential problems in your C code.
7. Why does GCC say that gcc is not recognized?
This usually means GCC is not installed, or the compiler’s executable directory has not been added to the system’s PATH.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
