C Programming language

C Standard Library Function

Day 1: Introduction and brief history of C Programming language

Day 1: Advantages and Disadvantages of C, C Keywords, Data type modifiers in C

Day 1: Data types in C Programming language

Day 1: Secondary data types, Primitive and Non-primitive data types

Day 1: C Variables, C Constant, Format Specifiers in C

Day 2: Write first C program

Day 2: Flow of C program with example, main(), printf(), scanf()

Day 2: Operaters in C Programming language, Arithmetic operators

Day 2: Relational operators and Logical Operators in C Programming language

Day 2: Assignment, Increments and Decrement Operators in C Programming language

Day 3: Conditional statement: if else statement in C Programming language

Day 3: Conditional statement: switch statement in C Programming language

Day 3: Jump statements: return statement in C Programming language

Day 3: Jump statements: go to statement in C Programming language

Day 3: Jump statements: break statement in C Programming language

Day 3: Jump statements: continue statement in C Programming language

Day 4: Loops OR Iteration statement in C Programming language: for Loop

Day 4: Loops OR Iteration statement in C Programming language: while Loop

Day 4: Loops OR Iteration statement in C Programming language: do while Loop

Day 5: Array in C Programming language

Day 5: Access elements of Array in C Programming language

Day 5: One dimensional Array representation in memory using C Programming language

Day 5: Two dimensional Array representation in memory using C Programming language

Day 5: Multidimensional Array in C Programming language

Day 6: Function in C Programming language

Day 6: Definition, Declaration and Calling a Function in C Programming language

Day 6: Passing array to a function in C Programming language

Day 6: Calling Function in C Programming language : Call by value

Day 6: Calling Function in C Programming language : Call by reference

Day 6: Recursive Function in C Programming language

Day 6: Adding function to the library in C Programming language

Day 7: Pointer in C Programming language, How to use Pointer, Pointer declaration

Day 7: NULL Pointers in C Programming language

Day 7: Array of Pointers in C Programming language

Day 7: Pointer arithmetic in C Programming language

Day 7: Pointer to Pointer in C Programming language

Day 7: Pointer to Function in C Programming language: Passing pointers to functions

Day 7: Pointer to Function in C Programming language: Return pointer from functions

Day 8: Strings in C Programming language, Declaring String in C Programming language

Day 8: String functions in C Programming language

Introduction of Structure

Accessing the members of Structure

Structure With typedef Keyword and Use of sizeof function

Example of Structure

Dynamic memory allocation in C: Introduction

adplus-dvertising
Day 2 Relational operators and Logical Operators in C Programming language
Previous Home Next

Relational operators

Often it is required to compare the relationship between operands and bring out a decision and program accordingly. This is when the relational operator come into picture. C supports the following relational operators:

OperatorMeaning
is less than
<= is less than or equal to
is greater than
>=is greater than or equal to
==is equal to
!=  is not equal to

It is required to compare the marks of 2 students, salary of 2 persons, we can compare them using relational operators. A simple relational expression contains only one relational operator and takes the following form.

exp1 relational operator exp2 : Where exp1 and exp2 are expressions, which may be simple constants, variables or combination of them. Given below is a list of examples of relational expressions and evaluated values.


6.5 <= 25 TRUE 
-65 > 0 FALSE 
10 < 7 + 5 TRUE 

Relational expressions are used in decision making statements of C language such as if, while and for statements to decide the course of action of a running program.

Logical Operators

C has the following logical operators, they compare or evaluate logical and relational expressions.

OperatorMeaning
&&Logical AND
||Logical OR
!Logical NOT

Logical AND (&&)

This operator is used to evaluate 2 conditions or expressions with relational operators simultaneously. If both the expressions to the left and to the right of the logical operator is true then the whole compound expression is true.

Example

a>b && x==10

The expression to the left is a > b and that on the right is x == 10 the whole expression is true only if both expressions are true i.e., if a is greater than b and x is equal to 10.

Logical OR (||)

The logical OR is used to combine 2 expressions or the condition evaluates to true if any one of the 2 expressions is true.

Example

a<m || a<n 

The expression evaluates to true if any one of them is true or if both of them are true. It evaluates to true if a is less than either m or n and when a is less than both m and n.

Logical NOT (!)

The logical not operator takes single expression and evaluates to true if the expression is false and evaluates to false if the expression is true. In other words it just reverses the value of the expression.

Example

! (x >= y) 

C allows usage of three logical operators, namely, &&, || and !.These are to be read as 'AND' 'OR' and 'NOT' respectively. here we can take an example to under stand the logical operators which is given below:

main( )
{
int m1, m2, m3, m4, m5, per ;
printf ( "Enter marks in five subjects " ) ;
scanf ( "%d %d %d %d %d", &m1, &m2, &m3, &m4, &m5 ) ;
per = ( m1 + m2 + m3 + m4 + m5 ) / 5 ;
if ( per >= 60 )
printf ( "First division" ) ;
if ( ( per >= 50 ) && ( per < 60 ) )
printf ( "Second division" ) ;
if ( ( per >= 40 ) && ( per < 50 ) )
printf ( "Third division" ) ;
if ( per < 40 )
printf ( "Fail" ) ;
}
/* As can be seen from the second if statement, the &amp;&amp; operator is 
used to combine two conditions. 'Second division' gets printed if both the
conditions evaluate to true. If one of the conditions evaluate to false then
the whole thing is treated as false. By the above example we can understand 
the else if condition which is only the arrangement of else with if. */

Example

/* In this program sex and ms are the variable of char type and age 
is the variable of integer type and operator is denoted as &&
 and or operator is denoted as ||  */

#include <stdio.h>
#include <conio.h>
main()
{
	char sex, ms;
	int age;
	clrscr();
	printf ("Enter Age, Sex, Merital Status");
	scanf ("%d %c %c" &age,&sex,&ms);
	if ((ms=='M')||(ms=='U'&&sex=='M'&&age>30)||(ms=='M')||
    (ms=='M'&&sex=='F'&&age>25))
		printf ("Driver is insured");
	else
		printf ("Driver is not insured");
}
Output : Enter Age, Sex, Marital Status

35 F M

Driver is insured

Previous Home Next