In order to be useful a program must be able to represent real life quantities or data e.g. a person’s name, age, height, bank balance, etc. This data will be stored in memory locations called variables that we will name ourselves. However so that the data may be represented as aptly as possible the variables will have to be of different types to suit their data. For example while an integer can represent the age of a person reasonably well it won’t be able to represent the pounds and pence in a bank balance or the name of an individual quite so well.
There are five basic data types in c language char, int, float, double, and void. All other data types in C are based on these. Note that the size of an int depends on the standard size of an integer on a particular operating system.
char | 1 byte ( 8 bits ) with range -128 to 127 |
int | 16-bit OS : 2 bytes with range -32768 to 32767 32-bit OS : 4 bytes with range -2,147,483,648 to 2,147,483,647 |
float | 4 bytes with range 10-38 to 1038 with 7 digits of precision |
double | 8 bytes with range 10-308 to 10308 with 15 digits of precision |
void | generic pointer, used to indicate no function parameters etc. |
Except for type void the meaning of the above basic data types may be altered when combined with the following keywords.
The signed and unsigned modifiers may be applied to types char and int and will simply change the range of possible values. For example an unsigned char has a range of 0 to 255, all positive, as opposed to a signed char which has a range of -128 to 127. An unsigned integer on a 16-bit system has a range of 0 to 65535 as opposed to a signed int which has a range of -32768 to 32767.
Note : However that the default for type int or char is signed so that the type signed char is always equivalent to type char and the type signed int is always equivalent to int.
The long modifier may be applied to type int and double only. A long int will require 4 bytes of storage no matter what operating system is in use and has a range of -2,147,483,648 to 2,147,483,647. A long double will require 10 bytes of storage and will be able to maintain up to 19 digits of precision.
The short modifier may be applied only to type int and will give a 2 byte integer independent of the operating system in use.
NOTE : Note that the keyword int may be omitted without error so that the type unsigned is the same as type unsigned int, the type long is equivalent to the type long int, and the type short is equivalent to the type short int.
A variable is a named piece of memory which is used to hold a value which may be modified by the program. A variable thus has three attributes that are of interest to us : its type, its value and its address.
The variable’s type informs us what type and range of values it can represent and how much memory is used to store that value. The variable’s address informs us where in memory the variable is located (which will become increasingly important when we discuss pointers later on).
All C variables must be declared as follows :-
type variable-list ;
For Example :-
int i ; char a, b, ch ;
Variables are declared in three general areas in a C program.
When declared inside functions as follows they are termed local variables and are visible (or accessible) within the function ( or code block ) only.
void main() { int i, j ; ... }
A local variable is created i.e. allocated memory for storage upon entry into the code block in which it is declared and is destroyed i.e. its memory is released on exit. This means that values cannot be stored in these variables for use in any subsequent calls to the function .
When declared outside functions they are termed global variables and are visible throughout the file or have file scope. These variables are created at program start-up and can be used for the lifetime of the program.
int i ; void main() { ... }
When declared within the braces of a function they are termed the formal parameters of the function as we will see later on.
int func1( int a, char b ) ;
Names of variables and functions in C are called identifiers and are case sensitive. The first character of an identifier must be either a letter or an underscore while the remaining characters may be letters, numbers, or underscores. Identifiers in C can be up to 31 characters in length.
When variables are declared in a program it just means that an appropriate amount of memory is allocated to them for their exclusive use. This memory however is not initialised to zero or to any other value automatically and so will contain random values unless specifically initialised before use.
type var-name = constant ;
For Example :-
char ch = 'a' ; double d = 12.2323 ; int i, j = 20 ; /* note in this case i is not initialised */
There are four storage class modifiers used in C which determine an identifier’s storage duration and scope.
An identifier’s storage duration is the period during which that identifier exists in memory. Some identifiers exist for a short time only, some are repeatedly created and destroyed and some exist for the entire duration of the program. An identifier’s scope specifies what sections of code it is accessible from.
The auto storage class is implicitly the default storage class used and simply specifies a normal local variable which is visible within its own code block only and which is created and destroyed automatically upon entry and exit respectively from the code block. The register storage class also specifies a normal local variable but it also requests that the compiler store a variable so that it may be accessed as quickly as possible, possibly from a CPU register.
The static storage class causes a local variable to become permanent within its own code block i.e. it retains its memory space and hence its value between function calls.
When applied to global variables the static modifier causes them to be visible only within the physical source file that contains them i.e. to have file scope. Whereas the extern modifier which is the implicit default for global variables enables them to be accessed in more than one source file.
For example in the case where there are two C source code files to be compiled together to give one executable and where one specific global variable needs to be used by both the external class allows the programmer to inform the compiler of the existence of this global variable in both files.
Constants are fixed values that cannot be altered by the program and can be numbers, characters or strings.
Some Examples :-
char : 'a', '$', '7' int : 10, 100, -100 unsigned : 0, 255 float : 12.23456, -1.573765e10, 1.347654E-13 double : 1433.34534545454, 1.35456456456456E-200 long : 65536, 2222222 string : “Hello World \ n”
NOTE : Floating point constants default to type double. For example the following code segment will cause the compiler to issue a warning pertaining to floating point conversion in the case of f_val but not in the case of d_val..
float f_val ; double d_val ; f_val = 123.345 ; d_val = 123.345 ;
However the value may be coerced to type float by the use of a modifier as follows :-
f = 123.345F ;
Integer constants may also be forced to be a certain type as follows :-
100U --- unsigned 100L --- long
Integer constants may be represented as either decimal which is the default, as hexadecimal when preceded by "0x", e.g. 0x2A, or as octal when preceded by "O", e.g. O27.
Character constants are normally represented between single quotes, e.g. 'a', 'b', etc. However they may also be represented using their ASCII (or decimal) values e.g. 97 is the ASCII value for the letter 'a', and so the following two statements are equivalent. (See Appendix A for a listing of the first 128 ASCII codes.)
char ch = 97 ; char ch = 'a' ;
There are also a number of special character constants sometimes called Escape Sequences, which are preceded by the backslash character '\', and have special meanings in C.
All Rights Reserved. © 2024 BookOfNetwork