KEMBAR78
Constant and Operator | PDF | Mathematics | Computer Architecture
0% found this document useful (0 votes)
25 views22 pages

Constant and Operator

The document discusses constants and operators in C programming, defining constants using const and #define, describing different types of operators like arithmetic, relational, logical, and bitwise operators, and providing examples of how to use these operators in C code.

Uploaded by

rohitggothi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views22 pages

Constant and Operator

The document discusses constants and operators in C programming, defining constants using const and #define, describing different types of operators like arithmetic, relational, logical, and bitwise operators, and providing examples of how to use these operators in C code.

Uploaded by

rohitggothi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

C Programming

Constant and Operators


Constants in C A constant is a value or variable that can't be changed in the program, for example: 10, 20, 'a',
3.4, "c programming" etc.

Constant Example

Integer Constant 10, 20, 450 etc.


Real or Floating-point Constant 10.3, 20.2, 450.6 etc.
Octal Constant 021, 033, 046 etc.
Hexadecimal Constant 0x2a, 0x7b, 0xaa etc.
Character Constant 'a', 'b', 'x' etc.
String Constant "c", "c program", "c in Study" etc.
2 ways to define constant in C
There are two ways to define constant in C programming

1. const keyword

2. #define preprocessor
The const keyword is used to define constant in C programming.
1) C const keyword

#include<stdio.h>
const float PI=3.14; int main()
{
const float PI=3.14;
printf("The value of PI is: %f",PI);
return 0;
}
2) C #define preprocessor The #define preprocessor is also used to define constant.

#define token value


#include <stdio.h>

#define PI 3.14

main()
{

printf("%f",PI);
}
Literals
Literals are the Constant values that are assigned to
the constant variables.

Literals represent fixed values that cannot be


modified.

Literals contain memory but they do not have


references as variables.

Generally, both terms, constants, and literals are used


interchangeably.
C - Operators An operator is a symbol that tells the compiler to perform specific
mathematical or logical functions. C language is rich in built-in
operators and provides the following types of operators −

1. Arithmetic Operators

2. Relational Operators

3. Logical Operators

4. Bitwise Operators

5. Assignment Operators

6. Misc Operators
These operators are responsible for performing arithmetic or mathematical
1 - Arithmetic Operators operations
The following table shows all the arithmetic operators supported by the C
language. Assume variable A holds 10 and variable B holds 20 then

Operator Description Example

+ Adds two operands. A + B = 30

− Subtracts second operand from the A − B = -10


first.

* Multiplies both operands. A * B = 200

/ Divides numerator by de-numerator. B/A=2

% Modulus Operator and remainder of B%A=0


after an integer division.

++ Increment operator increases the A++ = 11


integer value by one.

-- Decrement operator decreases the A-- = 9


integer value by one.
There are two types of arithmetic operators:

•Unary Operators: This type of operator works with a single value


(operand) like ++ and - -.

•Binary Operators: This type of operator works with two operands like
+,-,*,/
Arithmetic Operators in C
Output :
#include <stdio.h>

int main()
{
int a = 12, b = 6, c;
c = a + b;
printf("a+b = %d \n", c);
c = a - b;
printf("a-b = %d \n", c);
c = a *b;
printf("a*b = %d \n", c); c = a / b;
printf("a/b = %d \n", c); c = a % b;
printf("Remainder when a divided by b = %d \n", c);
return 0;
}
2 - Relational Operators When we want to compare the values of two operands, then relational operators
are used. The following table shows all the relational operators supported by C. Assume variable A holds 10 and
variable B holds 20 then −
Operator Description Example
== Checks if the values of two operands are equal (A == B) is not true.
or not. If yes, then the condition becomes true.
!= Checks if the values of two operands are equal (A != B) is true.
or not. If the values are not equal, then the
condition becomes true.
> Checks if the value of left operand is greater (A > B) is not true.
than the value of right operand. If yes, then the
condition becomes true.
< Checks if the value of left operand is less than (A < B) is true.
the value of right operand. If yes, then the
condition becomes true.
>= Checks if the value of left operand is greater (A >= B) is not true.
than or equal to the value of right operand. If
yes, then the condition becomes true.
<= Checks if the value of left operand is less than or (A <= B) is true
equal to the value of right operand. If yes, then
the condition becomes true.
#include <stdio.h> Output:

