Operators in C

Operators in C


One of the most important features of C is that it has a very rich set of built in operators including arithmetic, relational, logical, and bitwise operators.

An operator can be any symbol like + - * / that specifies what operation need to be performed on the data.

For ex: + indicates addition operation
* indicates multiplication operation

An operand can be a constant or a variable.

An expression is combination of operands and operators that reduces to a single value.

For ex: Consider the following expression a + b here a and b are operands, while + is an operator.

Types of C operators

C language offers many types of operators.

  • Arithmetic operators.
  • Assignment operators.
  • Relational operators.
  • Logical operators.
  • Bit wise operators.
  • Conditional operators (ternary operators).
  • Increment/decrement operators.
  • Special operators.


Arithmetic Operators


An arithmetic operator performs mathematical operations such as addition, subtraction and multiplication on numerical values (constants and variables).


Operator
Description
+Addition
-Subtraction
*Multiplication
/Division
%Modulus (Remainder )



Assignment Operators in C


An assignment operator is used for assigning a value to a variable. The most common assignment operator is =

It assign the value from right to left side variable.


Operator
Description
Explanation
=Simple Equal
C = A + B will assign the value of A + B to C
+=Add & assignment
C += A is equivalent to C = C + A
-=Subtract & assignment
C -= A is equivalent to C = C - A
*=Multiply & assignment
C *= A is equivalent to C = C * A
/=Divide & assignment
C /= A is equivalent to C = C / A
%=Modulus & assignment
C %= A is equivalent to C = C % A

Relational Operators in C


A relational operator checks the relationship between two operands. If the relation is true, it returns 1; if the relation is false, it returns value 0.

Relational operators are used in decision making and loops.


Operator
Description
>greater than 
<less than 
>=greater than or equal to 
<=less than or equal to 
==equal to 
!=not equal to 

Logical Operators in C


Logical operators are used to combine two or more condition for building complex expression.


Operator
Operator NameDescription
&&ANDLogical AND. True only if all operands are true
||ORLogical OR. True only if either one operand is true
!NOTLogical NOT. True only if the operand is 0


Increment and Decrement Operators in C


Increment operators are used to increase the value of the variable by one and decrement operators are used to decrease the value of the variable by one in C programs.

Type of Increment Operator

  • Pre increment/decrement Operator (++a, --a)
  • Post increment/decrement Operator (a++, a--)

In pre increment / decrement first increment/decrement the value of variable and then used inside the expression (initialize into another variable)

In post-increment / decrement first value of variable is used in the expression (initialize into another variable) and then increment / decrement the value of variable.

Conditional Operator in C


The conditional operator (? :) is a ternary operator. This operator needs 3 operands to perform the operation.

Syntax:

Variable = <test expression> ? <expression 1> : <expression 2>

Or

<test expression> ? <expression 1> : <expression 2>



The conditional operator works as follows:

 Test Expression is Condition

 Expression 1 is Statement Followed if Condition is True

 Expression 2 is Statement Followed if Condition is False


Example 1:

int num1=10,num2=20,max;
max=(num1>num2)?num1:num2; printf("Max is %d",max);

The output is:

max is 20

Example 2:

int num=45;
(num%2==0)?printf("Number is Even"):printf("Number is Odd");

The Output is:

Number is Odd


Question :What are the alphabets or character set of C programming language? 

Answer :

The characters that can be used to form words, numbers and expressions. The characters in C are grouped into the following categories

Letters: Lowercase letters a,b,...,z and uppercase letters A,B,...,Z

Digits: from 0 to 9

White spaces: Space, tab(\t), newline(\n), carriage return(\r), etc

Symbols:


Question : What are C-Tokens? List and define different C-Tokens with examples.

Answer :

A C-Token is a smallest element of a C program. One or more characters are grouped in sequence to form meaningful words. These meaningful words are called C-Tokens. The tokens are broadly classified as follows 

Keywords: Keywords are tokens which are used for their intended purpose only. Each keyword has fixed meaning or predefined meaning and that cannot be changed by user. Hence, they are also called as reserved-words. 

ex: if, for, while, int, float, break, char ..etc. 

Identifiers: As the name indicates, identifier is used to identify various elements of program such as variables, constants, functions, arrays etc. 

ex: sum, length, etc. 

Constants: A constant is an identifier whose value remains fixed throughout the execution of the program. The constants cannot be modified in the program. 

ex: 10, 10.5, 'a', "sri", etc. 

Operators: An operator can be any symbol like + - * / that specifies what operation need to be performed on the data. 

For ex: + indicates addition operation * indicates multiplication operation / indicates division operation, etc. 

