Introduction to C Programming - Programming Errors

Chapter chap1 section 6

It is quite likely that your attempt to write a hello world program will be completely successful and will work first time. It will probably be the only C program you ever write that works first time. There are many different types of mistake you might make when writing C programs. These can result in messages from the compiler, or sometimes the linker, programs that bring the computer grinding to a halt or programs that simply produce the wrong answer to whatever problem they were trying to solve or programs that get stuck in a never-ending loop. As a beginner you are more likely to encounter compiler or linker error messages, particularly if you have already done some programming in another programming language. The rest of this section illustrates some possible errors and their consequences.

In the first example the programmer has forgotten that the C programming language is case sensitive.

MAIN()
{
	printf("hello, world\n");
}
This error gave rise to some rather mysterious error messages from the linker. Typical examples are shown below. The compiler on the IBM 6150 used to write these notes produced the following error output.
ld: Undefined -
	.main
	_main
	_end
The compiler on a SUN Sparc Station produced the following messages.
ld: Undefined symbol 
   _main 
Compilation failed
The Turbo C integrated environment produced the following.
Linker Error: Undefined symbol _main in function main
And finally Microsoft C version 5.1 produced the following error messages.
hw.c

Microsoft (R) Overlay Linker  Version 3.65   
Copyright (C) Microsoft Corp 1983-1988.  All rights reserved.

Object Modules [.OBJ]: HW.OBJ 
Run File [HW.EXE]: HW.EXE /NOI
List File [NUL.MAP]: NUL
Libraries [.LIB]: SLIBCEC.LIB 

LINK : error L2029: Unresolved externals:



_main in file(s):
 C:\MSC\LIB\SLIBCEC.LIB(dos\crt0.asm)

There was 1 error detected

All these errors reflect the fact that the linker cannot put the program together properly. On Unix based systems the linker is usually called "ld". Along with the user supplied main() function all C programs include something often called the run-time support package which is actually the code that the operating system kicks into life when starting up your program. The run-time support package then expects to call the user supplied function main(), if there is no user supplied main() then the linker cannot finish the installation of the run-time support package. In this case the user had supplied MAIN() rather than main(). "MAIN" is a perfectly valid C function name but it isn't "main".

The rather extensive set of messages from Microsoft C 5.1 were partly generated by the compiler, which translated the program without difficulty, and partly generated by the linker. The reference to "crt0" is, in fact, a reference to the C Run Time package which tries to call main() to start the program running.

In the second example the programmer, probably confusing C with another programming language, had used single quotes rather than double quotes to enclose the string passed to printf().

main()
{
	printf('hello, world\n');
}
On the IBM 6150 the compiler produced the following error messages. The reference to "hw.c" is to the name of the source file that was being compiled.
"hw.c", line 3: too many characters in character constant
"hw.c", line 3: warning: Character constant contains more than one byte
The SUN Sparc Station compiler gave the following error messages.
"hw.c", line 3: too many characters in character constant
Compilation failed
The Turbo Integrated environment gave the following messages. C:\ISPTESTS\HW.C was the name of the source file.
Error C:\ISPTESTS\HW.C 3:
Character constant too long in function main
Microsoft C version 5.1 gave the following messages.
hw.c
hw.c(3) : error C2015: too many chars in constant
In each case the error message referred clearly to the line number in error. The reference to character constants appears because the C language uses single quotes for a different purpose (character constants).

In the third example the programmer, again possibly confused by another programming language, had missed out the semi-colon on line 3.

main()
{
	printf("hello, world\n")
}
The IBM 6150 compiler produced the following message.
"hw.c", line 4: syntax error
The SUN Sparc Station produced the following messages.
"hw.c", line 4: syntax error at or near symbol }
Compilation failed
Turbo C produced the following messages.
Error C:\ISPTESTS\HW.C 4:
Statement missing ; in function main
The Microsoft C version 5.1 compiler produced the following messages.
hw.c
hw.c(4) : error C2143: syntax error : missing ';' before '}'
In all cases the error message referred to line 4 although the error was actually on line 3. This often happens when compilers detect errors in free-format languages such as C. Simply the compiler didn't realise anything was wrong until it encountered the } on line 4. The first error message is particularly unhelpful.

In the fourth example the programmer wrote print() rather than printf().

main()
{
	print("hello, world\n");
}
This, not surprisingly, produced the linker error messages shown in below. These are rather similar to the error messages shown earlier when the effects of writing MAIN() rather than main() were shown. The IBM 6150 compiler generated the following messages.
ld: Undefined -
	.print
	_print
The SUN Sparc Station compiler generated the following messages.
ld: Undefined symbol 
   _print 
Compilation failed
Turbo C generated the following messages.
Linking C:\ISPTESTS\HW.C 4:
Linker Error: Undefined symbol _print in module HW.C
The Microsoft C version 5.1 compiler produced the following messages.
hw.c

Microsoft (R) Overlay Linker  Version 3.65   
Copyright (C) Microsoft Corp 1983-1988.  All rights reserved.

Object Modules [.OBJ]: HW.OBJ 
Run File [HW.EXE]: HW.EXE /NOI
List File [NUL.MAP]: NUL
Libraries [.LIB]: SLIBCEC.LIB 

LINK : error L2029: Unresolved externals:



_print in file(s):
 HW.OBJ(hw.c)

There was 1 error detected

In the final example the programmer left out the parentheses immediately after main.

main
{
	printf("hello, world\n");
}
The IBM 6150 compiler produced the following messages.
"hw.c", line 2: syntax error
"hw.c", line 3: illegal character: 134 (octal)
"hw.c", line 3: cannot recover from earlier errors: goodbye!
The SUN Sparc Station compiler produced the following messages.
"hw.c", line 2: syntax error at or near symbol {
"hw.c", line 2: unknown size
Compilation failed
Turbo C produced the following messages.
Error C:\ISPTESTS\HW.C 2: Declaration syntax error
The Microsoft C version 5.1 compiler produced the following messages.
hw.c
hw.c(2) : error C2054: expected '(' to follow 'main'
All of these messages are remarkably unhelpful and confusing except that from Microsoft C, particularly that from the IBM 6150 compiler.

You may find it interesting to try the various erroneous versions of the "hello world" program on your particular system. Do you think your compiler generates more helpful error messages?

If you are using Turbo C you will see the following message, even when compiling a correct version of the hello world program

Warning C:\ISPTESTS\HW.C 4:
   Function should return a value in function main
A warning message means that there is something not quite right with the program but the compiler has made assumptions that the compiler writer thought reasonable. You should never ignore warnings. The ideal is to modify the program so that there are no warnings, however that would introduce extra complications into the hello world program and this particular message can be ignored for the moment.

The message means that the user supplied function main() should return a value to the run-time support package. There is no requirement to do so and most compilers recognise main() as a special case in this respect. Returning a value to the run-time support package should not be confused with returning a value, sometimes known as an exit code, to the host operating system.


Standards and History