Home
About
Meetings Program
 and Reports
Location
Contact Us
Links
Blog
Headlines List

Page updated Wednesday
29th 2004f December 2004
23:10:57 UTC
Now 16:41:22 UTC
Hits:601


  Programming continued - Knoppix 3.6 from a "live CD" and getting a simple test program to work.

Date:December 3, 2004
Time:7:00 pm.
Place:WEA, 223 Angas Street, ADELAIDE
Presenter:SAMG Members

C PROGRAMMING EXAMPLES

Here is the program example called ADD.C that we were discussing at the December meeting. We used the Knoppix 3.3 Linux "live" CD operating system, wrote it with the Kwrite editor, compiled it with the gcc compiler on the command line of a console and ran it from the command line of a console.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
  printf(“%d\n”,atoi(argv[1])+atoi(argv[2]));
  exit(0);
}

In the example, we used the function main()as the starting point because all executable C programs must contain a function called main() to tell the compiler where to start. If there is no main()then the program is incomplete and must be part of a program with main() somewhere else - for example, library functions will not have a main(). Programs without a main() will still compile but won’t run by themselves. We want ours to run.


Another feature of main() is the “int” that occurs immediately in front of the function name. The “int” stands for “integer” and tells the compiler that the function main() will return an integer to the calling program (the operating system in the case of main()).


In the Linux compiler that we are using “int” is a 32 bit value. Other “types” that are used in C are “char” for character (8 bits), “long” for 32 bit integer numbers (same as int for this compiler), “float” for floating point numbers, and “double” for double precision floating point numbers. These are the basic types and are used by themselves or to construct other types. They also have further qualifiers such as “signed” and “unsigned” or “long” and “short” so that you could have, say, “short int” for a 16 bit integer or even "long long " for a 64 bit integer. The actual interpretation depends on the particular implementation of the C compiler you are using. We will look closer at types in future.


While talking about functions it should be noted that functions can only return single values or even none at all (in the case where no value is returned the type is void). Some functions only need to return an answer such as some maths function like squareroot, sqrt(). Other functions may return a single value indicating status such as an error code. Where more than one value is needed in a result such as a string or an array then the return value may be a pointer to the string or array. This takes a bit of getting used to if you are more familiar with languages that use functions for single value returns just like C, but use subroutines that can return more than one value such as Pascal, Fortran, etc. C does not have subroutines.


It is worth mentioning that these other languages often “nest” subroutines ie. they can include the code for subroutines within subroutines. C does not nest functions within functions. There is no need – you list all functions separately, but if required you simply call any other functions from within functions instead, which effectively does what nesting subroutines does.


The next features of the main() statement are the arguments within the round brackets. All functions must include brackets whether they have actual arguments or not. ie. the brackets may be empty, but the brackets must still be there).


In this example the arguments are returned from the command line.


The “argc” is the count of parameters returned from the command line that has the type “int” or integer.


The “argv” is the name of the array of strings that are returned from the command line. The square brackets combined with the “char” type indicate that each argv is a string of characters. The asterisk is the pointer indicator. So we have “argv” as the name of an array of strings of characters. This introduces pointers and arrays that I hope will be covered in much more depth in future meetings. Pointers and arrays can be very complex so don’t be phased if you don’t follow what’s happening just yet. It is pointers that make the C and C++ languages so powerful.


It is worth mentioning that Java looks a lot like C and C++, but without pointers. Sun and James Gosling who wrote it for Sun advocate Java over C and C++ because they say, without pointers, Java source or its intermediate pseudo-code can be easily ported unaltered to different platforms and different operating systems whereas C and C++ can’t. Then alternatively C and C++ compiled programs run faster than almost anything except very tight assembly code and can drive hardware, usually as part of the operating system or as a driver while Java can’t. Also, the Java compiler and pseucode interpreter itself is written in C or C++, so who wins that one? Well, the answer is “the one that best suits the application at hand”.


Next we have a sequence of statements enclosed in curly brackets that form the main() program.


