You are on page 1of 16

INTRODUCTION TO COMPUTER PROGRAMMING

BTC 1122/ETC 1112


Uthpala Samaratunga
Lecturer
Dept. of ICT, Faculty of Technology
University of Sri Jayewardenepura
uthpalas@sjp.ac.lk

DATATYPES AND VARIABLES TOPIC 2


TOKENS
In C programs, each word and punctuation is referred to as a
token. C Tokens are the smallest building block or smallest unit of a
C program.
•Keywords
•Identifiers
•Constants
•Strings
•Operators
•Special Symbols
KEYWORDS
Some of the words we use in the C programs – in particular int,
void etc. – are keywords or reserved words of the language. These
words have special meaning to the C compiler, so you must be
careful not to use keywords as identifiers.
KEYWORDS
IDENTIFIERS AND CASE SENSITIVITY
Identifiers refer to the names of variables, functions, arrays etc.
Identifiers are created by the programmer.
A variable name in C can be any valid identifier. An identifier is a
series of characters consisting of letters, digits and underscores (_)
that does not begin with a digit. Keywords cannot be used as
identifiers.
C is case-sensitive. Uppercase and lowercase letters are different in
C, so a1 and A1 are different identifiers.
VARIABLES
A Variable is a space in the computer’s memory set aside for a
certain kind of data and given a name for easy reference. Every
variable has a name, a type and a value. Variables can be
declared anywhere in a C program.
To use variables in a C program they should be declared and
initialized.
 Declaration
 Initialization
DECLARING VARIABLES
All variables must be declared with a name and a data type
before they can be used in a program.
Syntax

Examples
DATA TYPES
The data type of a variable determines what type of data is
stored in a variable and the possible range of data stored in a
variable.
INTEGER DATA TYPES
Type Storage size Value range Description
char 1 byte -128 to 127 or For characters
0 to 255
int 4 bytes -2,147,483,648 to For integers
2,147,483,647 (whole numbers)

The size of int can change on different systems. In modern 32-bit


and 64-bit systems it is most often 4 bytes. In older 16-bit systems,
it was 2 bytes.
FLOATING-POINT DATA TYPES
Type Storage size Value range Description
float 4 bytes 1.17549e-038 to • For numbers with decimal
3.40282e+038 points.
• Up to 6 decimal places.
double 8 bytes 2.22507e-308 to • For numbers with decimal
1.79769e+308 points.
• Up to 15 decimal places.
DECLARING VARIABLES CONTD.

These are variable declarations. The names integer1 and integer2


are the names of variables – locations in memory where values can
be stored for use by a program. These declarations specify that
variables integer1 and integer2 are of type int, which means that
they’ll hold integer values.
DECLARING VARIABLES

is the same as

Variables of the same data type can be declared in the same


statement.
INITIALIZING VARIABLES
After declaring variables they can be initialized with suitable
values. Those values will be assigned to those variables (the values
will be stored in those variables).
Syntax Examples
DECLARING AND INITIALIZING VARIABLES
Variables can also be declared and initialized in the same
statement.
Examples

Variables a and b are declared and


initialized in the same statement.
Variables d, e and f are declared in
the same statement, but only the
variable e is initialized.
QUESTION: WHICH OF THE FOLLOWING ARE
VALID VARIABLE NAMES?
1. FirstName
2. firstName
3. 1Name
4. _name
5. int
6. My name
7. intNo
Q&A

You might also like