Special symbols: The symbols that are used to indicate array subscript, function call, block, etc. 

 ex: [], (), {}, etc. 


Question : What are identifiers? What are the rules for naming a identifier? Give examples. 

Answer :

Identifier is used to name various elements of program such as variables, constants, functions, arrays, functions etc. An identifier is a word consisting of sequence of Letters, Digits or _(underscore).

Rules to Name identifiers are

1. The variables must always begin with a letter or underscore as the first character.

2. The following letters can be any number of letters, digits or underscore.

3. Maximum length of any identifier is 31 characters for external names or 63 characters

for local names.

4. Identifiers are case sensitive. Ex. Rate and RaTe are two different identifiers.

5. The variable name should not be a keyword. 

6. No special symbols are allowed except underscore. 

Examples: 

Valid identifiers: 

Sum
roll_no
_name
sum_of_digits
 avg_of_3_nums

Invalid Identifiers and reason for invalid:

$roll_no - Name doesn‟t begin with either letter or underscore

for - keyword is used as name

sum,1 - special symbol comma is not allowed

3_factorial - name begins with digit as first letter 


Question : What are constants? List and explain different types of constants with examples

Answer:

A constant is an identifier whose value remains fixed throughout the execution of the program. The constants cannot be modified in the program. 

For example: 1, 3.14512, „z‟, “hello"

Different types of constants are: 

1) Integer Constant: An integer is a whole number without any fraction part.

There are 3 types of integer constants:

i) Decimal constants (0 1 2 3 4 5 6 7 8 9) For ex: 0, -9, 22

ii) Octal constants (0 1 2 3 4 5 6 7) For ex: 021, 077, 033

iii) Hexadecimal constants (0 1 2 3 4 5 6 7 8 9 A B C D E F)

 For ex: 0x7f, 0x2a, 0x521 

2) Floating Point Constant: The floating point constant is a real number.

The floating point constants can be represented using 2 forms:

i) Fractional Form: A floating point number represented using fractional form has an integer part followed by a dot and a fractional part.

For ex: 0.5, -0.99

ii) Scientific Notation (Exponent Form): The floating point number represented using scientific notation has three parts namely: mantissa, E and exponent.

For ex: 9.86E3 imply 9.86 x 103

3) Character Constant: A symbol enclosed within a pair of single quotes(„) is called a character constant. Each character is associated with a unique value called an ASCII (American Standard Code for Information Interchange) code.
For ex: '9', 'a', '\n'
ASCII code of A = 65, a= 97, 0=48, etc 

4) String Constant: A sequence of characters enclosed within a pair of double quotes(“) is called a string constant. The string always ends with NULL (denoted by \0) character.
 For ex: "9", "a", "sri", "\n", “”

5) Escape Sequence Characters: An escape sequence character begins with a backslash and is followed by one character. 

A backslash (\) along with some character give special meaning and purpose for that character.

The complete set of escape sequences are: 
\b Backspace
\f Form feed
\n Newline
\r Return
\t Horizontal tab
\v Vertical tab
\\ Backslash
\' Single quotation mark
\" Double quotation mark
\? Question mark
\0 Null character

Question : What is datatype? List and explain different datatypes with examples. 

Datatype is an entity that defines set of values and set of operations that can be applied on those values.

Datatypes are broadly categorized as TWO types:

1) Primitive Data Types: Fundamental data types or Basic data types are primitive data types. 
Examples: int, char, float, double, void 

2) Non-Primitive Data Types: The derived or user-defined data types are called as nonprimitive data types. 
Examples: struct, union, pointers, etc

Programs

Write a program to input number of days from user and display it, week and days format.


For Example

input is : 20 days

then output is:

2 week and 6 days.



View Solution


Write a program to input number of days from user and display it, Month, Week and Days format. (consider 30days = 1 month)


For Example:

input is : 50 days


output is: 1 month, 2 week and 6 days.



View Solution


Write a C program which accept a character from user and ASCII Value of that character


View Solution


Write a program which accept one integer number from user and print it.


View Solution


Write a C Program to input dividend, divisor and Compute Quotient and Remainder


View Solution


Write a C Program to Find the Size of int, float, double and char


View Solution


Write a C Program to Swap Two Numbers


View Solution


Write a C program to Swap Numbers Without Using Temporary(3rd) Variables


View Solution


Write a C Program to input any  number and Check  number is Odd or Even Using the Ternary Operator


View Solution


CCC Online Test 2021 CCC Practice Test Hindi Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level NIELIT Study material and Quiz Bank SSC Railway TET UPTET Question Bank career counselling in allahabad Sarkari Naukari Notification Best Website and Software Company in Allahabad Website development Company in Allahabad