Home > Articles

A Word on C

  • Print
  • + Share This

Comment Tokens

Use of the comment token recognized by the C language starts our example:

0: /* the start of the program */

The slash asterisk (/*) combination begins the comment and an asterisk slash (*/) ends it. Anything contained within the start and end comment tokens is ignored by the compiler and does not get placed in the object file. The GNU C Compiler also accepts the token slash slash (//) for support of a comment contained on a single line:

// everything after this comment token is ignored

The Function main

Continuing the discussion of the code sample, look closely at the definition of the function main:

1: int main( int argc, char *argv[ ] )

Because the compiler requires the function main, it does not need a prototype (declaration) before its definition. The compiler expects the function and therefore dictates its parameter list and return type.

EXCURSION

A Syntax for Representing Arrays of Strings

The declaration of the variable argv uses a syntax that you've only seen in part.

char *argv[ ];
Use of the char data type as a pointer (char *) is for storing a string or group of characters.

char *str = "abcdefghij";

Further, the syntax of following a variable name with square brackets indicates that the variable is an array (argv[ ]) (see the section "Data Types" in Chapter 2, page 70 for a review of arrays).

Defining a variable as

char str[10];

requests the compiler create an array with enough space in memory to hold 10 characters.

str[0] = 'a';
str[1] = 'b';
.
.
.
str[9] = 'j';

Combining the two in a single declaration as with argv from the example, we see

char *strArray[3];

which indicates that strArray is an array of strings as in

strArray[0] = "abc";
strArray[1] = "def";
strArray[2] = "ghi";

Reviewing the definition of main, you see that many elements have already been discussed.

Identifying the data types in the function definition

1: int main( int argc, char *argv[ ] )

reveals the return type of the function main is the data type int. Further, main expects two parameters, an int named argc and an array of char * (character pointer) called argv.

EXCURSION

Compiler Differences Affecting the Declaration of the Function main

The C compiler used imposes the return type and parameter list of the function main. The arguments argc and argv are consistent with all C compilers. These parameters enable the program to access the command-line parameters the user passes.

For instance, in Chapter 1 we used man to illustrate passing flags and parameters to UNIX commands:

bash[1]: man ascii
With the execution of the man command, the operating system responds by calling the function main defined by the program's author. When main is entered, the first parameter, argc, specifies the number of arguments placed on the command line when the user invokes the program and argv holds the value of each of them.

Invoking man ascii results in the value of argc being set to 2 where argv[0] contains man and argv[1] ascii. A way of representing this is

char *argv[ ] = { "man", "ascii" };

Notice that in this example, argv contains two elements, which correspond to the value of argc.

The return type expected from main is not consistent between compilers. Unlike Linux, most SYSV versions of UNIX allow a return type of void when defining main.

A return type is beneficial because it gives you the ability to test for the program's success or failure. By convention, a program exits with a value of 0 to indicate that no errors occurred. If the program fails to complete, a value greater than 0 is generally returned.

Think of argc as the argument count and argv as the argument values.

 

  • + Share This
  • 🔖 Save To Your Account

Related Resources

There are currently no related titles. Please check back later.