C PROGRAMMING

By | 6th December 2019

=:C PROGRAMMING :=

An operator operators on variables and perform an action in a program. It consists of words or symbols. For instance, the arithmetic operators (+) and (-) cause us to add or subtract two numbers respectively.

Operators are essential to form expressions and to perform arithmetical and logical operators on variables.

We need operators to do arithmetic operators such as addition , subtraction , etc. Similarly, to perform relational comparison ,we need relational operators like  > , < , <= , >= etc. The programming language C supports the bitwise operators and increment / decrement operators. We shall see how to use them in various statements. Operators in C language may be classified as:

  • Arithmetic operators
  • Relational operators
  • Logical operators
  • Assignment operators
  • Pointer operators
  • Special operators
  • Bitwise operators

Arithmetic operators

An arithmetic operator is a symbol which performs an arithmetic operation namely, addition, subtraction, etc. The data on which such operators are carried out may be a variable or a constant.

            The operator thus operates on an operend . For instance, in the expression:

Table 3.7 Arithmetic Operators

          Operator                symbol                form                          operation             

Multiplication             *                        x * y                            x times y

Division                 /             x / y                             x divided by y

Remainder                  %           x % y               remainder of x divided by y  

Addition                +            x + y                y is added to x

Subtraction                 –             x – y                       y is subtracted form x

All the arithmetic operators are binary operators, since they operate on two operands at a time.

The new operator in Table 3.7 is the remainder(%) operator . The multiplication, division and remainder operators are called multiplication operators and have a higher precedence than the additive operators (addition and subtraction). These operators require two operands to operate upon.

The – operator is an unary operator when operates on one operand say – x. Unary operators take just a single operand. The operand can be any integer or floating point value.

Example

Figure 3.3 shows program to convert temperature in Fahrenheit to Centigrade .

         The program in Figure 3.3 demonstrates the conversion of temperature . It also shows the use of several arithmetic operators.

         Note that the program shows in Figure 3.3, we used parentheses around (ftemp – 32). This is because we want the number 32 to be subtracted form ftemp before we multiply it by 5 and then divided the resulting expression by 9. Since multiplication and division are carried out before addition and subtraction, we use parentheses to ensure that the subtraction is carried out first.

(*)and (/) have a higher precedence than (+)and (-)operetors.

The Remainder (modulus division) Operator(%)

Unlike other arithmetic operators which accept both integers and floating point operatands, the remainder operator or modulus operator accepts integer operands only.

The resulting value after the operator is the remainder of the first operand divided by the second operand. The remainder value is also an integer value. For example, in the expression given below, the variable answer will be assigned the value 3, since that is the remainder when 13 is divided by 5.

Precedence of Arithmetic Operators

Table3.8 shows the precedence or the preferences that the various arithmetic operators get and their associativity. Associativity means that in the same precedence, they are evaluated form left to right . But the increment and operators are evaluated from right to left.

Table 3.8 Precedence of arithmetic operators

Operator                                        Associativity                                                   Precedence

++ or —                                         Right to left                                                       Highest

–                                                    Left to right                                                       

*/%                                               Left to right

+-                                                  Left to right                                

                                                                                                                                Lowest

You can always use parentheses to change the order of calculation. For example :

2 + 8 / 2 is equal to 6

( 2 + 8 ) /2 is equal to 5

The expression “ 2 + 8 / 2” is evaluated as 6 since the division is done first ; while the expression :

“(2 + 8 ) / 2”

is evaluated as 5 because the addition ,between the parentheses, is done first.

Relational and Logical Operators

Relational operators such as greater than (>) or less than (<) are used to compare value between two variable and thus form relational expressions.

   The logical operator such as AND (&&) , OR (||) ,etc. Are used to connect relational expressions together using the rules of Boolean Algebra . Both these types of expression give TRUE or FALSE results. Here, FALSE is zero, while any nonzero number is TRUE.

The logical or relational expression give the value “1” for TRUE result and the value “0” for FALSE result.

This is because in both cases the addition is evaluated first. However, you will mak less mistake, if you use parentheses or the brackets ( ) .

Table 3.9 Relational and logical operators

Operator                                                             Action

Relational Operators:

>                                                                        Greater than

>=                                                                      Greater than equal

<                                                                         Less than

<=                                                                       Less than equal

= =                                                                      Equal

! =                                                                       Not equal

Logical operator:

&&                                                                     AND

||                                                                          OR

!                                                                          NOT

Evaluation of logical Expressions

To under how a logical expression is evaluated consider the following expression :

x = = y && w > = z

This expression means : “x is equal to y AND w is greater than or equal to z”, In other words the expression is evaluated as TRUE if both of the following conditions are True:

  • The expression x = = y is evaluated as TRUE, and
  • The expression w > = z is evaluated as TRUE.

