Jul 20, 2014

K&R#01 Exercise 1.1 - Leave out parts of program

Problem Statement:

Run the "hello, world" program on your system. Experiment with leaving out parts of the program to see what error messages you get.


Answer:


Lets us write the hello world program for our reference.


1
2
3
4
5
6
#include <stdio.h>
int main()
{
   printf("hello, world\n");
   return 0;
}

1. Modify first line to #include <stdio.h

Error:
$ gcc e_1_1.c 
e_1_1.c:1:18: error: missing terminating > character

Explanation:
e_1_1.c:1:18: error: => This is telling the exact location of error.

missing terminating > character => explanation of the error.

Here explanation of the error tells exactly what the error is.

2. Modify first line to #include <stdio>

Error:
$ gcc e_1_1.c 
e_1_1.c:1:17: fatal error: stdio: No such file or directory
compilation terminated.

Explanation:
  • Here the error is categorized as fatal error.
  • Compiler is complaining that there is no file stdio.
  • Error hints properly at what error is.

3. Modify line 1 to include <stdio.h> [note # is missing]

Error:
$ gcc e_1_1.c 
e_1_1.c:1:9: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘<’ token

Explanation:
  • Compiler is telling there some error in line 1.
  • Error is not hinting at missing #.
  • Compiler is no longer treating this as pre-processor statement because of missing #.

4. Modify line 2 to int main [round brackets are missing after main] 

Error:
$ gcc e_1_1.c 
e_1_1.c:3:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token

Explanation:

  • Compiler is telling that there is error in 3rd line.
  • Because of missing (), it no longer treats main as function.
  • Error is not exactly pointing out where the error is.

5. Remove semicolon from line 4. [at the end of printf]

Error:
e_1_1.c: In function ‘main’:
e_1_1.c:5:2: error: expected ‘;’ before ‘return’

Explanation:
  • Compiler now tells that error is inside function main.
  • It is exactly pointing out that ; is missing before return.
Previous Article - NONE 
All Article - K & R Answers

No comments :

Post a Comment