Lecture8 C Variables (1)
Lecture8 C Variables (1)
variables
Variables
The name of a variable can be composed of letters, digits, and the underscore
character.
Before variables can be used, they must be declared.
Before the program is linked, variables must have been defined in one of the
source files.
Simple variables are declared by first specifying the type and then the name of
the variable.
Upper and lowercase letters are distinct because C is case-sensitive.
A variable definition tells the compiler where and how much storage to create for
the variable. A variable definition specifies a data type and contains a list of one
or more variables of that type as follows
type variable_list;
A variable declaration is useful when using multiple files and can define a variable
in one of the files which will be available at the time of linking of the program. Use
the keyword extern to declare a variable at any place.
An int variable has no fractional part. Integer variables tend to be used for
counting.
To declare an int you use the instruction: int variable name;
For example: int a; declares that you want to create an int variable called a.
To assign a value to our integer variable we would use the following C
statement:
int a=10;
Unsigned int: takes two or four bytes to store, range -32,768 to 32,767 or
-2,147,483,648 to 2,147,483,647
Signed int: takes two or four bytes to store, range 0 to 65,535 or 0 to
4,294,967,295
Character Variables
C only has a concept of numbers and characters.
C has no understanding of strings but a string is only an array of characters
and C does have a concept of arrays.
To declare a variable of type character we use the keyword char. - a single
character stored in one byte.
Unsigned char: 1 byte, range 0 to 255
Signed char: 1 byte, range -128 to 127
For example:
char c;
To assign or store a character value in a char data type is easy - a character
variable is just a symbol enclosed by single quotes. For example, if c is a char
variable you can store the letter A in it using the following C statement:
c='A‘
Note that you can only store a single character in a char variable. Remember
that a char variable is 'A' and not "A".