Relational and Logical Operators in C

By: Tamil Selvan Emailed: 1643 times Printed: 2109 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

The relational operators are
   >   >=   <   <=
They all have the same precedence. Just below them in precedence are the equality operators:
   ==   !=
Relational operators have lower precedence than arithmetic operators, so an expression like i < lim-1 is taken as i < (lim-1), as would be expected.

More interesting are the logical operators && and ||. Expressions connected by && or || are evaluated left to right, and evaluation stops as soon as the truth or falsehood of the result is known. Most C programs rely on these properties. For example, here is a loop:

   for (i=0; i < lim-1 && (c=getchar()) != '\n' && c != EOF; ++i)
       s[i] = c;
Before reading a new character it is necessary to check that there is room to store it in the array s, so the test i < lim-1 must be made first. Moreover, if this test fails, we must not go on and read another character.

Similarly, it would be unfortunate if c were tested against EOF before getchar is called; therefore the call and assignment must occur before the character in c is tested.

The precedence of && is higher than that of ||, and both are lower than relational and equality operators, so expressions like

   i < lim-1 && (c=getchar()) != '\n' && c != EOF
need no extra parentheses. But since the precedence of != is higher than assignment, parentheses are needed in
   (c=getchar()) != '\n'
to achieve the desired result of assignment to c and then comparison with '\n'.

By definition, the numeric value of a relational or logical expression is 1 if the relation is true, and 0 if the relation is false.

The unary negation operator ! converts a non-zero operand into 0, and a zero operand in 1. A common use of ! is in constructions like

   if (!valid)
rather than
   if (valid == 0)
It's hard to generalize about which form is better. Constructions like !valid read nicely (``if not valid''), but more complicated ones can be hard to understand.

C Home | All C Tutorials | Latest C Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(0)


Be the first one to add a comment

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Tamil Selvan
Inner Class Example in Java
PreparedStatement Example in Java
UNIX read and write system calls sample program in C
Pointer Arrays and Pointers to Pointers in C
Relational and Logical Operators in C
Arithmetic Operators in C
strlen() sample program in C++
Sample program to draw a arc in J2ME
What Java Has Removed from C++
FileReader and FileWriter example program in Java

More Tutorials in C
Sum of the elements of an array in C
Printing a simple histogram in C
Sorting an integer array in C
Find square and square root for a given number in C
Simple arithmetic calculations in C
Command-line arguments in C
Calculator in C
Passing double value to a function in C
Passing pointer to a function in C
Infix to Prefix And Postfix in C
while, do while and for loops in C
Unicode and UTF-8 in C
Formatting with printf in C
if, if...else and switch statements in C with samples
Statements in C

More Latest News
Most Viewed Articles (in C )
Using memset(), memcpy(), and memmove() in C
Printing a simple histogram in C
lseek() sample program in C
UNIX read and write system calls sample program in C
Sum of the elements of an array in C
Find square and square root for a given number in C
perror() Function - example program in C
Listing Files and Directories sample program in C
Using free() Function in C
Using realloc() Function in C
assert() Function Example program in C
Fopen and Getc implementation program in C
Formatting with printf in C
fgets(), fputs() - Line Input and Output - sample program in C
Using Shift Operators in C
Most Emailed Articles (in C)
Multi-dimensional Arrays in C (Explained using date conversion program)
Macro Substitution using #define in C
Open, Creat, Close, Unlink system calls sample program in C
Formatting with printf in C
Unicode and UTF-8 in C
The for statement in C
Arguments - Call by Value in C
Character Arrays in C
Initialization of Variables in C
Pointer Arrays and Pointers to Pointers in C
Pointers vs. Multi-dimensional Arrays in C
Using Bit-field in C
UNIX read and write system calls sample program in C
lseek() sample program in C
Passing double value to a function in C