C programming language (Second Edition): exercise 1-1

The title is: run the "hello world" program on your own system. Then deliberately remove some of the contents of the program to see what error messages you will get.

The source code is as follows:

#include <stdio.h>

int main()
{
    printf("Hello World\n");
    return 0;
}

Compiled with online C compiler:

c online compiler, c language online interpreter, online programming website

Case 1:   Remove the ";" after the printf statement, and the reported compilation errors are as follows:   

Compilation Failed


/usercode/file.cpp: In function 'int main()':
/usercode/file.cpp:5:28: error: expected ';' before 'return'
    5 |     printf("Hello World\n")
      |                            ^
      |                            ;
    6 |     return 0;
      |     ~~~~~~                  

The error shows that a semicolon is missing before the return and indicates the specific location.

Case 2: remove the first line #include < stdio. H >, and the compilation error is as follows:

Compilation Failed


/usercode/file.cpp: In function 'int main()':
/usercode/file.cpp:5:5: error: 'printf' was not declared in this scope
    5 |     printf("Hello World\n")
      |     ^~~~~~
/usercode/file.cpp:1:1: note: 'printf' is defined in header '<cstdio>'; did you forget to '#include <cstdio>'?
  +++ |+#include <cstdio>
    1 | 

The compiler prompts that the printf function is not declared, and further prompts that printf is defined in the header file < cstdio >.

Case 3: remove the braces} in the last line, and the compilation error is as follows:

Compilation Failed


/usercode/file.cpp: In function 'int main()':
/usercode/file.cpp:6:13: error: expected '}' at end of input
    6 |     return 0;
      |             ^
/usercode/file.cpp:4:1: note: to match this '{'
    4 | {
      | ^

  The error prompt is missing a "}" after return 0, and further prompts that this "}" is "{" in the first column of row 4 of match.

Case 4: remove the parentheses in the printf statement, and the compilation error is as follows:

Compilation Failed


/usercode/file.cpp: In function 'int main()':
/usercode/file.cpp:5:27: error: expected ')' before ';' token
    5 |     printf("Hello World\n";
      |           ~               ^
      |                           )

  The word token is used in the description of the error message. Here, token is a term in the compilation principle, which can be understood as a word and the smallest unit of lexical analysis.

Reference answers in the book:

#include <stdio.h>
main()
{
    printf("hello, world");
}

The above example omits the line break (\ n), which will keep the cursor at the end of the output information.

#include <stdio.h>

main()
{
    printf("hello, world\n")
}

In this example, the semicolon after printf() is omitted. The statement of C program must end with a semicolon (see page 5 of the textbook). Therefore, for this example, the compiler will recognize that one semicolon is missing and give the corresponding error message.

#include <stdio.h>

main()
{
    printf("hello, world\n');
}

In the third example, \ n the following double quotation marks "Was wrongly written as a single quotation mark '. Therefore, this single quotation mark, followed by the closing parenthesis and semicolon, will be regarded as part of the entire output string. The compiler will treat this as an error and report that a double quotation mark is missing; a closing parenthesis is is missing before the closing curly bracket; the string is too long; and there is a newline in the string.

Add: for the third example in the reference answer, the error reported after the actual attempt is as follows

Compilation Failed


/usercode/file.cpp:5:12: warning: missing terminating " character
    5 |     printf("hello, world\n');
      |            ^
/usercode/file.cpp:5:12: error: missing terminating " character
    5 |     printf("hello, world\n');
      |            ^~~~~~~~~~~~~~~~~~
/usercode/file.cpp:3:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
    3 | main()
      | ^~~~
/usercode/file.cpp: In function 'int main()':
/usercode/file.cpp:6:1: error: expected primary-expression before '}' token
    6 | }
      | ^

The first warning and error prompt are missing a double quotation mark "; the second warning means that standard C + + does not allow the main function to have no return type; the second error means that a closing bracket is missing before the curly bracket, because the compiler only finds the opening bracket and does not find the closing bracket (the closing bracket is treated as a string)

It is not clear what primary expression means here

Keywords: C

Added by rab on Thu, 14 Oct 2021 20:29:34 +0300