TRANS03
C Data Types and Variables Chapter 3
Objectives of this chapter:
- Variables and Constants
- What are Data Types
- C Data Types
- int
- float
- double
- char
- void
- Defining Program Constants
Variables and Constants
a typical C program accomplishes many tasks and requires to work with data.
Some data are pre-set before a program is used and retain their values as the program runs. Such data are called constants.
Some data may change or be assigned values as the program runs, these data are called variables.
What are Data Types ?
Data can be numbers, letters, or characters.
The computer needs a way to identify and use these different kind of data.
C recognizes several fundamental data types by using seven keywords to set up the types: int, char, float, double, short, long and unsigned.
A variable when declared needs to have its type stated.
C Data Types.
The int Type
- the int type is a signed integer, which means it must be a whole number and can be positive, negative or zero.
- The range of possible values depends on the computer system.
- Typically, int uses one machine word for storage. On an IBM PC, which has a 2 byte word, uses 2 bytes (16 bits) to store an int.
- This specification allows for a range of values from 032768 to +32767.
Declaring an int variable.
to declare an int variable:
eg:
int sum;
int marks, scores, value1;
the declaration above create variables, but they dont provide values for them.
To give a variable a value we can use an an assignment statement:
marks = 75;
Initializing a variable
if we know in advance the starting value for a variable we can initialize that variable at the same time it is declared. For example:
int sum = 0;
int marks=19, score =94;
int value, num = 76;
&
in the last line, only num is initialized to 76.
int Constants
- an integer constant is a number without a decimal point and without an exponent.
- Normally C assumes integer constants as decimal integers, that is, in the base 10. For example:
sum=45;
- the value 45 in the above statement is a decimal integer constant.
- A zero (0) prefix means that the integer number is in base 8 or an octal number, for example:
sum = 055;
- in the above example, the octal integer constant value 55 is assigned to the variable sum.
- a prefic of 0x or 0X means that the integer number is in base 16 or a hexadecimal number.
Sum=0x2D;
the above statement will assign the hexadecimal integer constant value 2D into sum.
Other int types
- C offers three keywords to modify integer type:
unsigned, long and short.
- the type short int or short, may use less storage than int, thus saving space when only small numbers are needed. It is also a signed type.
- The type long int, or long may use more storage than int, this allowing larger integers to be used, It is also a signed type.
- On an IBM PC, a long int takes up four bytes or 32 bits of storage. This allows for integer values in the range of -2,147,483,648 to +2,147,483,647 to be stored.
- The type unsigned int, or unsigned allows only the value 0 or positive values to be stored.
- An unsigned int takes up the same amount of storage as an int but only allows values in the range of 0 to 65535 to be stored on an IBM PC.
- ANSI C compilers will also recognize unsigned long int, or unsigned long, and insigned short int, or unsigned short, as valid types.
Declaring other integer types
Examples
long int gross_pay;
long tax;
short int value;
short pants;
unsigned int count;
unsigned players;
unsigned long headcount;
unsigned short votes;
Type unsigned int, long int, and unsigned long int constants
an unsigned int constant can be specified by appending the letter U to the end of the constant.
To cause a constant to be stored as type long int the suffix L can be added.
An unsigned long int may be specified by appending the letters UL to the end of the constant. Note that the U must precede the L.
Examples:
unsigned int levels;
long int score;
unsigned long int high_score;
levels = 500U;
score=5000l;
high_score=45000UL;
- the U,L and UL suffixes also can be used in octal and hexadecimal constants.
- The float and double types
- the types float and double are floating-point data types.
- Floating-point types allows representation of a much greater range of numbers, including decimal fractions.
- The type float typically uses 4 bytes store floating point values.
- The type floating type is available called long double, whose precision is greater than double.
&
The types unsigned double, and unsigned long double are NOT valid floating-point types. All floating point types list above stores positive and negative values.
Declaring Floating-Point variables
- floating-point variables are declared and initialized in the same manner as other variables.
Example:
float average, frequency;
double trouble;
float price=6.54;
long double gnp;
Floating-Point Constants
- the basic form to write a floating-point constant is as a series of digits including a decimal point.
For example :
3.14159 .2 100 .25
- another way to express floating-point constants is in exponential notation.
For example:
1.2e16 4e12 .8E-5 2.e8
- floating-point constants are treated as double precision
For example:
float number;
number = 4.0 * 2.0;
- 4.0 and 2.0 are stored as double by the compiler, the product is calculated using double-precision arithmetic, and the answer is converted to the float type. This procedure ensures greater precision for your calculations.
- C lets you specify how a floating point constant is stored.
- An f or F suffix on a floating point number makes it type float.
Example :
2.3f 4.5f 9.11e9F 2.1E2f
- An l or L suffix makes it type long double.
Example:
54.321 4.32e4L
- If the floating-point number has no suffix, its type is double.
The char Type
- this type is used for storing characters.
- Technically, it is an integer type because it actually stores integers, not characters.
- The computer uses numerical codes in which certain integers represent certain characters.
- The most commonly used code is the ASCII code.
The char type is defined as a one-byte (eight bits) units of memory.
Declaring type char variables
variables of type char are defined in the same manner as other variables:
char choice;
char letter1, letter2;
signed and unsigned char type
by default the type char is a signed type, which means it can hold values in the range -128 to +127.
- For an unsigned char type the range of values provided is from 0 to 255.
Characters constants and initialization
to initialize a char variable, a character constant can be assigned to it or the ASCII code for the character.
For example:
char my_grade = 65;
char your_grade = A;
In the above declarations, my_grade is initialized to the value of 65 which is the ASCII code for the character A, your_grade is initialized to the character constant A.
a single letter between single quotation marks is a C character constant.
When the compiler encoutners a character constant like A, it is converted to its proper ASCII code value (which is 65).
Example:
char big; /* declare a char variable */
big = B; /* assign letter B into big*/
big = B; /* No! B is treated as a variable! */
big = "B"; /* no! "B" is a string */
Non-printing Characters
- non-printing characters are characters that represents action such as backspace, going to the next line, or making the speaker beep.
- There are three ways to represent these non-printing characters:
- For example, the ASCII code for the beep character is 7.
char beep = 7;
Use a special for of the ASCII code.
- For example, the ASCII code can be represented in octal or hexadecimal form.
- The statement below stores the Ctrl-P character expressed as an ASCII code in hexadecimal form.
char ctrl_p = \x10;
&
the character \X010 is equivalent.
Use special symbol sequences called escape sequences..
\a |
alert |
\b |
backspace |
\f |
formfeed |
\n |
newline |
\r |
carriage return |
\t |
horizontal tab |
\v |
vertical tab |
\\ |
backslash (\) |
\ |
single quote () |
\" |
double quote (") |
- these escape sequences are also enclosed within single quotation marks
For example:
char beep = \a;
char netx_line;
next_line = \n;
Defining Program constants
- a constant is a value that will not change.
- To define a constant in C there are two ways:
- using the #define directive to define a symbolic constant.
- The general format to decalre a sy\mbolic constant:
#define constant_name value
example:
#define PI 3.14159
- in the above example, when the program is compiled, the value 3.14159 is substituted whenever the word PI is used.
- Using the const keyword to declare a constant variable.
- the general format to declare constant variable:
const const_name type = value;
example:
const int pi = 3.14159;
- in the above constant declaration, pi is just like a variable, only that its contents cannot be updated or modified.