HORIZON 2000 Computer Training Centre C - Getting Started
HORIZON 2000 Computer Training Centre C - Getting Started
C Getting Started
GETTING STARTED
WITH
C
1
www.horizon2000computers.com
C Getting Started
C Introduction Course
Lesson 1 Getting Started with c
Table of Contents
C Introduction Course.......................................................................................... 1
What is C and how to set it up............................................................................... 2
Setting up the code editor..................................................................................2
Setting up a compiler......................................................................................... 2
Getting the GCC command to work in any folder...............................................3
Windows 7 & Windows Vista & Windows XP....................................................3
Creating your first C program................................................................................ 5
Basic C commands and libraries............................................................................6
Notes on C programs.......................................................................................... 6
Constants Vs Variables.................................................................................... 7
Data Types and Constants..................................................................................9
INTEGER.......................................................................................................... 9
FLOATING POINT.............................................................................................. 9
DOUBLE........................................................................................................... 9
CHARACTER..................................................................................................... 9
Input Vs Output................................................................................................ 11
2
www.horizon2000computers.com
C Getting Started
S
C
M
o
o
a
m
u
c
p
r
h
ic
le
n
e
e
rC
o
C
d
o
e
d
e
Setting up a compiler
Compilers are dependent on the platform (computer) they are installed on. Today
there are few different platform combinations. We will limit ourselves to Linux
and Windows. C is already present in Linux but not in windows. An easy way
round it would be to install mingw which comes with gcc a windows c compiler.
3
www.horizon2000computers.com
C Getting Started
C:\MinGW\bin
1. Open the Control Panel's System icon; the keyboard shortcut is Win+Break (my favorite
keyboard shortcut). Win+Break is also the Win+Pause key, should your keyboard not show
the Break key.
2. If you're using Windows XP, skip to step 5.
3. In Windows 7 and in Windows Vista, choose Advanced System Settings on the left side of
the System window.
4. In Windows Vista, click the Continue button or type in the Administrator's password to get
by the UAC warning.
5. If necessary, click the Advanced tab in the System Properties dialog box.
6. Click the Environment Variables button.
7. Choose the Path variable from the bottom part of the dialog box, shown above.
8. Click the Edit button.
4
www.horizon2000computers.com
C Getting Started
The changes take effect with the next command prompt window you open. If you were
successful, then you can run the gcc command in any folder/directory at the command
prompt.
5
www.horizon2000computers.com
C Getting Started
4. Using the prompt navigate to where you saved your file and type the
following command
5. Gcc hello.c
6. You will note that a new program was created in the root folder with the
executable extension. (a.exe)
6
www.horizon2000computers.com
C Getting Started
printf("Programming in C is easy.\n");
}
Sample Program Output
Programming in C is easy.
Notes on C programs
In C, lowercase and uppercase characters are very important! All commands in C must be
lowercase. The C programs starting point is identified by the word main()
This informs the computer as to where the program actually starts. The brackets that follow
the keyword main indicate that there are no arguments supplied to this.
The two braces, { and }, signify the begin and end segments of the program. The purpose of
the statment #include <stdio.h>
is to allow the use of the printf statement to provide program output. Text to be displayed by
printf() must be enclosed in double quotes. The program has only one statement
printf("Programming in C is easy.\n");
printf() is actually a function (procedure) in C that is used for printing variables and text.
Where text appears in double quotes "", it is printed without modification. There are some
exceptions however. This has to do with the \ and % characters. These characters are
modifier's, and for the present the \ followed by the n character represents a newline
character. Thus the program prints Programming in C is easy. and the cursor is set to the
beginning of the next line. As we shall see later on, what follows the \ character will
determine what is printed, ie, a tab, clear screen, clear line etc. Another important thing to
remember is that all C statements are terminated by a semi-colon ;
Summary of major points so far
C Getting Started
C is case sensitive, use lower-case and try not to capitalise variable names
the curly braces {} define the beginning and end of a program block
Constants Vs Variables
Whatever constants do they remain constant an example is a numeric number
say 105. This constant will always have the same value, on the other hand a
number called Age can have different values throughout the program
C provides the programmer with FOUR basic data types. User defined variables
must be declared before they can be used in a program.
Get into the habit of declaring variables using lowercase characters. Remember
that C is case sensitive, so even though the two variables listed below have the
same name, they are considered different variables in C.
sum
Sum
int sum;
sum = 500 + 15;
printf("The sum of 500 and 15 is %d\n", sum);
It is possible to declare variables elsewhere in a program, but lets start simply and then get
into variations later on.
The basic format for declaring variables is
data_type
where data_type is one of the four basic types, an integer, character, float, or double type.
The program declares the variable sum to be of type INTEGER (int). The variable sum is then
assigned the value of 500 + 15 by using the assignment operator, the = sign.
sum = 500 + 15;
8
www.horizon2000computers.com
C Getting Started
Now lets look more closely at the printf() statement. It has two arguments, separated by a
comma. Lets look at the first argument,
"The sum of 500 and 15 is %d\n"
The % sign is a special character in C. It is used to display the value of variables. When the
program is executed, C starts printing the text until it finds a % character. If it finds one, it
looks up for the next argument (in this case sum), displays its value, then continues on.
The d character that follows the % indicates that a decimal integer is expected. So, when the
%d sign is reached, the next argument to the printf() routine is looked up (in this case the
variable sum, which is 515), and displayed. The \n is then executed which prints the newline
character.
The output of the program is thus,
The sum of 500 and 15 is 515
_
Control Formatters
newline
tab
carriage return
form feed
vertical tab
Variable Formatters
%d
decimal integer
%c
character
%s
string or character array
%f
float
%e
double
The following program prints out two integer values separated by a TAB
It does this by using the \t cursor control formatter
#include <stdio.h>
main()
{
int sum, value;
sum = 10;
value = 15;
printf("%d\t%d\n", sum, value);
9
www.horizon2000computers.com
C Getting Started
15
These are whole numbers, both positive and negative. Unsigned integers (positive values
only) are supported. In addition, there are short and long integers. The keyword used to
define integers is:
int
An example of an integer value is 32. An example of declaring an integer variable called sum
is:
int sum;
sum = 20;
FLOATING POINT
These are numbers which contain fractional parts, both positive and negative. The keyword
used to define float variables is:
float
An example of a float value is 34.12. An example of declaring a float variable called money
is:
float money;
money = 0.12;
DOUBLE
These are exponetional numbers, both positive and negative. The keyword used
to define double variables is:
double
An example of a double value is 3.0E2. An example of declaring a double variable called big
is:
double big;
big = 312E+7;
CHARACTER
These are single characters. The keyword used to define character variables is,
char
10
www.horizon2000computers.com
C Getting Started
Note the assignment of the character A to the variable letter is done by enclosing the value in
single quotes. Remember the golden rule: Single character - Use single quotes.
int
sum;
float money;
char letter;
double pi;
sum = 10;
money = 2.21;
letter = 'A';
pi = 2.01E6;
printf("value
printf("value
printf("value
printf("value
/*
/*
/*
/*
of
of
of
of
assign
assign
assign
assign
integer value */
float value */
character value */
a double value */
The following program illustrates how the different data types are declared and
displayed
#include <stdio.h>
main()
{
int
char
float
double
sum = 100;
letter = 'Z';
set1 = 23.567;
num2 = 11e+23;
11
www.horizon2000computers.com
C Getting Started
Input Vs Output
Output is normally done through the monitor, and it usually consists of an
instruction to the user. Our first program will be a helloWorld.c program. On the
other hand an input is when the user enters a value mainly through the
keyboard. Example
How old are you ? <Output>
28 <Input>
#include <stdio.h>
int main()
{
int agent;
char code;
printf("Enter your agent number:");
scanf("%d",&agent);
fflush(stdin);
printf("Enter your single-digit code key:");
scanf("%c",&code);
if(agent==7 && code=='B')
{
puts("Welcome aboard, James Bond.");
puts("You may commence with operation FreeCell.");
}
else
{
puts("The authorities have been notified");
puts("of this illegal access.");
}
return(0);
}
12
www.horizon2000computers.com