M1-First Program
M1-First Program
Module 1 -
Soon I will
Your First control the world!
Program
Hello World!
1
Introduction to C
. C language
Facilitates a structured and disciplined approach to
computer program design
Provides low-level access
Highly portable
2
Program Basics
. The source code for a program is the set of instructions
written in a high-level, human readable language.
X = 0;
MOVE 0 TO X.
X := 0
. The source code is transformed into object code by a
compiler. Object code is a machine usable format.
. The computer executes a program in response to a
command.
3
4
Basics of a Typical C Environment
Program is created in
Editor Disk the editor and stored
on disk.
Phases of C Programs:
Preprocessor Preprocessor program
Disk processes the code.
1. Edit
Compiler creates
Compiler object code and stores
2. Preprocess Disk
it on disk.
Linker links the object
3. Compile Linker Disk
code with the libraries,
creates a.out and
stores it on disk
4. Link Primary
Memory
Loader
5. Load Loader puts program
in memory.
6. Execute Disk ..
..
..
Primary
Memory
CPU CPU takes each
instruction and
executes it, possibly
storing new data
values as the program
.
. executes.
.
5
GCC Program Basics
. The basic program writing sequence:
1. create or modify a file of instructions using an editor
Unix: Pico, vi, gEdit, emacs, …
2. compile the instructions with GCC
3. execute or run the compiled program
repeat the sequence if there are mistakes
6
Structure of a C Program
function 1
.
.
.
function n
7
Functions
. Each function consists of a header
followed by a basic block.
. General format:
8
The Basic Block
{
declaration of variables
executable statements
}
9
Return statement
. return expression
1. Sets the return value to the value of the expression
2. Returns to the caller / invoker
. Example:
10
Our First Program
Programming is fun!
11
12
Compiling and Running a Program
. To compile and print all warning messages, type
gcc –Wall prog-name.c
13
Compiling and Running a Program
. To execute the program type
./a.out
The ./ indicates the current directory
. To specify the file for the object code,
for example, p1.o, type
gcc –Wall prog1.c –o p1.o
then type
./p1.o
to execute the program
14
15
C Compilers and IDE’s
• One can:
– use a text editor to edit source code, and then use
independent command‐line compilers and linkers
– use an IDE: everything together + facilities to debug,
develop and organize large projects
• There are several C compilers and IDE’s that support
various C compilers
• We will use Code::Blocks and the “GNU GCC” compiler.
16
17
18
19
20
21
22
23
Comments
. Make programs easy to read and modify
. Ignored by the C compiler
. Two methods:
1. // - line comment
- everything on the line following // is ignored
2. /* */ - block comment
- everything between /* */ is ignored
24
Preprocessor Directive: #include
. A C program line beginning with # that is processed by
the compiler before translation begins.
. #include pulls another file into the source
25
Introduction to Input/Output
. Input data is read into variables
. Output data is written from variables.
. Initially, we will assume that the user
enters data via the terminal keyboard
views output data in a terminal window on the screen
26
Program Input / Output
. The C run-time system automatically opens two files
for you at the time your program starts:
stdin – standard input (from the keyboard)
stdout – standard output (to the terminal window in
which the program started)
. Later, how to read and write files on disk
1. Using stdin and stdout
2. Using FILE’s
27
Console Input/Output
. Defined in the C library included in <stdio.h>
Must have this line near start of file:
#include <stdio.h>
Includes input functions scanf, fscanf, …
Includes output functions printf, fprintf, …
28
Console Output - printf
. Print to standard output,
typically the screen
. General format (value-list may not be required):
printf("format string", value-list);
29
Console Output
What can be output?
. Any data can be output to display screen
Literal values
Variables
Constants
Expressions (which can include all of above)
. Note
Values are passed to printf
Addresses are passed to scanf
30
Console Output
. We can
Control vertical spacing with blank lines
Use the escape sequence "\n“, new line
Should use at the end of all lines unless you are building lines
with multiple printf’s.
If you printf without a \n and the program crashes, you will not
see the output.
Control horizontal spacing
Spaces
Use the escape sequence “\t”, tab
Sometimes undependable.
31
Terminal Output - Examples
32
Program Output: Escape Character \
Indicates that a “special” character is to be output
Escape Description
Sequence
\n Newline. Position the screen cursor to the beginning of
the next line.
\t Horizontal tab. Move the screen cursor to the next tab
stop.
\r Carriage return. Position the screen cursor to the
beginning of the current line; do not advance to the next
line.
\a Alert. Sound the system bell.
Module 1
Your First Program
THE END
35