int main()
{
int a = 7, b = 7, c = 10;
printf("%d == %d = %d \n", a, b, a == b); // true
printf("%d == %d = %d \n", a, c, a == c); // false
printf("%d > %d = %d \n", a, b, a > b); //false
printf("%d > %d = %d \n", a, c, a > c); //false
printf("%d < %d = %d \n", a, b, a < b); //false
printf("%d < %d = %d \n", a, c, a < c); //true
printf("%d != %d = %d \n", a, b, a != b); //false
printf("%d != %d = %d \n", a, c, a != c); //true
printf("%d >= %d = %d \n", a, b, a >= b); //true
printf("%d >= %d = %d \n", a, c, a >= c); //false
printf("%d <= %d = %d \n", a, b, a <= b); //true
printf("%d <= %d = %d \n", a, c, a <= c); //true
return 0; }
3- Logical Operators
Logical Operators are used for True or False results.

Operator Function Example (if a=1 and b=0)

&& Logical AND (a && b) is false

|| Logical OR (a || b) is true

! Logical NOT (!a) is false


Bitwise Operators These operators are used for bit-level operations on the operands. The operators
are converted first to bit-level, and then calculations are performed.

Bitwise operator works on bits and perform bit-by-bit operation. The truth tables
for &, |, and ^ is as follows −

p q p&q p|q p^q


0 0 0 0 0
0 1 0 1 1
Assume A = 60 and B = 13 in binary
1 1 1 1 0
format, they will be as follows −
1 0 0 1 1 A = 0011 1100
B = 0000 1101
-----------------
A&B = 0000 1100
A|B = 0011 1101
A^B = 0011 0001
~A = 1100 0011
Output:
#include <stdio.h>

int main()
{
int a = 10, b = 8;
printf("Output = %d", a&b);
return 0;
}
Explanation:

10 = 00001010 (In Binary)


8 = 00001000 (In Binary)

Bit Operation of 10 and 8

00001010
& 00001000

= 00001000 = 8 (In decimal)


The following table lists the bitwise operators supported by C. Assume variable 'A' holds 60 and variable 'B'
holds 13, then −

Operator Description Example


& Binary AND Operator copies a bit to the result if (A & B) = 12, i.e., 0000 1100
it exists in both operands.
| Binary OR Operator copies a bit if it exists in (A | B) = 61, i.e., 0011 1101
either operand.
^ Binary XOR Operator copies the bit if it is set in (A ^ B) = 49, i.e., 0011 0001
one operand but not both.
~ Binary One's Complement Operator is unary (~A ) = ~(60), i.e,. -0111101
and has the effect of 'flipping' bits.
<< Binary Left Shift Operator. The left operands
value is moved left by the number of bits A << 2 = 240 i.e., 1111 0000
specified by the right operand.
>> Binary Right Shift Operator. The left operands
value is moved right by the number of bits A >> 2 = 15 i.e., 0000 1111
specified by the right operand.
Assignment Operators These types of operators are used to assign a value to a variable

The following table lists the assignment operators supported by


the C language −

Operator Description Example

= Simple assignment operator. Assigns values


C = A + B will assign the
from right side operands to left side operand
value of A + B to C

+= Add AND assignment operator. It adds the right


operand to the left operand and assign the C += A is equivalent to C =
result to the left operand. C+A

-= Subtract AND assignment operator. It subtracts


the right operand from the left operand and
C -= A is equivalent to C =
assigns the result to the left operand.
C-A
Misc Operators ↦ sizeof & ternary Besides the operators discussed above, there are a few other
important operators including sizeof and ? : supported by the C
Language.

Operator Description Example

sizeof() sizeof(a), where a is integer, will return


Returns the size of a variable. 4.

& Returns the address of a &a; returns the actual address of the
variable. variable.

* Pointer to a variable. *a;

?: If Condition is true ? then value X :


Conditional Expression. otherwise value Y
#include <stdio.h>

main()
{
int a = 4;
short b;
double c;

/* example of sizeof operator */

printf("Line 1 - Size of variable a = %d\n", sizeof(a) );


printf("Line 2 - Size of variable b = %d\n", sizeof(b) );
printf("Line 3 - Size of variable c= %d\n", sizeof(c) );
}
#include <stdio.h>

main()
{

int a = 4;
int* ptr;

/* example of & and * operators */


ptr = &a; /* 'ptr' now contains the address of 'a'*/
printf("value of a is %d\n", a);
printf("*ptr is %d.\n", *ptr);
}
#include <stdio.h>

main()
{
int a,b;
/* example of ternary operator */
a = 10;
b = (a == 1) ? 20: 30;

printf( "Value of b is %d\n", b );

b = (a == 10) ? 20: 30;


printf( "Value of b is %d\n", b );
}

You might also like