C Programming For Beginners - Integer Expressions, Operators and Precedence

Written by:  • Edited by: J. F. Amprimoz
Updated Dec 19, 2008

In this article, we discuss how integer expressions are formed using “arithmetic operators”. We also explain how expressions are evaluated based on “operator precedence”.

Integer expressions

In C programming, an integer constant (e.g. 23, 0, -245) is the simplest example of an integer expression. However, most of the time, we write integer expressions by combining constants and variables with the following arithmetic operators:

+ add

- subtract

* multiply

/ divide

% find remainder

For example, suppose we have the following declaration:

int a, b, c;

then the following are all valid expressions:

a + 39

a + b - c * 2

b % 10 //the remainder when b is divided by 10

c + (a * 2 + b * 2) / 2

The operators +, - and * all give the expected results. However, / performs integer division; if there is any remainder, it is thrown away. We say integer division truncates. Thus 19 / 5 gives the value 3; the remainder 4 is discarded.

But what is the value of -19 / 5? The answer here is –3. The rule is that, in C, integer division truncates towards zero. Since the exact value of –19 ÷ 5 is –3.8, truncating towards zero gives –3.

The % operator gives the remainder when one integer is divided by another; for example,

19 % 5 evaluates to 4;

j % 7 gives the remainder when j is divided by 7;

You can use it to test, for instance, if a number j is even or odd. If j % 2 is 0 then j is even; if j % 2 is 1, j is odd.

Precedence of operators

C evaluates an expression based on the usual precedence of operators: multiplication and division are done before addition and subtraction. We say that multiplication and division have higher precedence than addition and subtraction. For example, the expression

5 + 3 * 4

is evaluated by first multiplying 3 by 4 (giving 12) and then adding 5 to 12, giving 17 as the value of the expression.

As usual, we can use brackets to force the evaluation of an expression in the order we want. For example,

(5 + 3) * 4

first adds 5 and 3 (giving 8), and then multiplies 8 by 4, giving 32.

When two operators which have the same precedence appear in an expression, they are evaluated from left to right, unless specified otherwise by brackets. For example,

24 / 4 * 2

is evaluated as

(24 / 4) * 2

(giving 12) and

12 - 7 + 3

is evaluated as

(12 - 7) + 3

giving 8. However,

24 / (4 * 2)

is evaluated with the multiplication done first, giving 3, and

12 - (7 + 3)

is evaluated with the addition done first, giving 2.

In C, the remainder operator % has the same precedence as multiplication (*) and division (/).

Exercise: What is printed by the following program? Verify your answer by typing and running the program. Note: use greater than and less than signs to properly enclose 'stdio.h' instead of parantheses.

#include (stdio.h)
main() {int a = 15:int b = 24;printf("%d %d\n", b - a + 7,b - (a + 7));printf("%d %d\n", b - a - 4, b - (a - 4));printf("%d %d\n", b % a / 2, b % (a / 2));printf("%d %d\n", b * a / 2, b * (a / 2));printf("%d %d\n", b / 2 * a, b / (2 * a));}
}

Comments

Showing all 9 comments
 
arungopakumar Oct 9, 2011 7:42 AM
RE: C Programming For Beginners - Integer Expressions, Operators and Precedence
#include<stdio.h>
#include<conio.h>
int main(void)
{
int i,j;
clrscr();
printf("*****\n\n");
for(i=0;i<3;i++)
{
printf("***\n\n");
}
printf("*****\n");
getch();
return 0;
}
arun Oct 9, 2011 7:06 AM
RE: C Programming For Beginners - Integer Expressions, Operators and Precedence
#include<stdio.h>
#include<io.h>
#include<conio.h>
int main(void)
{
int i,j,rows=3;
clrscr();
printf("***********\n\n");
for(i=0;i<rows;i++)
{
printf("**\n\n");
}
printf("***********\n\n");
getch();
return 0;
}
Noel Kalicharan Dec 8, 2010 8:46 AM
To Kaushik
You don't need to write a program to make the calendar. Any word processor or spreadsheet (e.g. Word or Excel) will do the job more easily.
kaushik Dec 8, 2010 8:30 AM
how to design the calender using c programming
sir i wanna design a calender of the year 2011.
so can you please send me the program via email??
Noel Kalicharan Jun 29, 2010 7:46 AM
Re: confusion with assignment
It is always a good idea to run your code and see what you get. This topic is dealt with here:

http://www.brighthub.com/computing/linux/articles/11239.aspx
skatergirl Jun 29, 2010 6:48 AM
confusion with assignment
if i say float a,b
int c
a=c/b;
what answer will i get say c=15, b=6;
will a be 2.5 or 2
great May 20, 2010 10:13 PM
Good day sir
i need to know more about how to paranthesize the expressions in c programming thanks
delphigeist Feb 4, 2010 11:56 AM
At your service Christine
#include <stdio.h>
#include <io.h>

int main(void)
{
int rows = 4, cols = 2;
int i, j;

printf("**************\n");

for(i = 0; i < rows; i++)
{
printf("\n");
for(j = 0; j < cols; j++)
{
printf("* ");
}
}
printf("\n\n**************\n\nPress any key to exit program...");
getch();
return 0;
}

the above application is written in latest version of TinyC 0.9.25, but this is a very standard program, therefore it should work in ANY C compiler
Christine Jan 15, 2010 2:16 PM
C programming to create a rectangle, oval and/triangle
Good day Sir - Can you assist me.

I need to know a simple program to create a rectangle - using *
Output

**************
* *
* *
* *
* *
**************

thanks
 
blog comments powered by Disqus
Email to a friend