Script 3 1
Script 3 1
1. Introduction
In the previous module we saw that every C programme had void main() or simply
main() as an integral part. The word main must appear one, and only once in every
C program. This is the point where execution begins when the program is run. It
does not have to be the first statement in the programme but it serves as an entry
point.
The term 'main' here is actually a function definition. Functions usually return a
value, and the term ‘void’ is used to denote that the main program does not return a
value. Following the “main” program name is a pair of parentheses, which are an
indication to the compiler that this is a function. We will learn more about functions
later, and we will see that C programs consist essentially of a collection of functions.
The two braces below 'main' are used to define the limits of the programme. We
saw that the actual program statements go between the two braces. We also saw
variables of different types, and how we declare and assign values to a variable.
In this module we will learn more about manipulating data using a C programme.
The basic data structure used in C programs is a number. C provides for two types
of numbers - integers and floating point numbers. Computer operations that involve
text, or character strings, also work essentially on numbers. The possible values of
an 'int' variable in C ranges from -32,768 to 32,767. Whereas character variables,
denoted by the data type 'char', use numbers from 0 to 255 to represent characters.
This number is called the ASCII equivalent of the character. The limit of 256 comes
from the fact that only one byte, or 8 bits, of memory is used to store one character.
All the operations possible on integers are possible on character data also.
Note that the data definitions are always given before any executable statements in
any program block in C. This is why the variables are defined first in any C
programme.
1
expressions. For example, 5 is an expression that has value 5, a is an expression
and the value is the same as the value of the variable a. 5 + 3 is an expression that
has value 8. a / b is another expression and the value depends on the values of a
and b.
Assignment operators
There are many assignment operators in C. They assign the value of an expression
to an identifier.
identifier = expression;
An assignment operation assigns the value of what the right hand side of the
operator '=' evaluates to, to the variable on the left hand side. The left-hand
operand of an assignment operation must be a variable identifier, and on the right
hand side we have the 'assignment expression'. Note that we cannot write
5 = a + b;
a + c = 5;
We can use many assignment operators in a single line, and the operator has right-
to-left associativity. It means that the operations on the right side are carried out
first.
For example,
a = b = c = 25;
The compiler finds the value 25, assigns it to c, then continues to the left. It assigns
the value of c to b, and then that value gets assigned to a.
There are also other assignment operators like multiplication assignment (*=),
2
addition assignment (+=), Division assignment (/=), Subtraction assignment (-=) etc.
a += 5; is equivalent to writing a = a + 5;
and b /= 2.5; means b = b / 2.5;
* and / have higher precedence compared to + and -. All these four operations are
left-to-right associative. It means that 2 + 3 * 5 refers to 2 + (3 * 5), and 2 - 3 - 4
refers to (2-3) - 4.
As you see, these operators work on two operands, or two numbers, at a time. That
is why they are called binary operators.
Apart from these four basic operators, there is modular division, using the %, or the
modulo operator. It gives the remainder when the first variable is divided by the
second. It can only be applied to int or char type variables, and to extended integer
variables such as long, short, etc.
3
The result of an arithmetic operation is determined by the data types of the
operands. For example, in the following expression 2.5 * 3 evaluates to 7.5, a
floating point number.
But if we assign this value to another variable using an assignment operator, the
value that gets assigned will depend on the type of the destination variable.
Other than the binary operators, C supports relatively less intuitive 'unary'
operations like ++ and - -. They work on a single operand, or on a single variable.
It can appear as a prefix or a suffix operator, i.e, ++a; or a++. Same is the case with
--. ++a; (or a++;), as stand-alone statements, are both equivalent to the statement
a = a + 1;
We will see this difference using an example. See the following sequence of
statements.
x = 5; // assigns value 5 to x
x++; // increments x, x becomes 6
++x; // increments x, x becomes 7
c = x++; // c gets assigned the old value of x (7), increments x (to 8)
b = ++x; // increments x again (to 9), and b gets assigned the new value (9)
4
b = --x; // first decrements x (to 8), and b gets value 8
c = x--; // c gets value 8, x becomes 7
sizeof operator
This operator returns the size of the operand in bytes. The syntax is
sizeof(<variable name>). For example, sizeof(x). The ‘sizeof’ operator always
precedes its operand. If the operand is an integer, ‘sizeof’ will return the value 2 and
it means that an integer is represented using two bytes. If it is float, the value
returned will be 4 and if it is double it will be 8 , if operand is a char, it will be 1 only.
There are four relational operators in C. They are < ( less than ), <= ( less than or
equal to ), > ( greater than) and >= ( grater than or equal to )
In addition to these operators there are two equality operators also in C . They are
== ( equal to ) and != ( not equal to ).
C also contains three logical operators. They are && ( and ), || (or) and ! (not).
They correspond to the logical operations AND, OR and NOT respectively. The
NOT operator, the exclamation, is used to invert the result of any logical compare.
Using these three logical operators, we can make complex condition expressions.
5
For example,
((x < 60) || (x >= 80)) evaluates to true if x lies below 60 or if its value is 80 or
more.
((x > 0) && (x % 2 == 0) && (x % 10 != 0)) is true for all positive even integer
values of x that are not multiples of ten.
!(x > 10) is same as (x <= 10), and this expression is true for all values of x upto
(and including) 10.
Such expressions are used in conditional operators, conditional statements that use
'if' keyword, and in loops that run based on certain conditions. We will see them in
more detail later.
Examples:
6
Here if a > b , the value of a will be assigned to c ob value of b will be assigned to c
and c will have the maximum of a and b
Here if b >= 4.5 , c will get the value 8.0 else c will get 9.5
It is easy to convert an integer into a float: simply assign it the new value and the
system will do the proper conversion. For example, x = 5; (where x is a variable of
7
type float).
The other way is slightly more complicated because there may be a fractional part
that will have to be truncated or rounded off. The rule is that it gets truncated
always.
For example,
int x;
x = 7.7; assigns the value 7 to variable x since x is defined as an integer.
4 . Hierarchy of operations
While executing an arithmetic statement that has two or more operators, the
computer will get confused unless we have clear rules to decide in what order are
these operations carried out.
For instance, if we write 2*x-3, does it correspond to (2x)-(3) or 2(x-3)? Even if the
two operators are the same such a confusion could arise: 2/3/4 could mean (2/3) / 4
or 2 / (3/4).
For solving such issues, we define the precedence among operators, and
associativity rules for each operation.
Precedence order tells us that which operator gets precedence, and the
associativity tells us in what order the operations are to be carried out when one
operator appears multiple number of times in an expression. Multiplication and
division get priority over plus or minus. All these four operators are left to right
associative. Unary increment and decrement operators ++ and - - have higher
precedence, and they are right to left associative. Typecast operators have higher
precedence over multiplication and division but have lower precedence compared
to the unary operators ++ or - -. See the table for a complete list.
It is always safe to use parentheses to ensure the operations take place in the
8
order that we desire. A list of precedence and associativity of operations can be
seen on the screen.
Left to Right
Logical AND &&
Left to Right
Logical OR ||
9
Ternary conditional
?: Left to Right
Assignment = Right to Left
*= /= Right to Left
%= Right to Left
+= -= Right to Left
&= Right to Left
^= |= Right to Left
<<= >>= Right to Left
Comma , Left to Right
5. Library Functions In C
Library functions are procedural abstractions that carry out various commonly used
mathematical functions and calculations in C. Some library functions like scanf and
printf are used to input and output data. Library functions are usually grouped
together as object programs in separate library files. These library files are supplied
as the part of C compilers. Some library functions return a value TRUE or FALSE or
1 or 0. Some library functions perform some operation on data but do not return any
value.
There are many functions related to input output operations such as printf() ,
scanf(), getchar (), putchar() etc. There are function that perform on character
strings, and mathematical operations etc. For mathematical and special kinds of
function, suitable header files should be included in the programme
#include <stdio.h>
main()
{
1
int c, d;
c = getchar() ;
d = toupper(c);
putchar(d);
}
While run, this program accept a character from keyboard and store into c throught
the function getchar() , the function toupper() convert the stored value into upper
character and store in d: putchar() function display the value of variable d on
screen.
Summary
Let us summarize what we have learned till now. In this chapter we studied
expression and operators. There are different kinds of operators. They are binary
arithmetical operators, unary arithmetical operators, assignment operators,
relational and logical operators, conditional operators etc. Then we discussed about
how different kind of operators can be used together in an expression. Later we
understood the hierarchy or the order in which operators work when many of them
are in use simultaneously. At the end we discussed about the library functions and
their uses.
Questions
Now here are few questions for you. Please try to find out answers.
11
6) The following program contains a run-time error. Find the error and fix it.
main()
{
int x,y;
x = 1/y;
}
7) Find out some important header files and library functions associated with
them.
References
Link : http://www.iu.hio.no/~mark/CTutorial/CTutorial.html