You are on page 1of 41

Chapter 2

C Programming
Language
Syllabus

The Program Structure


Preprocessor Directive
Declaration of Variable and
Constant
C Operator
Lesson Outcomes

1.Explain each element of C Language


structure
2.Describe how pre-processor works in C
3.Outline basic structure of C Language
4. Introduce and describe C operators
The Program Structure
Header file

E.g.: Constant

E.g.:
int fahr, celcius;
int lower, upper, step;
The Program Structure
Includes declarations for the
standard input/output library
of procedures.
Read: Hash-include
Comment

Instruction (function call) to


output Hello World

Curly braces mark the Statements (lines of


beginning and end of a instructions) always end with
block of instructions a semi-colon (;)
C Program Elements

Pre-processor*
Declaration
Keywords
Statements
Comments
Pre-Processor Directive

Preprocessor
Analyze source file before compiling
Process pre-processor directive
Remove comments
Divide program into tokens

Result in more efficient & clearer code


How pre-process works
Replace macros defined with #define directive

Source Processed source


code file code file
(FILE.C) Pre-processor Compiling

Include files with #include directive


Pre-Processor Directive

Begin with # character


Call up essential definitions & libraries
Pre-processor directives:
#include includes header files
#define define constant macros
Typical Header File

Header File Comment


stdio.h Standard I/O routine

math.h Math functions

ctype.h Character classification & conversion

string.h String manipulation functions

stdlib.h Miscellaneous routine

time.h Time functions

conio.h ????
Comment Special Character

\n newline
\t tab
\r carriage return
\f form feed
\v vertical tab
\b backspace
\ double quote (\ acts as an escape mark)
\nnn octal character value
Exercise 1

Write a program to display words:


1) My First Program C

2) My
First
Program C
* Using 1 command printf*
Identifiers
Identifiers are used to name :
Constant
Variables
Function names
Labels

General rules for identifiers


First character must be alphabetic character or
underscore (cannot begin with a digit)
Must consists ONLY alphabetic character, digits or
underscore
Cannot duplicate reserved words
Case sensitive
Exercise 2
Valid or Invalid
1Letter
Letter_1
Double
Int
TWO*FOUR
joes
_letter
Declaration of Variable and
Constant
Declaration
Define attribute (data type) of
identifiers used.
Must precede executable
statements
Global vs Local Declaration
Global vs Local variable

Global variable - declared outside any


function

Local variable - declared inside a function


Local variable take precedence over
global variable
AVOID using global variables
Declaring of Variables
Variables in C are memory locations that are given
names and can be assigned values.
We use variables to store data in memory for later use
There are 2 basic kinds of variables in C which are
numeric and character.
Must be declare prior to their use
You can name a variable anything you like as long as it
follow General rules for identifiers

General form :
<data_type> <variable_name>

e.g : int a;
e.g : int a,b,c;
Data Types

Data is a set of values and operations that can


performed on those values

Standard data types:


Integer (int)
Floating point (float, double)
Character (char)
1) Data type int
Represent the whole numbers
ANSI C range:
-32768 to +32768
16 bit ints
Example:
+10500
435
-25
+15
32767
Syntax: int435
2) Floating Point Data Type

Represent real numbers

2 types:

Example: 3.141593, 0.005,1234.0, 12e-4

Syntax: float 3.0 , double 45e+3, double x, y, z


3) Data type char

Represent individual character value


Letter, digit, special character

Example: A , *, z
Syntax: char z
Exercise 3

Identify suitable data type for following variables:

Data Type Variable name Value

? total -12

? Name Pdaus

? Wide 3.4E+38

? Radius 1.7E+308

? Age 12
Declaring of Constant
Values cannot be changed during execution of
program
Type of constants:
character, integer, floating point, string
General form:
const <data_type> <variable>

#define <variable>

const double PI
#define Nilai
Keywords

