Showing posts with label c. Show all posts
Showing posts with label c. Show all posts

Precedence of Operators

Monday, May 28, 2012
The arithmetic operators in an expression are evaluated according to their precedence. The precedence means which operator will be evaluated first and which will be evaluated after that and so on. In an expression, the parentheses ( ) are used to force the evaluation order. The operators in the parentheses( ) are evaluated first. If there are nested parentheses then the inner most is evaluated first.

The expressions are always evaluated from left to right. The operators *, / and % have the highest precedence after parentheses. These operators are evaluated before + and – operators. Thus + and – operators has the lowest precedence. It means that if there are * and + operators in an expression then first the * will be evaluated and then its result will be added to other operand. If there are * and / operators in an expression (both have the same precedence) then the operator which occurs first from left will be evaluated first and then the next, except you force any operator to evaluate by putting parentheses around it.

The following table explains the precedence of the arithmetic operators:

Operators Operations Precedence (Order of evaluation)
( ) Parentheses Evaluated first
*, /, or % Multiplication, Division, Modulus Evaluated second. If there are several, they are evaluated from left to right
+ or - Addition, Subtraction Evaluated last. If there are several, they are evaluated from left to right

Lets look some examples.

What is the result of 10 + 10 * 5 ?

The answer is 60 not 100. As * has higher precedence than + so 10 * 5 is evaluated first and then the answer 50 is added to 10 and we get the result 60. The answer will be 100 if we force the addition operation to be done first by putting 10 + 10 in parentheses. Thus the same expression rewritten as (10 + 10) * 5 will give the result 100. Note that how the parentheses affect the evaluation of an expression.

Similarly the expression 5 * 3 + 6 / 3 gives the answer 17, and not 7. The evaluation of this expression can be clarified by writing it with the use of parentheses as (5 * 3) + (6 / 3) which gives 15 + 2 = 17. Thus you should be careful while writing arithmetic expressions.

Tips
  • Use spaces in the coding to make it easy to read and understand
  • Reserved words can not be used as variable names
  • There is always a main( ) in a C program that is the starting point of execution
  • Write one statement per line
  • Type parentheses ’( )’ and braces ‘{ }’ in pairs
  • Use parentheses for clarification in arithmetic expressions
  • Don’t forget semicolon at the end of each statement
  • C Language is case sensitive so variable names x and X are two different variables

For previous lesson click here: Arithmetic Operators
For next lesson click here: Sample Program


the easiest way to learn programming
introduction to programming
Precedence of Operators

Arithmetic Operators

In C language we have the usual arithmetic operators for addition, subtraction, multiplication and division. C also provides a special arithmetic operator which is called modulus. All these operators are binary operators which means they operate on two operands. So we need two values for addition, subtraction, multiplication, division and modulus.

Arithmetic OperationArithmetic OperatorAlgebraic ExpressionC Expression
Addition+x + yx + y
Subtraction-x - yx - y
Multiplication*xyx * y
Division/x ÷ y, x / yx / y
Modulus%x mod yx % y

Addition, subtraction and multiplication are same as we use in algebra.
There is one thing to note in division that when we use integer division (i.e. both operands are integers) yields an integer result. This means that if, for example, you are dividing 5 by 2 (5 / 2) it will give integer result as 2 instead of actual result 2.5. Thus in integer division the result is truncated to the whole number, the fractional part (after decimal) is ignored. If we want to get the correct result, then we should use float data type.

The modulus operator returns the remainder after division. This operator can only be used with integer operands. The expression x % y returns the remainder after x is divided by y. For example, the result of 5 % 2 will be 1, 23 % 5 will be 3 and 107 % 10 will be 7.

Sample Program 2
This is a sample program that uses the arithmetic operators. It displays the result of arithmetic operations on the screen. To make the result more visible we have used end line character (endl) with cout. So the statement:

cout << endl;

Simply ends the current line on the screen and the next cout statement displayed at the next line on the screen.

Code of The Program(Operation with integers)

Output of the program

Code of The Program(Operation with decimal numbers)

Output of the program

