In this lesson you will learn:
- To write programs using different data types
- identify different data types, literals
Q: Do you know how to save a drowning lawyer?
A: Take your foot off his head.
Data types and constants
The four basic data types are:
1. Integer
These are whole numbers, both positive
and negative. Unsigned integers (positive values only) are
supported. In addition, there are short and long integers.
The keyword used to define integers is:
int
An example of an integer is 32. An example of declaring
an integer variable called sum is:
int sum;
sum = 20;
2. Floating Points
These are numbers which contain
fractional parts, both positive and negative. The keyword
used to define float variables is:
float
An example of a float value is 34.12. An example of declaring
a float variable called money is:
float money;
money = 0.12;
3. Double
These are exponential numbers,
both positive and negative. The keyword used to define double
variables is:
double
An example of double value is 3.02E2. An example of declaring
a double variable called big is:
double big;
big = 312E+7
4. Character
These are single characters. The
keyword used to define character variables is:
char
An example of a character value is the letter A. An example
of declaring a character variable called letter is:
char letter;
letter = ‘A’;
Note the assignment character A to the variable letter is
done by enclosing the value in single quotes. Remember the
golden rule: Single character – Use single quotes.
String Literals:
A string literal is a sequence
of letters, digits, or symbols enclosed in double quotation
marks (“ “). For example:
”this is an example
of a string literal”
‘\0’ is implicitly added to the end of the string
via double quote use.
String literals have the type char[]. This is an array
of characters. The maximum number of bytes (characters)
is 4096. It is automatically null terminated by the compiler.
It is similar to a character array.
The conversion characters
Following is a list of some of
the conversional characters and the way they
Are used in the printf() statement:
d decimal
notation
c character notation
s string notation
f floating point
notation
Each of these is used following a percent sign to indicate
the type of output conversion, and between those two character,
the following fields may be added:
- left
justification in its field
(n) a number specifying
the minimum field width
. to
separate n from m
(m) significant
fractional digits for a float
l to
indicate a long
Sample program illustrating each data type:
#include <stdio.h>
int main(void)
{
int sum;
float money;
char letter;
double pi;
sum = 10; /* assign integer value */
money = 2.21; /* assign float value */
letter = ‘A’; /* assign a character value
*/
pi = 2.01E+6; /* assign a double value */
printf(“ value of sum = %d\n”, sum);
printf(“ value of money = %f\n”, money);
printf(“ value of letter = %c\n”, letter);
printf(“ value of double = %e\n”, pi);
return 0;
}
Sample program output:
Value of sum = 10
Value of money = 2.210000
Value of letter = A
Value of pi = 2.010000e+06
More about float and double variables
C displays both float and double
variables to sex decimal places This does not
Refer to the precision (accuracy) of which the number is
actually stored, only how many decimal places printf() used
to display these variables.
The following program illustrated how the different data
types are declared and displayed:
#include <stdio.h>
int main(void)
{
int sum = 100;
char letter = ‘Z’;
flaot set1 = 23.567;
double num2 = 11e+23;
printf(“ integer value is %d\n”, sum);
printf(“ float variable is %f\n”, set1);
printf(“ character is %c\n”, letter);
printf(“ double variable is %e\n”, num2);
return 0;
}
Sample program output:
Integer variable is 100
Character variable is Z
Float variable is 23.567000
Double variable is 11.000000e23
To change the number of decimal places printed out for
float or double variables, modify the %f or %e to include
a precision value, e.g:
printf(“ float variable
is %.2f\n”, set1);
Special note about data type conversion:
Consider the following program:
#include <stdio.h>
int main(void)
{
int value1 = 12, value2 = 5;
float answer = 0;
answer = value1 / value2
printf(“The value of %d divided by %d is %f\n”,
value1, value1, answer);
return 0;
}
Sample program output:
The value of 12 divided by 5 is 2.000000
Even though the above declaration seems to work, it is
not always 100% reliable. Note that this answer does not
contain a proper fractional part (i.e. all zero’s).
To ensure that the correct result always occurs, the data
type of value1 and value2 should be converted to a float
type before assigning to the float variable answer. The
following illustrates how this can be done:
answer = (float)value1/(float)value2;
Printing & formatting variable types
#include <stdio.h>
int main(void)
{
int a = 1023; /*
simple integer type */
char c = ‘a’; /*
character type */
char s[] = “Hello”; /*
string up to 256 characters */
float f = 3.14159; /*
float point type */
printf(“a = %d\n”, a);
// decimal output
printf(“c = %c\n”, c);
// ASCCII string output
printf(“s = %s\n”, s);
// ASCII string output
printf(“f = %f\n”, f); //
floating output
printf(“a = %7d\n”, a); //
use a field width of 7
printf(“a = %-7d\n”, a); // left justify
in a field of 7
printf(“f = %.3f\n”, f); //
use 3 decimal places
return 0;
}
back to top exercises lesson
2 lesson
4