If you replace the AND (&&) by an OR (||), than the expression is evaluated as TRUE if either one of the two expression or both the expression are evaluated as TRUE.

Precedence of Relational and Logical Operators

Table 3.10 shows the relative precedence of the relational and logical operators

Operator                                      Associativity                                             Precdence

!                                                    Right to left                                                 Highest

> > = < < =                                   Left to right                        

= = ! =                                           Left to right

&&                                                Left to right                                                             

||                                                     Left to right                                                 Lowest

In order to see how the precedence helps in evaluating an expression, we study the following expression.

A = = b && x = = y || m = = n

This expression is evaluated as TRUE if one of the following condition are met :

  • If a = = b is evaluated as TRUE, and x = = y is evaluated as TRUE, OR
  • If m = = n is evaluated as TRUE.

This means that the operator AND (&&) is evaluated before the operator OR (||)because the operator&& is having higher precedence than the operator ||. In other words, this expression is evaluated as if it had parentheses as shown below :

(a = = b && x = = y) || (m = = n)

Getch( ) and getch( )

These two functions are very similar, as they responds without pressing the ENTER key . The difference is that the function getche( ), the echo of the pressed key is displayed on the screen (the letter “e” stands for “echo”), but with the function getch( ), there is no echoing. Example29, demonstrates the  difference between these two functions.

When you use the function getch( ) or getche( ) in a program, you must include the header file conoi.h in the beginning of the program.

The if Statement

The if statement is a control statement that tests a particular condition. Whenever ,the evaluation condition comes out to be true, then that action are carried out. Otherwise, the given set of (s)are ignored. The syntax for this statement is :

   If (expression)

          Statement (s);

The expression must be enclosed within parentheses( ). If expression evaluates to a non-zero value, the condition is considered to be true and the next statement (s)following the closing parentheses is / are executed.

The if statement also allows answer for the kind of either-or condition by using an else clause.

The format of the if-else statement is as following :

If (expression)

     Statement_1;

Else  

     Statement_2;

When the expression within the parentheses in the set of statement evaluates to a nonzero value, the condition is considered to be true  and statement_ 1 is executed ; otherwise , statement_2 is executed. Note that statement_1 is terminated by a semicolon. For example, the sentence, “if the light is red, stop, otherwise go” would be written in C as:

If (light = = “red”)

      Stop;

Else

      Go;

Note the semicolon (;) after the word stop.

Another example:

If (quantity > 50)

  Discount = 0. 1 * amount;

Else discount = 0. 0;

These are interpreted as follows:

If quantity purchased is more than 50 then execute the next statement namely discount = 0.1 * amount, otherwise the statement following else i.e. discount = 0.0.

While Loop

While loop construct contains the condition first. If the condition is satisfied, the control executes the statements following the while loop else, it ignores these statements. The general form of while loop is :

While (condition)

{

    statement1;

    statement2;

     …….

}

The condition in the program is evaluated first. The block of statement following the word while are executed repeatedly until the value of the expression laying down the condition is false.

You should also take care of the initializing and incrementing steps while using construct.

   For example, conceder the followng program segments:

X = 1

While (x < 10)

{

    print (“x is % d \ n”, x);

    x + = 1;

} / * end of while * /

In this case , the statement within the parentheses{} following while statement are executed so long as the current value of x is less than 10.

INTRODUCTION OF TWO DIMENSIONAL ARRAYS

Two dimensional array are declared in C language in the same manner as one dimensional array , except that a separate pair of square brackets is required for each additional dimension of the array. Thus, a two dimensional array will require two pairs of square brackets. A two-dimensional array(having to subscripts) is suitable for processing table. For this purpose we use two subscripts enclosed in square brackets. The first subscript designation the row and the second subscript designation the column.

       To declare a two- dimensional array ,the first value you specify indicates the number of rows and the second value the number of columns.

For example,

   Int x [3] [5];

Is the declaration of an array x which has rows and 5 columns and stores integer data in each element. A two dimensional array is best visualized as a table consisting of rows and columns. Figure 5.29 pictorially represents such an array with 3 rows starting with 0 and ending with 2. Also the number of columns are 5 starting with 0 and ending 4.Each of the cells in this two-dimensional array can take an integer value because we have declared the array x to be of integer type. The array can be declared of any type much similar to the type of variables. But each element of the array can store only that type of data value . To refer a specific array element, you must specify the corresponding row and column position.

String Variables

Variables of type char can hold a single character, so they have limited usefulness. You also need a way to store strings, which are sequence of characters. A persons name and address are example of strings. Although there is no special data type for strings, C handles this type of information with array of character.

   Before a string can be read into a program, some space in the computer’s memory must be set aside for it. Since a string is composed as a series of characters, a series of bytes must be set aside for them. An array of characters representing a string is defind as follows:

Char array_name [size];

Size of array must be an integer value.

Leave a Reply

Your email address will not be published. Required fields are marked *