C Programming Basics
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.
In C, a variable is declared before a pre-defined function.
Here is the syntax for declaration:
Data type variable list;
Examples:
int x,age;
float sum,a,b;
char middle_intial;
What is a data type?
Data Type is a set of values and a set of operations that can be performed on those values.
The following are the standard pre-defined data types in C:
1. char
2. double
3. int
The three data types above have the following modifiers.
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:
short int <= int <= long int
float <= double <= long double
Seven Basic C Data Types:
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
2. Integer values – those numbers you learned to count with.
3. Floating-point values – numbers that have fractional portions such as 12.345, and exponents
1.2e+22.
4. Double-floating point values – have extended range of 1.7e-308 to 1.7e+308.
5. Enumerated data types – allow for user-defined data types.
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.
Explaining each of the data types:
int is used to define integer numbers.
Example:
int Count;
Count = 5;
float is used to define floating point numbers.
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;
char defines characters.
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.
1. Names are made up of letters and digits.
2. The first character must be a letter.
3. C is case-sensitive, example ‘s’ is not the same with ‘S’.
4. The underscore symbol (_) is considered as a letter in C. It is not recommended to be used,
however, as the first character in a name.
5. At least the first 3 characters of a name are significant.
Examples:
Names... Example
CANNOT start with a number 2i
CAN contain a number elsewhere h2o
CANNOT contain any arithmetic operators... r*s+t
CANNOT contain any other punctuation marks... #@x%£!!a
CAN contain or begin with an underscore _height_
CANNOT be a C keyword struct
CANNOT contain a space im stupid
CAN be of mixed cases XSquared
The following is an example of a variable declaration.
int num;
Here is another one.
int num, num2;
Here is another one.
int num, num2, sum=0;
The previous example has a variable sum that is initialized during declaration.