*

Site Map

ShoutBox!

-no content-

Author Topic: Programming - The Basics  (Read 605 times)

0 Members and 1 Guest are viewing this topic.

Offline Dave

Programming - The Basics
« on: October 31, 2005, 01:12:45 PM »
Basic Programming

All basic C or C++ programs start with an include statement; this grabs the local libraries where different commands are stored and translated. The standard library of commands is <stdio.h>. This contains commands that handle input, output and general processing directives. Different libraries include <time.h> and <stdlib>

A standard include statement would look like this:

#inlcude <stdio.h>

If you decided that your coded program would deal with system time calls and such then you may want to add a line to this statement, like this:

#include <stdlib>  --  If dealing in C++ then this library is referred to as <cstdlib.h>
#include <time.h>

All and any of these include statements must be at the complete beginning of the file, this is so that the program immediately knows what it is dealing with.

Following including the libraries, you need to begin the main section of the program this comes it the form of:

int main()

This statement begins the main block of the program where most of the coding will take place. Some people prefer to fill in the empty brackets with the word void but leaving it blank does the same thing and takes up less space when compiling (remember, every Kb counts, when you get compiling HUGE programs, you will want to keep the size down as much as possible)

There are hundreds of executions that can be put into any C program; I will run through a few of them:

printf(??);   - prints out a given piece of text or variable, this is put into the quotes inside the brackets

scanf(?%?, &name);   - this scans the keyboard and records whatever the user types. Works best with numbers or integers

fgets(stdin, 49, name);   - this records a string of variables whether numbers or letters and puts them into an array given in both the int variable and the fgets statement.


Now you have the include statements and have begun the main block of programming. To start putting your actual content into the program you will need to start and end the program block, to do this you need to use the curly brackets:

Code: [Select]
#include <stdio.h>

int main()

{

MAIN PROGRAM BLOCK WILL BE PUT IN HERE

}


Now I will go on to dictate and explain some simple programs that are primarily coded in the basic C language, not C++.

1.   Simply print out a word on the screen, this word is pre-coded into the program and is not changeable by the user.



Code: [Select]
#include <stdio.h>

Int main()

{
Printf(?Hello There?);
}


That quite simply prints out the words ?Hello There?, on the screen.


2.   Define two numbers in the code and print out the result of adding the numbers together on screen

Code: [Select]
#include <stdio.h>

int main()

{
int first_number;
int second_number;
int result;

first_number = 5;
second_number = 8;
result = first_number + second_number;

printf(?%d?, result);

}

If you require specific help then please post your request in the Programming Section.

Thank you, Dave  ;D
« Last Edit: November 02, 2005, 08:35:50 AM by Dave »