Topic 2 - Introduction To C

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

1

INTRODUCTION TO C – LANGUAGE

The C-Character Set

Consists of

Letters [A to Z, a to z]
Digits [0 to 9]
Special characters [!, *, +, \, “, <, #, (, =, ;, {, >, . . . ]

C uses certain combinations of these characters to represent special conditions such as

\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.

When forming identifiers, note the following:

 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.

Keywords or Reserved Words


These are words with standard predefined meaning in C and that should not therefore be
used as programmer defined identifiers. Such words should only be used for the purpose
intended by the language.

Example Reserved Words

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

Data Type Describe Typical Memory Requirement


int Interger quantity. 2 bytes
char Single Character. 1 byte
float Single precision floating point number, 4 bytes
i.e., a number containing a decimal pt or
an exponent or both.
double Double precision floating point number. 8 bytes

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.

Numeric constants can be binary, octal, decimal or hexadecimal.

An integer constant is a whole number whereas a floating point constant is fractional.

A Character constant consists of a single symbol enclosed in a pair of single quotation


marks, e.g. ‘a’.

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.

Escape Sequence Character


\a Bell
\b Back space
\t Horizontal tab
\v Vertical tab
\n New line(line feed)
\r Carriage return
\” Quotation mark
\’ Apostrophe
\? Question mark
\\ Back slash

When an escape sequence is written within a pair of single quotation marks it becomes a
character constant, e.g. ‘\a’.

A String constantconsists of a sequence of characters enclosed within a pair of double


quotation marks, e.g. “Example program”.
Note that string constants do not have ASCII values.

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.

i) The minus Operator (-)


Used to indicate negative values, e.g. -2.5.

ii) The Type Cast Operator


Used to change the data type.
The syntax is (type cast) operand or type cast(operand)
e.g. int (10.5) = (int) 10.5 = 10

iii) The Increment Operator (++)


Used to increment the value of the operand
++i means increase the value of i by 1 and use it and
i++ means use the value of i and thereafter increase the value by 1.
5

iv) The Decrement Operator


Used to decrement the value of the operand
--i means decrease the value of i by 1 and use it and
i-- means use the value of i and thereafter decrease the value by 1.

5. The Assignment Operator


Assignment is done from right to left. The equal sign, i.e., “=” is used as the assignment
operator.

The general form is

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++)
{
----------------
----------------
----------------
}

You might also like