Pages

Wednesday, 1 March 2017

Getting Started with C Programming

In the last article in this series, I gave a quick overview of the C programming language and showed how to write a very simple program. Here I will explain how to pass arguments to your programs and display formatted strings.

Passing Arguments

To pass values to the program, you can just run the program at the command prompt and put any arguments after the name of the program itself, with spaces between each item. For example, if I wanted to pass the arguments “hello” and “world” to a program called HelloWorldArgs.exe (on Windows) or HelloWorldArgs.app (on OS X) I would enter this at the command prompt or Terminal:

HelloWorldArgs hello world

We’ll assume that the program has a main() function with argc and argv arguments like this:

int main(int argc, char **argv)

My program ‘receives’ the two bits of data (the strings “Hello” and “world”) which I entered after the program name itself and it stores them in the second argument, argv. The first argument, argc is an automatically calculated value that represents the total number of the arguments stored in argv. This is the program code:

int main(int argc, char **argv) {
int i;
for (i = 0; i < argc; i++) {
 printf("Hello World! argc=%d arg %d is %s\n", argc, i, argv[i]); }
return 0;
}

When I pass the program the two arguments: hello and world, this is the output which is displayed:

Hello World! argc=3 arg 0 is 03_HelloWorldArgs
Hello World! argc=3 arg 1 is hello
Hello World! argc=3 arg 2 is world

This shows that the count (argc) of arguments is 3 even though I have only passed two arguments. That’s because the program name itself, HelloWorldArgs, is automatically passed as the first argument. The first argument here has the index number 0. The arguments at index 1 and 2 are the arguments that I passed to the program: hello and world.

Note: the two asterisks before argv are important:

char **argv

They indicate that argv is a list of strings. Strictly speaking argv is an ‘argument vector’ or a pointer to an array of character-string arguments.

The block of code that starts with the keyword for is a loop that causes the code that follows it, between the curly braces, to execute for a certain number of times. Here the code executes for the number of times indicated by the value of the argc argument). The printf statement prints the string "Hello World! argc=%d arg %d is %s\n" and it substitutes the values of argc, i, argv[i], at the points marked by %d, %d and %s in the string. At each turn through the for loop the string at the index i in the argv array is printed.

puts and printf

There are several functions that can be used to display (print) information when your C programs run. Both printf and puts, can display a simple string.

printf("hello world\n");
puts("hello world again\n");

The printf function also allows you to embed ‘format specifiers’ into a string. A format specifier begins with a % and is followed by a letter: %s specifies a string. %d specifies a decimal or integer. When format specifiers occur in the string, the string must be followed a comma-delimited list of values. These values will replace the specifiers in the string. The programmer must take care that the values in the list exactly match the types and the number of the format specifiers in the string otherwise the program may crash. Here is an example:

printf("There are %d bottles standing on the %s.\n", 20, "wall\n" );

When run, the code produces the following output:

There are 20 bottles standing on the wall

Comments

It is a good idea to add comments to your programs to describe what each section is supposed to do. C lets you insert multi-line comments between pairs of /* and */ delimiters, like this:

/* This program displays any 
 * arguments that were passed to it */

In addition to these multi-line comments, modern C compilers also let you use ‘line comments’ that begin with two slash characters // and extend to the end of the current line. Line comments may either comment out an entire line or any part of a line which may include code before the // characters. These are examples of line comments:

// This is a full-line comment

for (i = 0; i < argc; i++) // this comment follows some code




This series of C programming lessons is based on my book, The Little Book Of C, which is the course text for my online video-based course, C Programming For Beginners, which teaches C programming interactively in over 70 lessons including a source code archive, eBook and quizzes. For information on this courses see HERE.