Skip to main content

C Operators

Declaration & Assignments

C Operators

C operators are symbols that is used to perform mathematical or logical manipulations. C programming language is rich with built-in operators. Operators take part in a program for manipulating data and variables and form a part of the mathematical or logical expressions

Types of Operators in C

C offers various types of operators having different functioning capabilities.
  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Assignment Operators
  • Increment and Decrement Operators
  • Conditional Operator
  • Bitwise Operators
  • Special Operators

Arithmetic Operators

Arithmetic Operators are used to perform mathematical calculations like addition (+), subtraction (-), multiplication (*), division (/) and modulus (%).
OperatorDescription
+Addition
Subtraction
*Multiplication
/Division
%Modulus

C Program to Add Two Numbers

Example:
#include <stdio.h>
void main()
{
int i=3,j=7,k; /* Variables Defining and Assign values */
k
=i+j;
printf
("sum of two numbers is %d\n", k);
}
Run 
Program Output:
c-add-two-numbers

Increment and Decrement Operators

Increment and Decrement Operators are useful operators generally used to minimize the calculation, i.e. ++x & x++ means x=x+1 or -x & x−−means x=x-1. But there is a slight difference between ++ or −− written before or after the operand. Applying the pre-increment first add one to the operand and then the result is assigned to the variable on left where as post-increment first assigns the value to the variable on left and then increment the operand.
OperatorDescription
++Increment
−−Decrement

Relational Operators

Relational operators are used to compare two quantities or values.
OperatorDescription
==Is equal to
!=Is not equal to
>Greater than
<Less than
>=Greater than or equal to
<=Less than or equal to

Logical Operators

C provides three logical operators when we test more than one condition to make decisions. These are: && (meaning logical AND), || (meaning logical OR) and ! (meaning logical NOT).
OperatorDescription
&&And operator. Performs a logical conjunction on two expressions.
(if both expressions evaluate to True, result is True. If either expression evaluates to False, result is False)
||Or operator. Performs a logical disjunction on two expressions.
(if either or both expressions evaluate to True, result is True)
!Not operator. Performs logical negation on an expression.

Bitwise Operators

C provides special operator for bit operation between two variables.
OperatorDescription
<<Binary Left Shift Operator
>>Binary Right Shift Operator
~Binary Ones Complement Operator
&Binary AND Operator
^Binary XOR Operator
|Binary OR Operator

Assignment Operators

Assignment operators applied to assign the result of an expression to a variable. C has a collection of shorthand assignment operators.
OperatorDescription
=Assign
+=Increments, then assigns
-=Decrements, then assigns
*=Multiplies, then assigns
/=Divides, then assigns
%=Modulus, then assigns
<<=Left shift and assigns
>>=Right shift and assigns
&=Bitwise AND assigns
^=Bitwise exclusive OR and assigns
|=Bitwise inclusive OR and assigns

Conditional Operator

C offers a ternary operator which is the conditional operator (?: in combination) to construct conditional expressions.
OperatorDescription
? : Conditional Expression

Special Operators

C supports some special operators
OperatorDescription
sizeof() Returns the size of an memory location.
& Returns the address of an memory location.
* Pointer to a variable.

Program to demonstrate the use of sizeof operator

Example:
#include <stdio.h>
void main()
{
int i=10; /* Variables Defining and Assign values */
printf
("integer: %d\n", sizeof(i));
}
Run 
Program Output:
c-sizeof-operator

Comments

For Programs Click Here

Popular posts from this blog

Syllabus

Syllabus  C Programming Tutorials C Tutorial C Introduction History of C Programming Language C Installation C Program Structure C Input and Output (I/O) C Format Specifiers Declaration & Assignments C Tokens C Identifiers C Keywords C Constants C Operators C Data Types C Variables C Preprocessors C Type Casting C Custom Header File Flow Control C Decision Making C if Statements C if-else Statements C Nested if-else Statements C else-if Statements C goto Statement C switch Statements C Loops C while loops C do while loops C for loops Functions C Functions C Function Arguments C Library Functions C Variable Scope Arrays & Strings C Arrays C Strings Pointers C Pointers C Dynamic Memory Allocation Structure & Union C Structures C Unions File I/O C File Handling C fopen C fclose C getc C putc C getw C putw C fprintf C fscanf C fgets C fputs C feof                                     ...

Syllabus

Python Tutorials Python Tutorial Python Overview Python Installation Basics of Python Programming Python Operators Python Keywords Python Numbers Python Strings Python Data Types Python Variables Python Lists Python Tuples Python Date and Time Python Decision Making Python Loops Python File Handling Python Dictionaries Python Functions Python Modules Python Exceptions Handling Python Object Oriented Inheritance in Python Python Regular Expressions Python Networking Programming Python Multithreaded Programming Python CGI Programming Python Database Connection Python Metaprogramming Python Data Processing And Encoding Python GUI Programming

C++ Data Abstraction

C++ Data Abstraction Object oriented programming offers various features to write programs with various concepts that help to minimize problems and increase flexibility in program. One of the features of object oriented programming is Data abstraction. In this chapter you will learn about how the concept data abstraction is carried out within the C++ program. What is Data abstraction? Data abstraction allows a program to ignore the details of how a data type is represented. Abstraction (derived from a Latin word  abs , meaning away from and  trahere , meaning to draw) refers to the act of representing essential features without including the background details or explanations. C++ classes use the technique of abstraction and are defined as a list of abstract attributes such as width, cost, size etc and functions to operate on these attributes. They put in a nutshell all the essential properties of an object that are required to be created. The attributes are therefore called...