The first statement is a print function, or “printf()”, that is one of the most commonly used and complex functions in the C language. You can look at its complexity by listing its description with the command line:


man printf


from a Linux console.


When you look up the function it will show its “prototype” that describes its type and the parameters it accepts in the round brackets as well as their type and description.


There is another important item shown in any functions’ description and that is the “header” that defines all of the constants, variables, other functions, in fact, anything that you want to predefine to avoid having to do so each time you use the function. The header used by printf() is stdio.h. It precedes the function that uses it. Usually all #includes are listed at the top of a program. In this case the header is a standard one and is found in the include directory so it is enclosed in angle brackets < >. If we write our own header and include it in the working directory it will be enclosed in double quotes " ".


In our case the format of the printf() function’s output is contained in the quotes indicating that we want to print a decimal number, “%d”, followed by a newline, “\n”.


The decimal value that we will print is the result of the next bit of code. We are adding the two arguments from the command line. However, the arguments are ASCII strings (even though they may look like numbers). The arguments from the command line are indicated as “argv[1] and “argv[2]”. Because they are strings we convert them to numbers using the ASCII to integer function “atoi()” first then add them with “+”. A lot of work in one line, but that is the sort of brevity (or obtruseness) you can achieve with C.


The atoi() function requires the header stdlib.h.


If you want to take things a bit slower you can substitute the printf() line with the lines. (BASIC programmers may prefer this method)


int a, b, c;


a = atoi(argv[1]);

b = atoi(argv[2]);

c = a + b;

printf(“%d\n”,c);


Lastly, there is the exit() function. This returns the value within the round brackets. If you look up the manual using:


man exit


the value in the round brackets is returned to the operating system. In this case a zero has been chosen to indicate normal program completion.


You will note that the include header reference for exit() is stdlib.h that we have already used for the atoi() function so we don’t have to include it again.


You can now compile the program with:


gcc add.c


to get an executable file a.out that you can run using, say,


./a.out 23 41


resulting in the answer


64


An alternative for compiling the program that gives an executable output file with another name other than a.out is:


gcc add.c –o add


This version of the program is run using:


./add 51 34


resulting in the answer


85


Here are a few more things to consider.


What if there are no arguments in the command line other than the program’s name? What if there is only one argument following the program’s name? What if there are more arguments than 2? What if one or more of the arguments are not integer numbers, but are alpha characters, or floating point numbers, or are too long to be integers, or are a mixture of alpha and number characters? You should be doing error checking. There are a few more things to look at in future.


C++ PROGRAMMING


Here is an example showing that C++ is really just an extension to the original C language.


This is the well known “Hello World!” test program used by everyone to see that most things are operational, but written in C to start with and called hello.c.


#include <stdio.h>


int main()

{ printf(“Hello C World!\n”);

return(0);

}


Try compiling this with the C compiler gcc with the command line

gcc hello.c

and run it with

./a.out


Now try compiling with the C++ compiler g++

g++ hello.c

And run it with ./a.out They should both work.


Now let us add some C++ code


#include <stdio.h>

#include <iostream>


int main()

{ printf(“Hello C World!\n”);

std::cout << “Hello C++ World” << std::endl;

return(0);

}


If you attempt to compile that with gcc you will get a heap of errors. Try compiling with g++ instead which should result in no errors, and then run the result.


This uses two methods in the class std. Because the std class is used so often, it can be declared with the “using namespace std;” statement. The code becomes, by including the namespace statement and omitting the class parts:


#include <stdio.h>

#include <iostream>


using namespace std;


int main()

{ printf(“Hello C World!\n”);

cout << “Hello C++ World” << endl;

return(0);

}


You can see from that you can mix C and C++ statements more or less without a problem provided that you use the g++ compiler and use the correct C and C++ syntax.



C++ MANUAL


The December issue of Australian Personal Computer (APC) have two electronic books on C++ programming (and other languages as well). The files are a bit hard to find on the DVD, so look carefully.


Good luck,


Rick Matthews