C Programming Module 1 C Programming Basic
C Programming Module 1 C Programming Basic
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.
Sample Tool:Flowchart
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
1. include
2. define
int x,age;
float sum,a,b;
char middle_intial;
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
8
8
32
64
96
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
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
Date Types
printf conversion
scanf conversion
specification
specifications
long double
%Lf
%Lf
double
%f
%lf
float
%f
%f
%lu
long int
%ld
%ld
unsigned int
%u
%u
int
%d
%d
short
%hd
%hd
char
String - %s
%c
%c
Example:
printf(Hello world);
printf(%d,x);