_________ SWAT MAGAZINE ISSUE TWENTY: AUGUST 1999 __________ / \___________________________________________/ \ / Programmation guide for beginner ( Part 1 ) \ / By Sir. Lestat UIN: 512262 \ ----------------------------------------------------------------------- This is the very first part of the guide. I hope to write a lot of them to teach everyone who want to learn C/C++. This part is on C basics. Before you begin, if you want to try out examples or code of your own you'll need a C/C++ compiler. I am using Microsoft Visual C++ 6.0 for DOS and Windows programs. I was using Borland Turbo C++ 3.0 for DOS which is good too, a lot smaller, have a good IDE with syntax highlight and really good for someone who begin. I also heard about LCC, which is free and supposed to be good. I never used it yet. Be carefull, in C/C++ everything is case sensitive. Let's begin with a very simple DOS program. You need a main function which is executed when you start the compiled program. It looks like this: void main ( void ) { // Put Code Here } Every functions are declared in the same way: ReturnType Name ( ArgType0 ArgName0, ArgType1 ArgName1, ... ) Name and ArgName are single word that identify the function and arguments ( Variable ) ReturnType and ArgType are identifiers to determine what type of value the function return and the argument contain. There is a list of differents types: void -> nothing char -> 1 byte character ( between 0 and 255 ) short -> 2 bytes number ( between -32768 and 327687 ) long -> 4 bytes number ( between -2147483648 and 2147483647 ) float -> 4 bytes floating point number ( between 3.4Eħ38 [ 7 digits ] ) double -> 8 bytes floating point number ( between 1.7Eħ308 [ 15 digits ] ) long double -> 10 bytes floating point number ( between 1.2Eħ4932 [ 19 digits ] ) So the main function declared as the above example is a no return no arguments function ( as void type means nothing ). Any number of functions can be declared in a single program. There is a little function that add two numbers: short add ( short number1, short number2 ) { return number1 + number2; } This function will return a short number ( 2 bytes ) which will be the sum of number1 and number2 that are the two arguments. "return" is a keyword to tell to return a value and exit the function. ( You only need to use it in non void return type functions ). Now you need to know how to call this function, let's put it in our main function. void main ( void ) { short sum; sum = add( 12, 17 ); } "short sum" declare a new two bytes memory space ( variable ) named "sum" to put anything you want to. You can assign them the result of any operations like: sum = 12 + 17; // sum now equal 29 sum = sum + 2; // Add 2 to sum or the return value of a fonction like the example above. You need to end each statements with ; to tell the compiler it is complete. So in this example sum will be equal to 29. To sucessfully call a function you need it to be declared before the function call. // are used to comment the code, everything after them on the same line is ignored by the compiler. You can also put comments between /* and */ anywhere in the code. void main /* this is my main function */ ( void ) { // There is where I put my code } is, for the compiler, identical as: void main ( void ) { } Functions, arguments and variables names must begin with a letter and can contain letters, numbers and underline character _ . There already are a lot of predefined functions that can be called to execute common tasks like I/O, math and strings functions. To use them you need to include their respective libraries. There is how to include stdio.h ( Standard I/O library header file ) #include Put this line at the top of your program. stdio contain printf, scanf functions ( and many others ) to print messages to the screen and to scan user keyboard input. There comes the classic Hello World program: #include void main ( void ) { printf( "Hello World !\n" ); } The printf function call in this example takes only one argument which is between " cause it's a string. The result should be "Hello World !" with a carriage return and line feed printed to the screen ( "\n" there are a lot of other special character that can be printed by using \, read the help file ). The printf function can have more than one argument. You can use it to print variables content with the % symbol follows by the format. short sum; sum = add( 12, 17 ); printf( "This is the sum of 12 and 17 -> %hd\n", sum ); printf( "This is the number 9 -> %hd\nThe sum of 2 and 5 -> %hd\nThe sum of 3 and " "6 -> %hd", 9, 2 + 5, sum( 3, 6 )); Screen output: This is the sum of 12 and 17 -> 29 This is the number 9 -> 9 The sum of 2 and 5 -> 7 The sum of 3 and 6 -> 9 %hd means you want to print a sHort number ( sum is declared as short ) and d is for decimal ( because you can print in numbers hexadecimal ). There are a lot more format type and options, for a complete reference check in the help file. You can print more than one variable in the same printf call as you see in the second printf call. String can be broken on more than one line if you close " at the end of the line and reopen them at the begining of the other. This is only one function call, it ends with the ; ( remember? everything need to ends with ; ) The scanf function works a like printf but you need to add a little something that makes scanf able to write in your variable. You'll now why later. You need to put & before the name of the variable. short my_number; printf( "Enter a number between -32768 and 32767: " ); scanf( "%hd", &my_number ); printf( "\n You wrote the number: %hd", my_number ); That will print "Enter a number between -32768 and 32767: " and then wait for user input followed by return. Then the program will change line "\n" at the begining of the laft printf and print "Your wrote the number: " followed by the number you entered. %hd in the scanf function call means the same thing that in then printf call. There is a complete last example for the part 1 of the guide. #include // Return the average of tree numbers. short average ( short number1, short number2, short number3 ) { short sum; sum = number1 + number2 + number3; return sum / 3; } // Main function void main ( void ) { short nb1, nb2, nb3; printf( "Enter the first number: " ); scanf( "%hd", &nb1 ); printf( "\nEnter the second number: " ); scanf( "%hd", &nb2 ); printf( "\nEnter the third number: " ); scanf( "%hd", &nb3 ); printf( "\n\nThe average of %hd, %hd, %hd is %hd", nb1, nb2, nb3, average( nb1, nb2, nb3 )); }