HORIZON 2000 computer training centre
C Getting Started
GETTING STARTED
WITH
C
1
www.horizon2000computers.com
HORIZON 2000 computer training centre
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
HORIZON 2000 computer training centre
C Getting Started
What is C and how to set it up
C is a programming language. A programming language is a set of syntax
(English like) used by programmers to write programs. A tool referred to as a
compiler will transform this program from text (source code) to binary (machine
code).
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 the code editor
Any text tool can be used as a code editor. I suggest notepad ++ or
programmers note pad as these have syntax highlighting and other features that
can simplify the process of reading the code in c or other languages.
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
HORIZON 2000 computer training centre
C Getting Started
Getting the GCC command to work in any folder
The best way to get the gcc command to work in any folder on your hard drive is to place the
bin folder onto the Windows search path. This can be complex, so pay attention!
First you need to know where the
folder is located, the bin folder's path. If you've
installed MinGW according to its wont, then the path to bin may look like this:
bin
C:\MinGW\bin
Second, you need to modify the
environment variable in Windows so that Windows
knows where to look for GCC and its companion commands.
path
Windows 7 & Windows Vista & Windows XP
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
HORIZON 2000 computer training centre
C Getting Started
9. Click in the Variable value text box.
10. Press the End key on your keyboard so that you may add text to the end of path.
11. Type a semicolon, ;
12. Type the path to the BIN folder, as in:
;c:\mingw\bin
13. Double-check! There should be no spaces in there.
14. Click OK to close the Edit System Variable box.
15. Click OK to close the Environment variables box.
16. Click OK to close the System Properties window.
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
HORIZON 2000 computer training centre
C Getting Started
Creating your first C program
Open your note pagd. I will assume you have developers notepad installed.
1. Click file -> Click new ->Select c /C++
2. Type the following code and save to a known directory:
Ex: G:\C course\Lesson 1\hello1.c
#include <stdio.h>
int main()
{
printf("Hello World!\n");
return 0;
}
Note all c programs should be named with the .c extension.
3. Run the command: Go start -> run and type cmd
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
HORIZON 2000 computer training centre
C Getting Started
Basic C commands and libraries
From now on we will have few programs that will show us how to make basic
commands in c.
#include <stdio.h>
main()
{
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
program execution begins at main()
keywords are written in lower-case
statements are terminated with a semi-colon
text strings are enclosed in double quotes
7
www.horizon2000computers.com
HORIZON 2000 computer training centre
C Getting Started
C is case sensitive, use lower-case and try not to capitalise variable names
\n means position the cursor on the beginning of the next line
printf() can be used to display text to the screen
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
The declaration of variables is done after the opening brace of main(),
#include <stdio.h>
main()
{
int sum;
sum = 500 + 15;
printf("The sum of 500 and 15 is %d\n", sum);
Sample Program Output
The sum of 500 and 15 is 515
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
var, var, ... ;
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
HORIZON 2000 computer training centre
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
_
Some of the formatters for printf are,
Cursor
\n
\t
\r
\f
\v
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);
Program output looks like
9
www.horizon2000computers.com
HORIZON 2000 computer training centre
10
_
C Getting Started
15
Data Types and Constants
The four basic data types are
INTEGER
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
An example of a character value is the letter A. An example of declaring a character variable
called letter is:
char letter;
letter = 'A';
10
www.horizon2000computers.com
HORIZON 2000 computer training centre
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.
Sample program illustrating each data type
#include < stdio.h >
main()
{
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 */
sum = %d\n", sum );
money = %f\n", money );
letter = %c\n", letter );
pi = %e\n", pi );
Sample program output
value of sum = 10
value of money = 2.210000
value of letter = A
value of pi = 2.010000e+06
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;
printf("Integer variable is %d\n", sum);
printf("Character is %c\n", letter);
printf("Float variable is %f\n", set1);
printf("Double variable is %e\n", num2);
}
Sample program output
11
www.horizon2000computers.com
HORIZON 2000 computer training centre
C Getting Started
Integer variable is 100
Character variable is Z
Float variable is 23.567000
Double variable is 11.000000e23
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