UrbanPro
true

Learn C Language from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

Operators in C

Venkateswararao S.
18/01/2017 0 0

Operators in C

Operator:    An operator is a symbol that tells the compiler to perform certain mathematical or logical calculations. Operators are used in programs to manipulate data and variables. The data items that operators act upon are called operands.

Arithmetic Operators:     There are five arithmetic operators in C. there are

                             Operator                         Purpose

                +                              addition

  • subtraction

*                               multiplication

/                              division

%                              remainder after integer division

          The operands acted upon by arithmetic operators must represent numeric values. Thus, the operands can be integer quantities, floating – point quantities or characters. The remainder operator (%) requires that both operands be integer and the second operand be nonzero. Similarly, the division operator(/) requires that the second operand be nonzero.

Ex:-   a, b are integer variables whose values are 10,3.

                                      Expression                               Value

                                          a + b                                       13

                                          a – b                                         7

                                          a * b                                        30

                                          a / b                                        3

                                          a % b                                       1

          The value of an expression can be converted to a different data type if desired. To do so, the expression must be preceded by the name of the desired data type, enclosed in parentheses, i.e.,

                   (data type) expression

          This type of construction is known as a cast.

Unary Operators:

          C includes a class of operators that act upon a single operand to produce a new value. Such operators are known as unary operators. The most common unary operator is unary minus, where a numerical constant, variable or expression is preceded by a minus sign.

Ex:-   -743            -0.2             -0X7FFF               -(x + y)

          There are two other commonly used unary operators: The increment operator, ++, and the decrement operator, - -. The increment operator causes its operand to be increased by 1, whereas the decrement operator causes its operand to be decreased by 1. The operand used with each of these operators must be a single variable.

 

Ex:-   The expression ++ i which is equivalent to writing i= i+ 1.

          A prefix operator first add one or subtract one to the operator and then the result assigned to the variable. A postfix operator first assigns the value to the variable and then increment or decrement to the operand.

Ex:-   a=10;

          b=++a;

          The result of the above statement is, the variable ‘a’ is increment by one and result will be assigned to ‘b’. Then ‘b’ will have the value of 11.

          a=10;

          b=a++;

          First the value of ‘a’ will be assigned to ‘b’, after that ‘a’ will be incremented by one. Then ‘b’ will have the value of 10.

          a=10;                                       a=10;

          b=--a;                                       b=a--; c=b++;

          Result  b = 9.                           Result  c = 10.

Relational and logical operators:

          There are four relational operators in C. They are

                             Operator                         Meaning

                                  <                               less than

                                  <=                             less than or equal to

>                               greater than

>=                             greater than or equal to

                          These operators all fall within the same precedence group, which is lower than the arithmetic and unary operators. The associativity of these operators is left to right.

Equality operators:

                             Operator                         Meaning

                                  ==                             equal to

                                  !=                              not equal to

          The equality operators fall into a separate precedence group, beneath the relational operators. These operators also have a left – to – right associativity.

          These six operators are used to form logical expressions, which represent conditions that are either true or false. The resulting expressions will be of type integer, since true is represented by the integer value 1 and false is represented by the value 0.

          In addition to the relational and equality  operators, C contains two logical operators (also called logical connectives). They are

                             Operator                         Meaning

                                  &&                               and

                                  ||                                or

          These operators are referred to as logical and  and logical or, respectively. The logical operators act upon operands that are themselves logical expressions. The result of a logical and operation will be true only if both operands are true, whereas the result of a logical or operation will be true if either operand is true or if both operands are true. In other words, the result of a logical or operation will be false only if both operands are false.

          C also includes the unary operator ! that negates the value of a logical expression; i.e., it causes an expression that is originally true to become false, and vice versa. This operator is referred to as the logical negation (or logical not) operator.

Operator precedence are summarized below, from highest to lowest.

Operator category                              Operators                                 Associativity

Unary operator                        -  ++  --  !  sizeof   (type)                            RàL

Arithmetic multiply, divide

          and remainder                         *   /   %                                           LàR

Arithmetic and subtract                      +   -                                               LàR

Relational operat

0 Dislike
Follow 0

Please Enter a comment

Submit

Other Lessons for You

Be prepared to get trained--init
Before starting the training,students must be mentally prepared for acceptance of new knowledge. Students must attend training with open minded forgetting the position they are working.This will help...
S

Smartnub Softsolutions

0 0
0

Static and dynamic libraries
A library is a package of code that is meant to be reused by many programs. A static library (also known as an archive) consists of routines that are compiled and linked directly into your program. When...

C Program-Vowels and Consonants
/*WAP to print the character entered by user is a vowel or consonant*/ //Header files #include<stdio.h>#include<conio.h> //Main functionvoid main(){ char c; //Function for clearing screen...
S

Program to swap the value of two variables without using third variable(simplest way)...
int main() { int a=10,b=20; printf("values of a before swap ="%d,a); printf("values of b before swap ="%d,b); a=a+b; b=a-b; a=a-b; printf("******************"); printf("values of a after swap...

C Program-Error Handling[Program Exit Status]
//Header files #include<stdio.h>#include<conio.h>#include<stdlib.h> //Main Function void main(){ int dividend=20; int divisor=5; int quotient; //Function for clearing screen clrscr();...
S
X

Looking for C Language Classes?

The best tutors for C Language Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn C Language with the Best Tutors

The best Tutors for C Language Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more