see everything clearly
// A Sample Program that uses Arithmetic Operators.

#include

main ()
{
//declaration of variables
int a;
int b;
int c;

float x;
float y;
float z;

// assigning values to variables
a = 23;
b = 5;
x = 12.5;
y = 2.25;

// processing and display with integer values
cout << "Operations with integers" << endl; cout << "a = " << a << endl; cout << "b = " << b << endl; //addition c = a + b; cout << "a + b = " << c << endl; //subtraction c = a - b; cout << "a - b = " << c << endl; //multiplication c = a * b; cout << "a * b = " << c << endl; // division c = a / b; cout << "a / b = " << c << endl; //modulus c = a % b; cout << "a % b = " << c << endl; // processing and display with decimal values cout << "Operations with decimal numbers" << endl; cout << "x = " << x << endl; cout << "y = " << y << endl; //addition z = x + y; cout << "x + y = " << z << endl; //subtraction z = x - y; cout << "x - y = " << z << endl; //multiplication z = x * y; cout << "x * y = " << z << endl; //division z = x / y; cout << "x / y = " << z << endl; } Output of the program
Operations with integers

a = 23
b = 5
a + b = 28
a - b = 18
a * b = 115
a / b = 4
a % b = 3

Operations with decimal numbers

x = 12.5
y = 2.25
x + y = 14.75
x - y = 10.25
x * y = 28.125
x / y = 5.55556

For previous lesson click here: Data Types
For next lesson click here: Precedence of Operators


the easiest way to learn programming
introduction to programming
Arithmetic Operators

Steps involved in writing and executing a program

Saturday, May 26, 2012
The following figure represents a graphical explanation of all the steps involved in
writing and executing a program.

For previous lesson click here: Tools Of Trade
For next lesson click here: IDE (Integrated Development Environment)


the easiest way to learn programming
introduction to programming
Steps involved in writing and executing a program

History of C Language

The C language was developed in late 60’s and early 70’s, in Bell Laboratories. In those days BCPL and B languages were developed at Bell Laboratories. The BCPL language was developed in 1967 by Martin Richards as a language for writing operating systems software and compilers. In 1970 Ken Thompson used B language to create early versions of the UNIX operating system at Bell Laboratories. Thus both the languages were being used to develop various system software even compilers. Both BCPL and B were ‘type less’ languages, every data item occupied one ‘word’ in memory and the burden of treating a data item as a whole number or real number, for example was the responsibility of the programmer.

Dennis Ritchie developed a general purpose language, called C language, by using different features of BCPL and B languages. C uses many important concepts of BCPL and B while adding data typing and other features. In the start C became widely known as the development language of the UNIX operating system, and the UNIX operating system was written by using this C language. The C language is so powerful that the compiler of C and other various operating systems are written in C. C language has almost unlimited powers to do with computers. You can program to turn on or off any device of computer. You can do a lot to hard disk and other peripherals. It is very easy to write a program in C that stops the running of computer. So be careful while programming in C.

The C language and UNIX operating system widely spread in educational and research institutions. There was C and UNIX everywhere. Due to the wide spread of C, different researchers started to add their features in the language. And thus different variations in C came into existence. Many universities developed their own C by adding different features to the C language developed by Ritchie. These variations led to the need of a standard version of C. In 1983 a technical committee was created under the American National Standards Committee on Computer and Information Processing to provide an unambiguous and machine-independent definition of the language. In 1989 the standard was approved. ANSI cooperated with the International Standard Organization (ISO) to standardize C worldwide.

There were revolutions in computer industry, there came new features and languages. So immediately after C came the C++. Stroustrup wrote the C++. First its name was ‘C with objects’ and then eventually became C++. It is an object oriented language having all powers of C. The development environment which we are going to use for programming is actually the environment of C++. It has objects, methods and many others thing. But we will avoid object oriented features in our course.

For previous lesson click here: types of system softwares
For next lesson click here: Tools Of Trade


the easiest way to learn programming
introduction to programming
History of C Language

 

introduction to programming Copyright © 2011-2012 | Powered by Blogger