MCS-201 · Unit 1

Programming Fundamentals

Section 8 of 9

Block 1 · Unit 1Programming Fundamentals14 min · Section 8 of 9

1.8Writing and Compiling a C Program

Covers IGNOU MCS-201 §1.8, 1.9, 1.9.1, 1.9.2

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):

StageWhat happensCommand to stop after this stage
PreprocessingHandles lines starting with #, such as #include; removes commentsclang -E hello.c
CompilationTranslates C into assembly language; reports syntax and semantic errorsclang -S hello.c
AssemblyConverts 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 1 · multiple choice

What does “the program is syntactically correct” mean?

Show answer

Answer: C. The program follows the grammar rules of the language

Syntax is about grammar only. A syntactically correct program can still give wrong output, which would be a logical error.

Question 2 · multiple choice

Which line contains the syntax error?

C · q2.c
#include <stdio.h>

int main(void)
{
    int a = 5
    printf("%d\n", a);
    return 0;
}
Show answer

Answer: D. Line 5

int a = 5 on line 5 is missing its semicolon. clang points to the end of line 5, while gcc reports line 6, where it first notices the problem; either way, the mistake is on line 5.

Question 3 · multiple choice

The code include <stdio.h> / int main(void) / [ / printf("hello\n"); / ] has which two errors?

Show answer

Answer: B. Missing # before include, and square brackets instead of braces

Preprocessor directives must start with #, and function bodies are enclosed in { }, not [ ]. The printf call itself is correct.

Question 4 · multiple choice

Which of these is a semantic error rather than a syntax error?

Show answer

Answer: C. Assigning a string to an int variable

Assigning a string to an int is grammatically valid but breaks the type rules, so it is semantic. Missing punctuation and misspelt keywords break the grammar.

Question 5 · multiple choice

A program declares int unused; and never uses it. What does the compiler do?

Show answer

Answer: D. Compiles it, possibly with a warning

An unused variable breaks no language rule, so compilation succeeds; with -Wall the compiler warns about it. It is not a semantic error.

Question 6 · multiple choice

What does the -o hello part of clang hello.c -o hello do?

Show answer

Answer: A. Names the output executable hello

-o sets the output file name; without it the executable is called a.out. Running the program is a separate command, ./hello.

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.

↑ ↓ move↵ openTab switch type