Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Variables and Constants

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

Variables

In programming, a variable is a container (storage area) to hold data.


To indicate the storage area, each variable should be given a unique name
(identifier).
Variable names are just the symbolic representation of a memory location.

For example:
int playerScore = 95;

Here, playerScore is a variable of integer type. The variable is holding 95 in the


above code.

The value of a variable can be changed, hence the name 'variable'.

In C programming, you have to declare a variable before you can use it.
2.4 Memory Concepts
• Variables
– Variable names correspond to locations in the
computer's memory
– Every variable has a name, a type, a size and a value
– Whenever a new value is placed into a variable
(through scanf, for example), it replaces (and
destroys) the previous value
– Reading variables from memory does not change
them
• A visual representation

integer1 45
4
What is a Variable? symbol table?

A Variable names a place in memory where you Symbol Addr Value


store a Value of a certain Type. 0
1
You first Define a variable by giving it a name 2
and specifying the type, and optionally an 3
initial value declare vs define?
x 4 ?
y 5 ‘e’ (101)
char x; Initial value of x is undefined
char y=‘e’; 6
7
The compiler puts them
Initial value 8
somewhere in memory.
9
Name What names are legal? 10

Type is single character (char) 11

extern? static? const?


12
5
Rules for writing variable name in C

A variable name can have letters (both uppercase and lowercase letters), digits
and underscore only.

The first letter of a variable should be either a letter or an underscore. However, it


is discouraged to start variable name with an underscore. It is because variable
name that starts with an underscore can conflict with system name and may cause
error.

There is no rule on how long a variable can be. However, the first 31 characters of
a variable are discriminated by the compiler. So, the first 31 letters of two variables
in a program should be different.
Constants/Literals

A constant is a value or an identifier whose value cannot be altered in


a program.

For example: 1, 2.5, "C programming is easy" etc.

As mentioned, an identifier also can be defined as a constant.

const double PI = 3.14

Here, PI is a constant. Basically what it means is that, PI and 3.14 is


same for this program.
Integer constants

A integer constant is a numeric constant (associated with number)


without any fractional or exponential part.

There are three types of integer constants in C programming:


decimal constant(base 10)
octal constant(base 8)
hexadecimal constant(base 16)
For example:
Decimal constants: 0, -9, 22 etc
Octal constants: 021, 077, 033 etc
Hexadecimal constants: 0x7f, 0x2a, 0x521 etc

In C programming, octal constant starts with a 0 and hexadecimal


constant starts with a 0x.
Floating-point constants

A floating point constant is a numeric constant that has either a


fractional form or an exponent form.

For example:
-2.0 0.0000234 -0.22E-5
Note: E-5 = 10-5

Character constants
A character constant is a constant which uses single quotation around
characters.

For example: 'a', 'l', 'm', 'F'


Escape Sequences

Sometimes, it is necessary to use characters which cannot be typed


or has special meaning in C programming.

For example: newline(enter), tab, question mark etc. In order to use


these characters, escape sequence is used.

For example: \n is used for newline. The backslash ( \ ) causes


"escape" from the normal way the characters are interpreted by the
compiler.
Escape Sequences

Escape Sequences Character


\b Backspace
\f Form feed
\n Newline
\r Return
\t Horizontal tab
\v Vertical tab
\\ Backslash
\' Single quotation mark
\" Double quotation mark
\? Question mark
\0 Null character
String constants

String constants are the constants which are enclosed in a pair of double-quote
marks.
For example:
"good" //string constant
"" //null string constant
" " //string constant of six white space
"x" //string constant having single character.
"Earth is round\n“ //prints string with newline

Enumeration constants

Keyword enum is used to define enumeration types.

For example:

enum color {yellow, green, black, white};

Here, color is a variable and yellow, green, black and white are the enumeration
constants having value 0, 1, 2 and 3 respectively. For more information, visit
page: C Enumeration.

You might also like