1. C PROGRAMMING
LESSON ONE: DESCRIPTION OF THE INTERFACE
1. Include files
The two lines #include stdio.h and # include stdlib.h permit files to be included in the project,
that is, to add files for compilation. These two lines are called pre-processor directives. They
will be read by a program called the pre-processor (a program that is loaded at the beginning
of the compilation). There are two include files. They are source files, that is, files which exist
already in the program. They contain prepared codes or ready-made codes (printf, scanf)
which are used to type text on the DEV++ environment. They are also called libraries. In
general, these two files contain libraries which enable text to be written on the screen.
2. Int main: this line contains the name of the function. It means the name of the function is
main. Main is the principal function of the program. The program always starts by the
function main. A function begins and ends with braces ({&}).
3. System pause: it is an instruction that asks the computer to pause the program. When the
program will reach this line it is going to display the message “press any key to continue”.
The user will have to press the key before the program goes to the next instruction.This
instruction only functions in a windows environment. If the system “pause” is omitted the
message on the console will appear and disappear at a very fast. U wil not even have time to
read the message.
4. Return 0: this means we are at the end of the function main and the program returns the
value to 0. Every program will have to return a value to indicate that everything is alright.
Most of the time this value is not used but it is good to return it. Any other value can be used
for example ‘error’.
5. So the structure of a function is as follows:
Intmain ()
{
System (“PAUSE”);
Return 0;
}
LESSON 2: How to Write a Message on Screen
We are going to write the message «good morning”. For this, we are going to use a special
instruction. This instruction is called printf. This instruction is found in the pre-processor directives.
These are directives made up of files which contain many ready-made codes ready to use. One of
these codes is printf.
LESSON 3: Special Characters
n: means go to the line
t: means tabulation
2. LESSON 4: Commentaries
If your commentary is just a line, it should be preceded by two slashes, that is, //. If it is going to be a
long commentary with paragraphs, it should be preceded with /* and end with */. Commentaries are
useful because they enable other users to understand your program and they guide the programmer
who is writing the source code.
LESSON 5: Variables
Here we are to know how to stock numbers in the memory (this will be very important for further
understanding) . There many different kinds of memory in the computer. The computer needs at the
same a very fast memory to fetch information very rapidly and a very large memory to keep a
significant amount of information. The different kinds of memory are the registers, the cache, the
RAM and the hard disc. Programming deals mostly with RAM. The RAM can stock numbers. An
address is a number that enables the computer to locate itself in the RAM. The number of addresses
depends on the number of RAMs that a user possesses. The more the Ram the more the addresses.
Each address can store a value which is still in the form of a number. An address can stock only one
number. A computer stocks numbers in the Ram so that it can easily remember them. The RAM can
only store numbers. If a computer stores the number , it is going to keep it in a memory space and
note the address where this number is stored. Lets say the address is 36489356262. When it needs
the number 5, it is going to find it in memory space n036489356262.
LESSON 6: Declaration of a variable
A variable is a temporary information that is stocked in the Ram. It is a value that can change in the
course of the program. In C language, a variable is made up of two things:
A value which is the number that it stocks
A name that enables it to be recognised
LESSON 7: How to name Variables
A variable should be made up of a letter or a number it has to start by a letter, it should not have
space and it should not be made up of special characters l ike.
LESSON 8: Types of variables
Integers which are positive whole numbers e.g 3,4,5
Decimals which are numbers with fractionse.g 3.5, 6.457
Negative whole numbers e.g -3,-4
Decimal negative numbers e.g -3.456
When stocking a number you have to tell the computer the type of the number you are stocking.
When you create a variable you have to tell the computer which type of variable it is. Char, int and
long enable to stock real numbers. Float and double are used to stock extremely large decimal
numbers. Char, int and long can be unsigned or signed. The unsigned types don’t stock ne gative
values but have the advantage of stocking numbers two times bigger( the biggest number in char is
128 while that of unsigned char is 255). Many types of variables were created to economise space.
3. When you ask the computer to give a ‘char’ variable you take less memory space than when you ask
it to give the ‘long’ type. Today it doesn’t really matter because computers are created with large
memories which can hold large amount of information.
LESSON 9: Declare a variable
Declaring a variable means asking the computer to use its memory. To declare a variable you use the
following steps:
Type the type of the variable
Press the space button
Write the name you want to give to the variable
And end with a semi colon
You declare a variable at the beginning of a function. If you have the same variable types to declare,
u can write them in one line and separate them with a coma.
LESSON 10: How to assign a value to a variable
U assign a value to variable using the ‘=’sign. U first declare a variable followed by an equal sign and
the value e.g long typesoflife=5. To change the value u can proceed as follows:
Long typeoflife=5;
Typeoflife=4;
Typeoflife=3;
In the above example, the variable typeoflife will first take the value 5, then and lastly 3.
LESSON 11: The value of a new variable
What is the value of the variable at the declaration stage? Is there a value by default when a variable
is declared? Well, when the variable is declared, it has no predefined value in the memory. A
memory space is reserved but the value inside that space doesn’t change. That variable will take the
value found in that space and this value can be anything(it could be a value from the former program
or 0).Since one is not sure of the value found in the memory space and to avoid confusion, it is
important to declare a variable and assign a value to it so that you can be sure of the value of the
variable.
LESSON 12: Constants
A constant is a variable that maintains the same value throughout the program. When declaring a
constant u write the word const in front of the variable type. It is an obligation to assign a value to it
at the moment of the declaration. It is preferable to use’ _’ in the place of ‘space command’. For
example: const long type_of_life=5.
LESSON 13:Display the content of a variable
To display the content of a variable, you use the function printf but a special symbol will be added to
the place were you want to display the value of the variable. For example:
4. Printf(il vous reste %ld vies);
This special symbol is % followed by ld. ld means the number to display is an integer (char,long, int).
Lf means u want to display a decimal(float, double). In the above instruction, we have indicated the
location where we want to display an integer but we don’t have the variable type.
Printf(“il vous reste %ld vies”, typeoflife)
LESSON 14 : display many variables in the same printf
LESSON 15: asking the user to type something in the console
To do this you use the function scanf. For example: scanf (“%ld”, &age). When the program reaches
scanf, it pauses and waits for the user to input a value. This value is stocked in the variable age. With
this function, the user can interact with the computer.
LESSON 16: Mathematical operations in C
1. Addition
The following code is used:
{long result=0;
Result= 5+3;
Printf(“5+3=%ld”, result;
}
The same applies for multiplication and subtraction. Only the sign changes.
2. Division
It is the same code which is written. The problem lies in the decimal places to be displayed
after the result. If u type 5/2 using the above format with a division sign, the result will be 2.
Whereas 5/2 is 2.5. so when typing with the division sign, u wil l have to register the numbers
as 5.0 and 2.0, use double instead of long and replace %ld by %lf to have the appropriate
answer.
3. Modulos
It is calculated using the % sign.
5. LESSon 16: operations with many variables
The static list is implemented using arry which is the container with the capacity which is the
maximum number of items with the number of items. To be able to answer such questions
there should be a variable to be able to count the maximum number of items. Before
inserting in a list u have to cheack if the list is full but with the dynamic list u don’t need to
check if the list is full bcos the size is not limited. A list is empty if the pointer if NULL. In static
usx an array to know that it is empty the no of variables is =items. To model the list u can use
a record an array, capacity and number of items describes a static list whic is the record. To
define u write; typedef struct static_ list{
Int array(container){30}