Introduction to C Programming - Program Layout

Chapter chap1 section 5

The hello world program listed at the start of this chapter is laid out in accordance with normal C conventions and practice. The C programming language is "free format" which means you can lay out the text of programs in whatever form takes your fancy provided, of course, you don't run words together. The "hello world" program could have been written in this form

main(){printf("hello, world\n");}
or even this
main
(
)
{
printf
(
"hello, world\n"
)
;
}
Neither of the above is recommended.

The indentation of the word printf in the original version of the hello world program is achieved by the use of a single TAB character not a sequence of spaces. TAB characters may be generated by hitting the TAB key on normal keyboards, occasionally the TAB key carries a mysterious symbol consisting of 2 arrows. Multiple TAB characters should be used to indent C programs in preference to sequences of spaces. The use of TABs makes for smaller source files, less typing and tidier layout. All C compilers understand the use of TABs. Unfortunately some word processors and editors have strange ideas about standard TAB settings, for C source TABs should be set every 8 characters.

All but the briefest programs will benefit from descriptive comments included in the source. In the C programming language anything enclosed between the character pair /* and the character pair */ is treated as a comment. The compiler treats any comment, however long, as if it were a single space. Comments may appear in any reasonable place in the source. A commented version of the "hello world" program appears below.

/*	This is the first C program

	Author	: J.P.H.Burden
	Date	: 22 September 1992
	System	: IBM 6150 + AIX Version 2.1
*/
main()
{
	printf("hello, world\n");
}
Comments may stretch over several lines but may not be nested, i.e. you cannot include comments within comments. This can cause difficulties if you wish to "comment out" parts of a program during development. The following use of comments is perfectly correct.
main()
{
	printf("hello, world\n");
/*	
	some very clever code I haven't
	quite sorted out yet.
*/
        printf("the solution to the problem of \
life, the universe and everything is");
/*	
	more very clever code still to be
	developed
*/
}
But the following attempt to "comment out" further parts of the program.
main()
{
	printf("hello, world\n");
/*
	OK let's stop pretending we can write
	really clever programs
	/*	
		some very clever code I haven't
		quite sorted out yet.
	*/
		printf("the solution to the problem of \
	life, the universe and everything is");
	/*	
		more very clever code still to be
		developed
	*/
*/
}
Gave the following compiler error messages when using the SUN Sparc Station compiler.
"comm2.c", line 17: syntax error before or at: /
Compilation failed
The error message refers to the final "*/" in the program.


Program Errors