Topic 2 - Introduction To C
Topic 2 - Introduction To C
Topic 2 - Introduction To C
INTRODUCTION TO C – LANGUAGE
Consists of
Letters [A to Z, a to z]
Digits [0 to 9]
Special characters [!, *, +, \, “, <, #, (, =, ;, {, >, . . . ]
\b - blank space
\t - tab
\n -new line (normally obtained by pressing the Enter key on the keyboard), e.t.c.
Identifiers
These are names given to various program elements such as variables, constants, functions
and arrays.
An identifier consists of letters and digits in any order except that the first
character in the name should be a letter.
Both upper and lower case letters are permitted, but lower case letters are favoured.
C is case sensitive.
The underscore character can also be used and is considered a letter. When used, it
should appear in the middle of the identifier.
Break, float, case, switch, before, continue, char, struc, const, int, while, void, if, e.t.c.
2
Data Types
These are keywords used to categorize data.
There are four basic data types in C as identified below
Constant
This is a quantity that remains unchanged during the execution of the program. C language
has four basic types of constants.
a) Integer constants.
b) Floating point constants.
c) Character constants.
d) String constants.
Integer and Floating point constants represent numbers and are called numeric constants.
Rules for numeric constants.
i) Commas and blank spaces should not be included.
ii) The constant can be preceded by a minus sign if desired.
iii) The value of the constant cannot exceed max & min bounds. For each type of
constant, these bounds vary from one C compiler to another.
Note
3
Character constants have unique ASCII values, which are useful in arranging words in
alphabetical order.
Escape Sequences
These are non-printable characters that represent special functions. An escape sequence
consists of a backslash followed by a character.
When an escape sequence is written within a pair of single quotation marks it becomes a
character constant, e.g. ‘\a’.
Variables
A variable is an identifier or name that represents a data item that changes in value during
the execution of the program. Variables can be integral, fractional or character.
Operators
An operator is a symbol that denotes an operation to manipulate data. C has several
operators divided into a number of categories identified in the following section.
1. Arithmetic Operators.
These are symbols that represent arithmetic operations.
Operator Function
+ Addition
4
- Subtraction
/ Division
* Multiplication
% Remainder (Only for integers)
2. Relational Operators
These are symbols used to compare quantities
Operator Function
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
== Equal to
!= Not equal to
3. Logical Operators
Used to combine two relational expressions for one decision.
Operator Function
&& And
|| Or
4. Unary Operators
An operator that requires only one operand is called a unary operator. There are four
unary operators in C.
Variable = expression
e.g., sum = a + b;
Expression
An expression consists of constants, variables and/or built-in functions connected
by operators.
An expression can be numerical, relational or logical.
When evaluated, a numerical expression results into a numerical value.
When evaluated, a relational or logical expression results into a true or false.
Note:
When the expression is mixed, the lower precision value will be converted to the
higher precision before evaluation of the expression e.g.
Integer and Long Integer
int is converted to long int because long integer has higher precision.
Integer and Short int
shortint will be converted to int.
Integer and float
intwill be converted to float.
Float and double
Float will be converted to double.
6
Declaration
A declaration specifies the data type of a single or group of variable(s),
constant(s), array(s) or function(s).
Library Functions
These are predefined routines for performing routine or common functions or
tasks. Example library functions in C are given below.
Function ArgumentType Purpose
abs(x) int Returns the absolute value of x
ceil(x) double Rounds off to the next integer greater than
or equal to x, e.g. ceil(12.3) = 13.
floor(x) double Rounds down to the next integer less than or
equal to x, e.g. ceil(12.3) = 12.
pwd(x,y) double Returns xy
log(x) double Returns the natural logarithm of x.
sqrt(x) double Returns the square root of x.
toaskii(x) char Converts the character in the argument to
the ASCII value.
tolower(x) char Converts the character in the argument to
lower case.
toupper(x) char Converts the character in the argument to
upper case.
Satatement
A statement makes the computer to take some action.
There are three categories of statements.
1) Expression Statement
They involve expressions, e.g.
y = a * b + c – d;
2) Compound Statement
A group of statements written within braces, e.g.
{
a = a + 2;
7
b = c + d;
printf(“%d\n”, d);
}
3) Control Statement
These are statements which make a structure for the features of the language,
e.g.
a) while(expression)
{
------------
------------
------------
}
b) for(I = 1; I <= n; i++)
{
----------------
----------------
----------------
}