Vous êtes sur la page 1sur 33

Module 1

Computer programming is
creating a sequence of
instructions to enable the
computer to do something.

A programming language is an
artificial language that can be
used to control the behavior of a
machine, particularly a
computer.
For this subject, the programming
language used is Turbo C.

Dennis Ritchie developed Turbo


C at AT&T Bell Laboratories.
Turbo C was first developed for
system programming.

1. Problem statement: The programming


process begins with a clear, written
statement of the problem to be solved by the
computer.
2. Algorithm development: Once the problem
has been clearly stated and all the
requirements have been understood, the
next step is to develop the program logic
necessary for accomplishing the task.
*An algorithm is defined as a logical sequence
of steps that must be performed in order to
accomplish a given task.

Sample Tool:Flowchart

3. Program coding: When the programmer is


satisfied with the efficiency of the logic
developed in the preceding step, it is time
to convert that logic (in either flowchart or
pseudo code form) to the specific syntax of
the programming language that will be
used.
4. Program testing: The coded program is next
checked for errors.
5. Program documentation: The programming
process is complete when the program has
been fully documented.

1. Syntax error - occurs when your code


violates one or more grammar rules of
C and is detected by the compiler as it
attempts to translate your program.
Note: If a statement has a syntax error, it
cannot be translated and your
program will not be executed.

2. Run-time Errors
- are detected errors and displayed by the compiler
during the execution of the program.
- occurs when the program directs the computer to
perform illegal operation, such as dividing a number
by zero.
- an attempt to perform an invalid operation,
detected during program execution.
Note: When a run-time error occurs, the computer will
stop executing your program and will display a
diagnostic message that indicates the line where
the error was detected.

3. Logic Errors
- occur when a program follows a
faulty algorithm.
- do not cause a run-time error and do
not display error messages, so are
very difficult to detect.
Note: The only sign of a logic error may
be incorrect program output.

Preprocessor Directives
int main(void){
local declarations
statements

Your First Program


#include <stdio.h>
int main(void)
{
printf("Hello World!\n");
return 0;
}

The C Preprocessor - a program


that is executed before the source
code is compiled.
DIRECTIVES how C preprocessor
commands are called, and begin
with a pound / hash symbol (#).
No white space should appear
before the #, and a semi colon is
NOT required at the end.

1. include
2. define

gives program access to a library.


causes the preprocessor to insert
definitions from a standard header
file into the program before
compilation.
tells the preprocessor that some
names used in the program are
found in the standard header file.

Every C program has a main function. This


is where program execution begins.
Body- the remaining line of the program in
the body.
Braces {} enclose the body of the
function.
- indicates the beginning and end of the
function main.

1. Declarations the part of the program


that tells the compiler the names of
memory cells in a program needed in
the function, commonly data
requirements identified during problem
analysis.
2. Executable statements derived
statements from the algorithm into
machine language and later executed.

data type variable_list;


Ex.

int x,age;
float sum,a,b;
char middle_intial;

Data Type a set of values and a


set of operations that can be
performed on those values.
Standard Predefined Data Type in
C:
char
double
int

Seven Basic C Data Types:


1. Text (data type char) made up of single characters
(example x,#,9,E) and strings (Hello), usually 8 bits, or
1 byte with the range of 0 to 255.
2. Integer values those numbers you learned to count
with.
3. Floating-point values numbers that have fractional
portions such as 12.345, and exponents 1.2e+22.
4. Double-floating point values have extended range of
1.7e-308 to 1.7e+308.
5. Enumerated data types allow for user-defined data
types.
6. void signifies values that occupy 0 bit and have no
value. You can also use this type to create generic
pointers.
7. Pointer does not hold information as do the other data
types. Instead, each pointer contains the address of the
memory location.

int - data type


int is used to define integer numbers.
Ex.
{ int Count;
Count = 5; }
float - data type
float is used to define floating point numbers.
Ex.
{ float Miles;
Miles = 5.6; }

double - data type


double is used to define BIG floating point
numbers. It reserves twice the storage for
the number. On PCs this is likely to be 8
bytes.
Ex.
{ double Atoms;
Atoms = 2500000; }
char - data type
char defines characters.
Ex.
{ char Letter;
Letter = 'x'; }

Modifiers
The three data types above have the following
modifiers.
short
long
signed
unsigned
The modifiers define the amount of storage
allocated to the variable. The amount of storage
allocated is not cast in stone. ANSI has the
following rules:
short int <= int <= long int
float <= double <= long double

Type
Bytes
short int
2
unsigned short int 2
unsigned int
4
int
4
(2Gb)
long int
4
(2Gb)
signed char
1
unsigned char 1
float
4
double
8
long double
12

16
16
32
32

Bits
Range
-32,768 -> +32,767 (32kb)
0 -> +65,535 (64Kb)
0 -> +4,294,967,295 ( 4Gb)
-2,147,483,648 -> +2,147,483,647

32

-2,147,483,648 -> +2,147,483,647

8
8
32
64
96

-128 -> +127


0 -> +255

Variables - are like containers in your


computer's memory - you can store values
in them and retrieve or modify them when
necessary.
- associated with a memory cell whose
value can change as the program executes.
Variable declaration statements that
communicate to the C compiler the names
of all variables used in the program and the
kind of information stored in each variable.
- also tells how that information will be
represented in memory.

Naming Conventions
1. Names are made up of letters and digits.
2. The first character must be a letter.
3. C is case-sensitive, example s is not the
same with S.
4. The underscore symbol (_) is considered
as a letter in C. It is not recommended to
be used, however, as the first character
in a name.
5. At least the first 3 characters of a name
are significant.

Names...
CANNOT start with a number
CAN contain a number elsewhere
CANNOT contain any arithmetic operators...
CANNOT contain any other punctuation marks...
%!!a
CAN contain or begin with an underscore
_height_
CANNOT be a C keyword
CANNOT contain a space
stupid
CAN be of mixed cases
XSquared

Example
2i
h2o
r*s+t
#@x

struct
im

Equal sign (=)


- the most basic assignment
operator where the value on the
right of the equal sign is
assigned to the variable on the
left.
Example:
c = c + 1;
radius = 2 * diameter;
stat = getch();

Binary Operators
- take two operands and return a result.
Operator
+
*
/
%

Use
op1
op1
op1
op1
op1

+ op2
- op2
* op2
/ op2
% op2

Result
adds op1 to op2
subtracts op2 from op1
multiplies op1 by op2
divides op1 by op2
computes the remainder
from dividing op1 by op2

scanf() one of the Turbo C object stream object


that is used to accept data from the standard
input, usually the keyboard.
syntax:
scanf(format, &var_name);
Example:
printf(Input side of the square:);
scanf(%d, &s);

Date Types

printf conversion

scanf conversion

specification

specifications

long double

%Lf

%Lf

double

%f

%lf

float

%f

%f

unsigned long int %lu

%lu

long int

%ld

%ld

unsigned int

%u

%u

int

%d

%d

short

%hd

%hd

char
String - %s

%c

%c

printf writes formatted output to the


standard output device such as the
monitor.
Syntax:
printf(string expression);
printf(format code,var_name);

Example:
printf(Hello world);
printf(%d,x);

1. Restate the problem.


2. Analyze the problem.
3. Identify the output.
4. Identify the input.
5. Identify the process.

It specifies the sequence of execution of


a group of statements.
3 Types:
1. Sequential
2. Conditional
3. Iterative