2. Compiling a program
• The typical process for creation of a program is
– Editing the source code using a text editor
– Compilation of source code resulting in object code
– Linking of any required external libraries or code which results in an
executable file.
– This file can be executed by the OS
2
source file linkercompiler
source
file
object
file
executable
file
Included Libraries
3. The Command Line
• A command-line interface (CLI) is a mechanism for interacting
with a computer operating system or software by typing text
commands to perform specific tasks.
• The commands that are executed are themselves small programs
that together make up the operating system.
– Eg. Copy, Del, Move
• The command prompt represents a position in the file system of
the computer
• Some useful command for navigating the file system:
– cd is change directory (cd .. goes down one directory)
– dir gives you a list of all the folders and files in the current folder
• If you type the name of an executable file that is in the same path
as the command prompt the OS will run that program
3
4. Compiling a program
• Originally programs were edited using a text editing program and
then compiled using the command line interface to run the
Compiler and provide it with the source code file that needs to be
compiled
– Eg. C:>gcc simple.c –o output.exe
• The most popular way to create programs is to use an Integrated
Development Environment (IDE)
– Single program that provides the functions of a text editor, compiler and
debugger.
– For C/C++ development Dev-C++ and Code::Blocks are popular free IDE’s
4
5. Four steps to writing a C program
1. Typing in the code in a text window and saving the
resulting text as a separate file (source file). The text of a
C program is stored in a file with the extension .c for C
programming language
2. Compiling the code produces an intermediate object file -
with the extension .obj or .o. The source file must be
translated into binary numbers understandable to the
computer's Central Processing Unit This process
3. Linking the code to include library routines (routines
written by the manufacturer of the compiler to perform a
variety of tasks, from input/output to complicated
mathematical functions). In the case of C the standard
input and output functions are contained in a library
(stdio.h) so even the most basic program will require a
library function. After linking the file extension is .exe which
are executable files
4. Executing (running) the code. Run .exe files as you can
other applications, simply by typing their names at the
DOS prompt
5
6. Pass 1: Quick Synopsis
• The first line tells the computer to include information found in the
file stdio.h, which file is part of the C compiler package:
• #include <stdio.h> <= Include another file
• C programs consist of one or more functions, which are the basic
modules of a C program.
• This program consists of one function called main.
• The parentheses identify main() as a function name:
• main() <= Function name
• You can use the symbols /* and */ to enclose Comments, which
are remarks to help clarify a program. They are intended only for
the reader and are ignored by the computer:
• /* a simple program */ <= Comment
• // comment <= Single line comment
6
7. Pass 1: Quick Synopsis
• The opening brace { marks the start of the statements that make
up the function.
• The function definition is ended with a closing brace }
• { <.=Beginning of the body of the function
• The declaration statement announces that we will use a variable
called num and that num is an integer:
• int num; <= Declaration statement
• The assignment statement assigns the value 1 to num:
• num = 1; <= Assignment statement
• C statements all use the semi-colon ; to terminate!
7
8. Pass 1: Quick Synopsis
• The following line prints the phrase within the quotation marks (I
am a simple).
• printf(”I am a simple “);<= Print statement
• The following line prints the word computer to the end of the last
phrase printed. The n tells the computer to start a new line:
• printf (“computer .n”); <= Another print statement
• The following line prints the value of num (which is 1) embedded
in the phrase quotation marks. The %d instructs the computer
where and in what form to print n value:
• printf(”My favourite number is %d because It is first.n”, num);
• As noted earlier, the program ends with a closing brace:
• } <= Program end
8
9. Pass 2: Details
• #include Directives and Header Files
– The stdio.h file is supplied as part of a C compiler package,
and it contains information about input and output functions
(such as printf (for the compiler to use:
• #include <stdio.h>
– The name stands for standard input/output header.
– C users refer to a collection of information that goes at the top
of a file as the header, and C implementations typically come
with several header files.
• The effect of #include <stdio.h> is the same as if we were to copy
the entire contents of the stdio.h file into our file at the position the
line appears.
• Such #include files provide a convenient way to handle
information that is required by many programs.
• Note the use of the angle brackets (< and >) around the header's
name. These indicate that the header file is to be looked for on
the system disk which stores the rest of the C program application9
10. Pass 2: Details
• The main( ) Function
• All C programs will consist of at least one function, but it is usual
to write a C program that comprises several functions.
• The only function that has to be present is the function called
main.
• For more advanced programs the main function will act as a
controlling function calling other functions in their turn to do the
dirty work
• The main function is the entry point into the program and is the
first function that is called when your program executes.
• The parentheses following a function name generally enclose
information being passed along to the function. For our simple
example, nothing is passed along with main( ), so the parenthesis
remain empty.
• The int indicates the return data type from the main function.
10
11. Pass 2: Details
• Comments
• Using comments makes it easier for someone (including yourself)
to understand your program by adding descriptions.
• Everything between the opening /* and the closing */ is ignored by
the compiler:
– /* a simple program */
• // indicates a single line comment. Anything appearing in a line
following // will be ignored by the compiler
– int num; //this will be ignored.
11
12. Pass 2: Details
• Braces ({,}) mark the beginning as well as the end of the body of
a function:
• {
• .
• .
• .
• }
• Braces also mark the beginning and end of blocks of statements
within flow constructs such as if, for, while and switch constructs.
12
13. Pass 2: Details
• Declarations
• The declaration statement is one of the most important features of
C.
– As noted earlier, our example declares two things:
– int num;
• First, somewhere in the function, we use a variable having the
name num.
– In C, all variables must be declared, which means you must
list all the variables you use in a program, and you must show
what “type” each variable is.
– Second, int proclaims num as an integer, that is, a whole
number without a decimal point.
• The word int is a C keyword that identifies one of the basic C data
types.
• Keywords are specific words used to express a language; they
may not be usurped for other purposes.
13
14. Pass 2: Details
• The compiler uses the information in the declaration statement to
arrange for suitable storage space in memory for the num
variable.
• The semicolon at the end of the line identifies the line as a C
statement or instruction.
• Data Types
• C deals with several kinds (or types) of data: integers, characters,
and floating point numbers for example.
• Declaring a variable to be an integer or a character type makes it
possible for the computer to store, fetch, and interpret the data
properly.
• An integer and a single precision floating point variable are
represented in memory using the same number of bits but the
compiler knows how to interpret those bits to represent:
– a integer ranging from -32768 to 32767
– or a float ranging from 3.4x10-38 to 3.4x10+38
14
15. Pass 2: Details
• Name Choice
• Use meaningful names for variables.
• The number of characters you can use varies among
implementations.
• To name variables you may use lowercase letters, uppercase
letters, digits, and the underscore (_), which is counted as a letter.
• The first character must be a letter. The following are some
examples:
15
Valid Names Invalid names
Squiggles &^#Z**
cat1 1cat
Human_names Human-names
_iCounter Don’t
16. Pass 2: Details
• Assignments
• The assignment statement is one of the most basic operations.
– num = 1;
• In our example, the assignment statement means “give the
variable num the value of 1”.
• Note that the assignment statement, like the declaration
statement, is completed with a semicolon.
16
17. Functions
• A function contains a set of instructions that are executed
whenever that function is called.
• To call a function you need only to use its name followed by
brackets
– Eg printf();
• It is possible to send data to a function by making use of the
parameters defined by the function. These parameters will be
places between the brackets following the function name.
– Eg. sum(2,5)
• Defining parameters in a function are optional.
• A function can also return resulting value but this is not required.
17
18. Printf()
• The function writes a sequence of data to the standard output.
• The text sequence to be written is passed to the printf() function
via its first parameter.
• A string of text must be bounded by “ ”
• There are special types of characters that affect how this string is
printed to the screen
• Escape sequence characters represent characters that are not
possible to type on a keyboard but are understood by a computer
such as tabs and newline characters.
• These characters are preceded with a
• Example
– n represents a new line
– t represents a tab
18
19. Printf()
• The sequence can be formatted using a set of specifiers
– %d or %i Decimal Signed Integer
– %f Decimal floating point number
– …many more
• These specifiers are placeholders that tell the printf() function
where to print the value of variables specified by the parameters
that follow the first control string parameter.
• Printf(“This is an integer %d, this is a double %f.”,12, 12.55);
– This is an integer 12, this is a double 12.55.
• Can specify formatting of numbers
– Printf(“This is an integer %3d, this is a double %4.3f.”,12, 12.55);
– This is an integer 012, this is a double 0012.550.
• http://www.cplusplus.com/reference/clibrary/cstdio/printf/
19
20. Libraries
• For the purpose of modularity it is possible to create a source
code file that contains a collection of functions that are used often.
• So that it is not necessary to include the source of these common
functions in every source code file you write it is possible to just
link a library that contains the functions to your program.
• This is done using the #include directive
– E.g. #include <math.h>
• This directive essentially copies the contents of the library into
that position in your program source code and after this directive
one can access the functions in the library as if they were
declared.
20