A C program starts life as a plain text file. A compiler checks that the text follows C’s rules and translates it into machine code. This section shows how to write and compile a program with current tools, and how to read the compiler’s error messages.
Writing the program
A C program is typed into a source file with any plain-text editor, such as vi, nano, Visual Studio Code or Xcode. By convention the file name ends in .c, for example hello.c. Word processors are not suitable because they save formatting along with the text.
Compiling the program
Compiling with gcc or clang happens in three internal stages, followed by linking (covered in the next section):
Stage
What happens
Command to stop after this stage
Preprocessing
Handles lines starting with #, such as #include; removes comments
clang -E hello.c
Compilation
Translates C into assembly language; reports syntax and semantic errors
clang -S hello.c
Assembly
Converts assembly into machine code in an object file (hello.o; .obj on Windows)
clang -c hello.c
Syntax errors
A program is syntactically correct when it follows the grammar rules of the language. A syntax error breaks those rules, just as “Shyam, is playing, with a ball” breaks the rules of English punctuation. The compiler reports syntax errors with the file name, line number and column, and produces no object file until they are fixed. One mistake can trigger several messages, so fix the first error and recompile before worrying about the rest.
Semantic errors and warnings
A semantic error is a statement that is grammatically valid but has no valid meaning under the language’s rules, such as giving a variable a value of the wrong type or using a variable that was never declared. Like syntax errors, semantic errors stop compilation.
C · semantic_error.c
#include <stdio.h>int main(void){ int count = "five"; /* a string given to an int variable */ printf("%d\n", count); return 0;}
Compiler output
semantic_error.c:5:9: error: incompatible pointer to integer conversion initializing 'int' with an expression of type 'char[5]'
A warning is different: the compiler has noticed something suspicious, but the program still compiles. For example, a variable that is declared but never used produces warning: unused variable 'unused'. Warnings should be read and usually fixed, but they are not errors.
Key takeaways
Source code is written in a plain-text .c file and compiled with gcc or clang.
Compilation passes through preprocessing, compilation and assembly, producing an object file.
Syntax errors break grammar rules; semantic errors break meaning rules; both stop compilation.
Warnings point out suspicious code but do not stop compilation.
Practice questions
Answer on paper first, then check. The explanation matters more than the letter you chose.
Question 7 · short answer
The study material asks you to indicate the syntax errors in: include <stdio.h> / main( ) / [ / printf("hello\n"); / ]. List the errors and write the corrected program.
Show worked answer
1. include must be written #include, because preprocessor directives start with #. 2. The function body must be enclosed in braces { }, not square brackets [ ]. 3. In current C, main( ) must have a return type, so write int main(void), and end the body with return 0; (older compilers accepted the original, but compilers since C99 do not). Corrected program: #include <stdio.h> / int main(void) / { / printf("hello\n"); / return 0; / }. It prints hello.
MCA Online notes are made for reading on screen. Open https://mcastudy.online/mcs-201/programming-fundamentals/writing-and-compiling-a-c-program/ to read this page.