Data Types and Input or Output Operators
Data Types and Input or Output Operators
3.1 Introduction
In the previous unit, you learned about the operators that a C programming
language supports. You also learned how the operators are used in the
expressions of C programs. In this unit, you will learn about the data types
that are supported in C. You will also study about the Input/output operators
which makes C as the most efficient and powerful programming language.
Integer is one of the fundamental data types. All C compilers support four
fundamental data types, namely integer (int), character (char), floating point
(float), and double-precision floating point (double). Like integer data type,
other data types also offer extended data types such as long double and
signed char.
C supports a rich set of operators. We have already used several of them,
such as =, +, -, *, / and %. An operator is a symbol that tells the computer to
perform certain mathematical or logical manipulations. Operators are used
in programs to manipulate data and variables. They usually form a part of
the mathematical or logical expressions.
Manipal University Jaipur Page No.: 36
Programming in C Unit 3
Table 3.1: Floating point numbers and qualifiers and their size and range on a
16-bit machine.
x = 1.234567880630
x = 1.234568
y = 9.876543210000
y = 9.876543
k = 54321 p = 1.000000 q= 1.000000000000
Program 3.2: Program to calculate the average of N numbers
#define N 10 /* SYMBOLIC CONSTANT */
main()
{
int count; /* DECLARATION OF
float sum, average, number; VARIABLES */
sum = 0; / * INITIALIZATION OF
count = 0; VARIABLES*/
while (count<N)
{
scanf(“%f”, &number);
sum = sum + number;
count = count + 1;
}
average = sum / N;
printf(“N = % d Sum = %f “, N, sum);
printf(“Average = %f”, average);
Output
1
2.3
4.67
1.42
7
3.67
4.08
2.2
4.25
8.21
N= 10 Sum= 38.799999 Average= 3.880000
Given below is the sequence of rules that are applied while evaluating
expressions.
All short type are automatically converted to int ; then
1. If one of the operands is long double, the other will be converted to
long double and the result will be long double;
2. else, if one of the operands is double, the other will be converted to
double and the result will be double;
3. else, if one of the operands is float, the other will be converted to float
and the result will be float;
4. else, if one of the operands is unsigned long int, the other will be
converted to unsigned long int and the result will be unsigned long
int;
5. else if one of the operands is long int and the other is unsigned int,
then:
if unsigned int can be converted to long int, the unsigned int
operand will be converted as such and the result will be long int;
else, both operands will be converted to unsigned long int and the
result will be unsigned long int;
6. else, if one of the operands is long int , the other will be converted to
long int and the result will be long int;
7. else, if one of the operands is unsigned int , the other will be converted
to unsigned int and the result will be unsigned int;
The final result of an expression is converted to type of the variable on the
left of the assignment sign before assigning the value to it. However, the
following changes are introduced during the final assignment:
1. float to int causes truncation of the fractional part.
2. double to float causes rounding of digits.
3. long int to int causes dropping of the excess higher order bits
3.2.2 Mixed-mode Expressions
When one of the operands is real and the other is integer, the expression is
called a mixed-mode arithmetic expression. If either operand is of the real
type, then only the real operation is performed and the result is always real
number. Thus
25 / 10.0 = 2.5
Whereas 25 / 10 =2
Note that with the format characters %d, the ASCII number of the character
is displayed. With the format character %c, the character corresponding to
the given ASCII number is displayed.
Self Assessment Questions
9. What is the format character to display the value of a char variable?
10. The output of the following C statement:
printf(“%c”, 70); is ___________.
3.5 Keywords
Keywords are the reserved words of a programming language. All the
keywords have fixed meanings and these meanings cannot be changed.
Keywords serve as basic building blocks for program statements. The list of
all keywords in ANSI C are listed in the Table 3.3.
Table 3.3: ANSI C Keywords
EOF will be assigned the value -1, but this may vary from one compiler to
another.
The syntax of the getchar() function is written as
character variable= getchar()
where character variable refers to some previously declared character
variable.
Example:
char c;
…
c=getchar();
The first statement declares that c is a character-type variable. The second
statement causes a single character to be entered from the keyboard and
then assign to c.
A companion function is putchar(), which writes one character to the
“standard output.'' (The standard output is usually the user's screen).
The syntax of the putchar() function is written as
putchar(character variable)
where character variable refers to some previously declared character
variable.
Example:
char c;
…
putchar(c);
The first statement declares that c is a character-type variable. The second
statement causes the current value of c to be transmitted to the user monitor
where it will be displayed.
Using these two functions, we can write a very basic program to copy the
input, a character at a time, to the output:
%c a single character
%d a decimal integer
%i an integer
%e, %f, %g a floating-point number
%o an octal number
%s a string
%x a hexadecimal number
%p a pointer
%n an integer equal to the number of characters read so far
%u an unsigned integer
%[] a set of characters
%% a percent sign
scanf() reads the input, matching the characters from format. When a
control character is read, it puts the value in the next variable. Whitespaces
(tabs, spaces, etc) are skipped. Non-whitespace characters are matched to
the input, then discarded. If a number comes between the % sign and the
control character, then only that many characters will be entered into the
variable. If scanf() encounters a set of characters, denoted by the %[]
control character, then any characters found within the brackets are read
into the variable. The return value of scanf() is the number of variables that
were successfully assigned values, or EOF if there is an error.
This example illustrates the use of the scanf() function to enter a string
consisting of uppercase letters and blank spaces. Please note that if you
want to allow lowercase letters to be entered, all the lowercase letters
(i.e. from a-z) must be included in the list of control string.
Formatted Output
Output data can be written from the computer onto a standard output device
using the library function printf(). This function can be used to output any
combination of numerical values, single characters and strings. It is similar
to the input function scanf(), except that its purpose is to display data rather
than enter into the computer.
The syntax of the printf function can be written as follows:
printf(control string, arg1, arg2, …, argn)
where control string refers to a string that contains formatting information,
and arg1, arg2, …, argn are arguments that represent the individual output
data items. The arguments can be written as constants, single variable or
array names, or more complex expressions.
Examples:
printf("Hello, world!\n");
printf("i is %d\n", i);
printf(“%d”, 10);
printf(“%d”, i+j);
The first statement simply displays the string given as argument to the
printf() function. In the second statement, printf() function replaces the two
characters %d with the value of the variable i. In the third statement the
argument to be printed is a constant and in the fourth, the argument is an
expression.
There are quite a number of format specifiers for printf(). Some of them are
listed in Table 3.5.
It is also possible to specify the width and precision of numbers and strings
as they are inserted ; For example, a notation like %3d means to print an
int in a field at least 3 spaces wide; a notation like %5.2f means to print a
float or double in a field at least 5 spaces wide, with two places to the right
of the decimal.)
To illustrate with a few more examples: the call
printf("%c %d %f %e %s %d%%\n", '3', 4, 3.24, 66000000, "nine", 8);
would print
3 4 3.240000 6.600000e+07 nine 8%
The call
printf("%d %o %x\n", 100, 100, 100);
would print
100 144 64
Successive calls to printf() just build up the output a piece at a time, so the
calls
printf("Hello, ");
printf("world!\n");
would also print Hello, world! (on one line of output).
Earlier we learned that C represents characters internally as small integers
corresponding to the characters' values in the machine's character set
(typically ASCII). This means that there isn't really much difference between
a character and an integer in C; most of the difference is in whether we
choose to interpret an integer as an integer or a character. printf is one
place where we get to make that choice: %d prints an integer value as a
Manipal University Jaipur Page No.: 52
Programming in C Unit 3
string of digits representing its decimal value, while %c prints the character
corresponding to a character set value. So the lines
char c = 'A';
int i = 97;
printf("c = %c, i = %d\n", c, i);
would print c as the character A and i as the number 97. But if, on the other
hand, we called
printf("c = %d, i = %c\n", c, i);
we'd see the decimal value (printed by %d) of the character 'A', followed by
the character (whatever it is) which happens to have the decimal value 97.
You have to be careful when calling printf(). It has no way of knowing how
many arguments you've passed it or what their types are other than by
looking for the format specifiers in the format string. If there are more format
specifiers (that is, more % signs) than the arguments, or if the arguments
have the wrong types for the format specifiers, printf() can misbehave badly,
often printing nonsense numbers or (even worse) numbers which mislead
you into thinking that some other part of your program is broken.
3.10 Summary
Floating point(or real) numbers are stored in 32 bit (on all 16 bit and 32 bit
machines), with 6 digits of precision. Floating point numbers are defined in
C by the keyword float. When the accuracy provided by a float number is
not sufficient, the type double can be used to define the number.
Characters are usually stored in 8 bits (one byte) of internal storage. Like
integer data type other data types also offer extended data types such as
long double and signed char. C permits mixing of constants and variables
17. True
18. 64, 12, 4B
19. %o
20. a
21. False
22. String
23. False
3.14 Exercises
1. Represent the following numbers using scientific notation:
a) 0.001 b) -1.5
2. Represent the following scientific numbers into decimal notation:
a) 1.0E+2 b) 0.001E-2
3. What is unsigned char? Explain.
4. What is short char? Explain.
5. Distinguish between float and double data types.
6. Write a program to print the factors of a given number.
7. Given the length of a side, write a C program to compute surface area
and volume of a cube.
8. Write a program to reverse a number and find sum of the digits.
9. Write a program to print the multiplication table for any given number.
10. Write a program to check whether a given number is palindrome.