3 Declaration
3 Declaration
3 Declaration
Variable Declaration
As a programmer, one of the most important things to consider is how to allocate memory that would
handle the data.
Variable declaration is a statement that communicates to the C compiler the names of all variables used
in the program and the kind of information stored in each variable. It also tells how that information will
be represented in memory.
Examples:
int x,age;
float sum,a,b;
char middle_intial;
Data Type is a set of values and a set of operations that can be performed on those values.
1. char
2. double
3. int
short
long
signed
unsigned
The modifiers define the amount of storage allocated to the variable. The amount of storage allocated is
not cast in stone. ANSI has the following rules:
1. Text (data type char) – made up of single characters (example x,#,9,E) and strings (“Hello”), usually
8 bits, or 1 byte with the range of 0 to 255.
C Programming Basics
3. Floating-point values – numbers that have fractional portions such as 12.345, and exponents
1.2e+22.
6. void – signifies values that occupy 0 bit and have no value. You can also use this type to create
generic pointers.
7. Pointer – does not hold information as do the other data types. Instead, each pointer contains the
address of the memory location.
Example:
int Count;
Count = 5;
Example:
float Miles;
Miles = 5.6;
double is used to define BIG floating point numbers. It reserves twice the storage for the number.
Example:
double Atoms;
Atoms = 2500000;
Example:
char Letter;
Letter = 'x';
What is a variable?
Variable is like a container in your computer's memory - you can store a value in it and retrieve or modify
it when necessary. It is associated with a memory cell whose value can change as the program executes.
C Programming Basics
A variable is an identifier. An identifier is name that identifies or labels the identity of an object. In C, the
following are the naming conventions for an identifier.
Examples:
Names... Example
The previous example has a variable sum that is initialized during declaration.