C Programming in 2021

by Ray Dall

################################################################################
Section 3
################################################################################

Now let us examine a more realistic program. One which most schools will use as their first program, but misses some of the fine points you just learned. The "Hello World" program.


Once again, type the following into nano (nano printing.c), then write it out ([CTL] X).
(You should be an old hand at this by now).

/* 
   NAME:	printing.c
   DESCRIPTION:	Teaches about compiler errors, inclusion, header files, and the standard input/output header.
   SYNOPSIS:	./a.out
*/
   int	main()		
   {
	printf("Hello World\n");
	return(0);
   }



Once you have typed this into your text editor and saved it as a .c file, and assuming you've made no mistakes, you have a fully functional C program. One small problem: Computers don't speak C - so you are going to have to compile (convert) it into an Assembly language program. This is done using the C compiler:

cc printing.c 



Note: If you typed this into nano properly, when you compiled it, you should have gotten an error message much like the one below.


$: > cc ./printing.c
./printing.c: In function "main":
./printing.c:6:2: warning: incompatible implicit declaration of built-in function "printf" [enabled by default]
  printf("Hello World\n");



Here is a good point to teach you a couple of acronyms.

     RTFM and RTFC

RTFM is an acronym for:

Read
The
FULL
Manual

This is referring to the MANual... or MAN pages in UNIX. (The language that C was originally written both for and in. When you have a question about a command (say the command printf) you merely type into a Unix terminal:


$: > man printf



and it will give you a full manual describing the command and its usage. More on this in a minute...

RTFC is an acronym for:

Read
The
Frustrated
Compiler

In this particular case, we frustrated our compiler by telling it to do something it wasn't equipped to do. Whenever you do that, the compiler barfs out a message telling you just how inadaquate you are, and usually giving you hints on how you can improve your code.

Let's RTFC and examine EXACTLY what it is telling us:


$: > cc ./printing.c
./printing.c: In function "main":
./printing.c:6:2: warning: incompatible implicit declaration of built-in function "printf" [enabled by default]
  printf("Hello World\n");



We typed:
   $: > cc ./printing.c

In the first line, it tells us which function (main) was at fault. Remember, we can have many functions in a single program.

In the second line, it told us that we "implicitly" declared a function.
   ./printing.c:6:2: warning: incompatible implicit declaration of built-in function "printf" [enabled by default]

You see, up until this point, you haven't written a "printf()" function, nor have you told the compiler where to find one that someone else has written.

Some compilers may be nice enough to even tell you where to find the printf() function, and how to change your code to fix it.


    warning: incompatible implicit declaration of built-in function "printf"
    note: include "<stdio.h>" or provide a declaration of "printf"
    #include <stdio.h> or provide a declaration of "printf"

If your compiler was not so nice as to give you such hints.... that's where the aforementioned acronym RTFM comes in handy.

Every Unix/Linux computer comes with built in MANUALS! (Users manuals, Programmer's Manuals, etc). If you READ the FULL MANUAL you will get all the answers, although they may be a bit terse and/or cryptic. To read a manual for a particular command, just type man.... and the command:

$> man ls



This will give you the "user manual" for the Unix/Linux command "ls" aka list. To exit the manual, simply type q for quit.

There are actually several different sections in the man pages. Not only is there a man(ual) page for each command - but some commands may have more than one man page. printf() is one of them. Why? Because it is both a USER command, and a function in C. If you look at " man printf ", you get the user's manual. If instead type in " man 3 printf ", you get the programmer's manual. Here is a list of the manual sections and their corresponding numbers:
    1      User Commands
    2      System Calls
    3      C Library Functions
    4      Devices and Special Files
    5      File Formats and Conventions
    6      Games et. al.
    7      Miscellanea
    8      System Administration tools and Daemons

So if we want to see the programmer's manual for the printf() command, we type:

$> man 3 printf



The 3 behind man tells your computer that you want the command from programmer's manual, rather than the user's manual. Again, you would type "q" to quit the manual.

Note that when you type "man 3 printf" into the shell, it gives you different information than simply typing "man printf". That is because there is both a user command printf and a C function printf().

Specifically what you are looking for, toward the top of the programmer's manual, is this:

SYNOPSIS
       #include <stdio.h>

       int printf(const char *format, ...);




The synopsis section of the manual tells you EXACTLY how to use the command. In this case, it tells you that you can't use it without INCLUDING the standard input/output header ( <stdio.h> ).

Note that it also tells you the proper syntax for the printf statement, including the type (int) identifier (printf) arguments, and endpunc. So let's go back into our program and add the line:

#include <stdio.h>

/* 
   NAME:	printing.c
   DESCRIPTION:	Teaches about compiler errors, inclusion, header files, and the standard input/output header.
   SYNOPSIS:	./a.out
*/

#include <stdio.h>

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



This now tells the compiler that when it compiles the program, to use the definition of the printf() function that was already predefined in the standard input/output header ( #include <stdio.h> ).

Now when we write and compile our program, it will compile without the compiler getting frustrated and complaining.
Then you can run the program by typing ./a.out

$> cc printing.c
$> ./a.out
Hello World
$>



The reason why it worked this time and not last time, is because just like you wrote a "printing.c" program that told the shell to print out "Hello World", someone long before you wrote a C program called "stdio.h", and one function in that program is a function called "printf()". Once we included his program as part of our program ( #include <stdio.h> ), it knew what to do with the printf and how it should work.

Interesting to note - if you RTFM (read the FULL manual) for printf(), you'll find that it is an int type (integer).

int printf(const char *format, ...);

This tells us that when it finishes, whether correctly or incorrectly, it returns an integer. If you RTFM, you'll see down at the bottom of the manual, that it has a section called "RETURN VALUE".

In that section it states, "If an output error is encountered, a negative value is returned."

What that tells us is that as long as printf() is successful, it will return a non-negative integer (0,1,2,3,4...) We can use this info later on to act as a type of error checking within a program. For now, it is enough to know that it does have a return, as do most functions in C - including nearly all that you will write.

One final note before we end this chapter. You might be wondering what the "f" is for. Why not just call it print? Simply put, because it prints "formatted" text, not just ascii characters. One of those formatis is the "escape" n ( or /n ) that we put at the end of our hello world. The escape symbol tells printf that the next character is a special character, in in the case of /n, that is the "newline" or carraige return. Another commonly used escape sequence is the /t, or tab. We will cover this in more detail in the next chapter.

################################################################################
1 2 3 4 5
################################################################################