Reserved word
Predefined by compiler libraries
Cannot be used as identifiers
Must be written in lowercase letters
Instruction set
Reserved Words
Identifiers which have standard, predefined meaning

Cannot be used for any other purpose


Statements

Sequence of instructions
Branching or looping or Function calls
Start with a command word and ends
with ;
Compounded by curly brackets { }
Comments

Use to clarify program statements


Make program code more readable
Start with /* and end with */
Can be included anywhere in the
program
C language elements
Standard header

Pre-process
/* converts distance from miles to kilometres */ Constant
directive
# include <stdio.h> macro
# include <math.h>
Main function # define KMS_PER_MILE 1.609 Reserved
main ( ) word
Start program {
float miles;
printf (enter the distance in miles);
Start scanf (%f, &miles);
identifier kms=KMS_PER_MILE * miles;
printf (That equals %f kilometres \n, kms);
getch();
}

End program
C Operators

Basic tools for building expression


Four main types of C operators:
Arithmetic operators
Logical operators
Equality operators
Relational operators
Arithmetic Operators
Performing arithmetic on numeric data
Arithmetic expression is written as...
identifier = operand operator operand

C Operator Arithmetic Operator


Addition +
Subtraction -
Multiplication *
Division /
Modulus Reminder %
Increment ++
Decrement --
Arithmetic Operators

Example of Arithmetic Statement Equivalent

x+=3.0 x=x+3.0;
voltage/= sqrt(2); voltage=voltage/sqrt(2);
i-- i=i 1
i++ i=i + 1
(i is incremented AFTER I has been used)
++i i incremented before i is used
--i i decrease before i is used
Relational Operators

Relational operator = use for data


comparison
Use with if statements
The result is TRUE (1) and FALSE (0)
There are six relational operators in C.
Relational Operators

C Operator Meaning
< less than
> greater than
=< less than or equal to
=> greater than or equal to

Examples:

x > y : x is greater than y


x < y : x is less than y
x => y : x is greater than or equal to y
x =< y : x is less than or equal to y
Equality Operators

Equality operator has a unique


appearance
It has two characters
There are two types equality operators:
equal to (==)
not equal (!=)
Examples:
x == y : x is equal to y
x != y : x is not equal to y
Logical Operators

Logical operator is use to combine more than


one relational test.

Also known as compound relational


operators.

C Operator Meaning
&& Logical AND
|| Logical OR
! Logical negation
Logical Operators
For logical AND (&&)

Operand 1 Operand 2 Operand 1 && Operand 2


true true true
true false false
false true false
false false false

For logical OR (||)

Operand 1 Operand 2 Operand 1 || Operand 2


true true true
true false true
false true true
false false false
Trigonometry Functions
Pre-Processor header declaration
#include <math.h>

Function Description
sqrt (x) Square root of
exp (x) Exponential function ex
log (x) Natural logarithm of x
abs (x) Absolute value of x
pow (x) X raised to power y (xy)
sin (x) Trigonometric sine of x (in radian)
cos (x) Trigonometric cose of x (in radian)
Order in Expressions

B - Brackets
O - Order (exponents)
D - Division
M - Multiplication
A - Addition
S - Subtraction

1 + 2 * 3 - 4 / 5 = 1 + (2 * 3) - (4 / 5)
Example
The following example illustrates the two methods for variable initialization:
#include <stdio.h>
main ( ) {
int sum=33;
float money=44.12;
char letter;
double pressure;
letter=E ; /* assign character value */
pressure=2.01e-10; /*assign double value */
printf(value of sum is %d\n,sum);
printf(value of money is %f\n, money);
printf(value of letter is %c\n, letter);
printf(value of pressure is %e\n, pressure);
}

Which produces the following output:


Value of sum is 33
Value of money is 44.11999
Value of letter is E
Value of pressure is 2.010000e-10
Outcomes

Explain each element of C Language


structure
Describe how pre-processor works in
C
Outline basic structure of C Language
Able to write program to solve
expression statements.
Q&A

You might also like