First

There are numerous websites and books that do an admirable job of teaching the C language. This section of CodePlain is intended to give a brief introduction to the C language by way of some very plain examples that can be typed using your favorite text editor and then compiled using any available C compiler.

No attempt is made to give complete explanations concerning the full range of possible uses of statements and functions presented. But, understanding these example programs will lay the foundation for a deeper understanding of C.

First  |  Print

Your First C Program

It could be argued that this program doesn't do anything. But, in fact, successfully compiling and running this program is a significant first step into the world of C programming. Assuming that you are new to C coding, it is advisable that you don't bother with the meaning of any of the code in this example. Just accept it as the minimum code for any and all C programs you write and you will be at peace with the world of C.

C is case sensitive. This means, for example, that C does not consider 'main' and 'Main' to be the same. An easy way to see for yourself is by using 'Return' instead of 'return' in this program. You should get a compile error when you code 'Return'.

int main(int argc, char *argv[])
{
  return 0;
}
Top

printf

The printf function is used in these example programs to allow the programmer to 'see' what the program is doing. The printf function also acts as a placeholder which should be replaced with meaningful code when writing your programs.

The printf function is one of many functions that can be found in the C Standard Library. There are numerous references to this library on the web that can be easily located using your favorite internet search engine.

The printf function declaration is in the stdio.h header file, so stdio.h must be included into the program using the #include statement.

#include <stdio.h>

int main(int argc, char *argv[])
{
  printf("Hi");
  return 0;
}

To get a new line of output, add a newline character which is defined in C by using "\n".

#include <stdio.h>

int main(int argc, char *argv[])
{
  printf("Hi\n");
  printf("\n");
  printf("Bye\n");
  return 0;
}

The printf function actually has two parts: a format specification and a variable argument list. In the above examples the format specification is just a hard coded message. In this example, in addition to a hard coded message there are format specifications. Some common format specifications are:

%d - integer
%f - floating point
%c - a single character
%s - multiple characters
%x - hexadecimal

In this example, variable Count is defined as an integer (int Count;) and then initialize to a value of 7. The %d in the printf statement is used to format Count as an integer. The second printf has two %d's and two corresponding references to Count.

#include <stdio.h>

int main(int argc, char *argv[])
{
  int Count;

  Count = 7;
  printf("The count is: %d\n", Count);
  printf("The count is: %d Yep yep, %d is the count!\n", Count, Count);
  return 0;
}
Top