0% found this document useful (0 votes)
9 views

Lecture 04

Uploaded by

imalshaamashan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Lecture 04

Uploaded by

imalshaamashan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Fundamentals of Programming

COSC 11023 / COST 11023

Variables and Constants in C

Sachintha Pitigala
© 2023
What is a Variable?
● Variables in C have the same meaning as variables in
algebra. That is, they represent some unknown, or
variable, value. x
x = a+b
5
z + 2 = 3 ( y - 5)
● In programming, a variable is a location in memory
where a value can be stored for use by a program.

● A variable is best thought of as a container for a value.


2
Variable Declaration
● Before using a variable, you must give the compiler
some information about the variable; i.e., you must
declare it.

● A variable declaration specifies the type, the name


and possibly the initial value of the variable.

● A variable name in C also called as an identifier.


3
Identifiers
● Identifiers refer to the names of variables, functions
and arrays.

● These are user defined names and consists of


sequence of letters, digits and underscore.

● Both uppercase and lowercase letters are


permitted to make the identifier.
4
Rules for Variable Names (Identifiers)
● The first letter of a variable name must be either a letter or an
underscore (_).

● A variable name can have letters (both uppercase and lowercase


letters), digits and underscore only.

● There is no rule on how long a variable can be. However, only the
first 31 characters of a variable are checked by the compiler.

● Variable name in C must not be a C reserved word (Keyword).


5
Reserved Words (Keywords in C)

6
Naming Conventions
● C programmers generally agree on the following conventions for naming
variables.

● Begin variable names with lowercase letters

● Use meaningful identifiers

● Separate “words” within identifiers with underscores or mixed upper and lower
case.
Examples: surfaceArea surface_Area
surface_area

● Be consistent!
7
Case Sensitivity
● C is case sensitive language

● It matters whether an identifier, such as a variable name, is


uppercase or lowercase.
■ Example:
○ area
○ Area
○ AREA
○ ArEa

● are all seen as different variables by the compiler.


8
Which are Legal Identifiers?

9
Declaring Variables
● The declaration statement includes the
data type of the variable.

● Examples of variable declarations:


int age;
float area;
10
Declaring Variables (con’t)
● When we declare a variable
■ Space is set aside in memory to hold a value of
the specified data type
■ That space is associated with the variable name
■ That space is associated with a unique address

age
● Visualization of the declaration:
int age; garbage

11
1000101
Data Types in C
There are four basic data types in C.

1. int
used to represents whole numbers
2. float
used to represents real numbers
3. double
used to represents real numbers
4. char
used to represents characters
12
int Data Type
● Represents a signed integer (Whole numbers or Counting
numbers) of typically 4 or 8 bytes (32 or 64 bits).

● Precise size is machine-dependent.

13
float and double Variable Types
● Represent typically 32 and/or 64 bit real numbers.

● How these are represented internally and their precise


sizes depend on the machine.

14
char Variable Type
● Represents a single byte (8 bits) of storage.

● Can be signed or unsigned.

● Internally char is just a number.

● Numerical value is associated with character via a


character set.

● ASCII character set used in ANSI C. 15


Additional Qualifier Names
● Note that other data types can be constructed using
the modifiers:
short, long, signed, unsigned

● The precise sizes of these types is machine-specific.

● To find out the meaning of short int, etc. on a given


system, use <limits.h>
16
Variable Assignment
● After variables are declared, they must (should) be given values. This
is called assignment and it is done with the ‘=‘ operator.

● This (=) operator does not denote equality. It assigns the value of the
righthand side of the statement (the expression) to the variable on the
lefthand side.

● Examples:
float a, b;
int c;
b = 2.12;
c = 200;
17
Type Casting (Type Conversions)
● Type casting is a way to convert a variable from one
data type to another data type.

● There are two ways of achieving the type casting.


There are:
1. Implicit Type Casting
2. Explicit Type Casting
18
Implicit Type Casting
● In implicit type casting (automatic transformation) variable
in a smaller data type is transformed into a larger data
type automatically.

● Example:
int a;
float f, g;
g = a + f;

19
Explicit Type Casting
● In explicit type casting (automatic transformation) data
type of a variable will be explicitly converted into a
different data type by the programmer.

● Example:
a = (int) c;
b = (double) d + c;

20
Constants in C
● What is a Constant?
○ Constant is a value that cannot change during the
execution of a program.

● Constants in C can be divided into two main types.


○ Numeric Constants
○ String or Character Constants

21
Numeric Constants
● Numeric constants can be categorized into two categories.
1. Integer Constants
2. Real or Floating point constant

● An Integer constant is a signed or unsigned whole number.

● C language supports an integer constant in octal (base 8),


decimal (base 10) and hexadecimal (base 16). The default
number system follows in C language is decimal (base 10).
22
Numeric Constants
● Real or floating point constants
○ Any signed or unsigned number with fractional part is
called real or floating point constant. A real constant
can be written in decimal or exponential form.

● Example:
○ Decimal form: 0.254, +32.0, 2.95
○ Exponential form: 0.218e6 0.42e-32
23
Character Constants
● Any letter or character enclosed in single apostrophe is
call character constant.
■ Example: ‘y’ ‘$’ ‘+’

● Any sequence of characters consisting of letters, digits


and symbols enclosed in double quotes is called string
constant.
■ Example: “uok” “value is 0”
24
Syntax for Constant Declarations
const data_type variable_name;
or
data_type const variable_name;
or
#define variable_name value;

25
Example Constant Declarations
const int SIZE = 10;
or
char const GRADE = ‘A’;
or
#define pi 3.14;
(This should go before the main() function)

26

You might also like