You are on page 1of 41

COMMENTS AND

OPERATORS
Comments
• A comment is an explanation or description about a piece/segment of
a code.
• A comment can helps the other programmers to understand the
program flow.
• In C Programming Language, there are two types of commenting
options, and they are Single Line and Multi-line.
• During program execution, comments or commented parts of codes
are skipped (they are not executed)
Single Line Comment
• We represent a single line comment with two forward slashes //.
• In this program, we are displaying Hello World as the C Programming
output. Here, we used a single line comment.
/* Single Line Comments in C */
#include<stdio.h>
int main()
{
// Printing Hello World Message as Output
printf(" Hello World! \n");
return 0;
}
Multi-Line Comments
• To use a multiline comment in C we use /*………….*/.The doted space represent specific
descriptions about a code segment.
• Below example describes about multiline comments.

/* Multi Line Comments in C */


#include<stdio.h>
int main()
{
/*
Print
Message
as an Output
*/
printf(" Welcome to C Programming\n");
return 0;
}
• It should be clear that, multiline comment is also used to comment a
single line.
• It is also used in programming to disable apart of a code if required
for any reason.
• This technique is most used when you want to trace a problem, by
commenting some functions or startments.
OPERATORS
C Programming Operators
• C Programming Operators are the symbols used to perform
mathematical and logical operations.
• You can use the C Operators on individual values or variables.
Arithmetic Operators
• The Arithmetic operators are used to perform basic mathematical
calculations like Addition, Subtraction, Multiplication, Division, and
Modulus.
/* Program to Perform Arithmetic Operations in C */
#include<stdio.h>
int main()
{
int a = 12, b = 3;
int addition, subtraction, multiplication, division, modulus;
addition = a + b; //addition of 3 and 12
subtraction = a - b; //subtract 3 from 12
multiplication = a * b; //Multiplying both
division = a / b; //dividing 12 by 3 (number of times)
modulus = a % b; //calculation the remainder
printf("Addition of two numbers a, b is : %d\n", addition);
printf("Subtraction of two numbers a, b is : %d\n", subtraction);
printf("Multiplication of two numbers a, b is : %d\n", multiplication);
printf("Division of two numbers a, b is : %d\n", division);
printf("Modulus of two numbers a, b is : %d\n", modulus);
}
NOTE:
•When we are using division (/) operator the result will completely
depend upon the data type it belongs to.
•For example, if the data type is an integer then division arithmetic
operators in c will produce the integer value by rounding the value (5 /
2 = 2).
•If you want the correct result then change the data type to float.
•Don’t get confused, let’s see one more example for better
understanding
Arithmetic Operators using Float
• /* C Program to Perform Division and Modulus on Float data type */
#include<stdio.h>
int main()
{
int a = 7, b = 3;
int integerdiv, modulus;
float floatdiv;
integerdiv = a / b; // dividing 7 by 3
modulus = a % b; // calculation the remainder
floatdiv = (float)a / b; // Converting int to float
printf("Division of two numbers a, b is : %d\n", integerdiv);
printf("Modulus of two numbers a, b is : %d\n", modulus);
printf("---------Correct Results is------- \n");
printf("Division of two numbers a, b is : %f\n", floatdiv);
}
• From the above arithmetic operators, you can notice that we got two
different results for the same calculation.
• Because for the first result, both a and b are integers and the output
is also an integer (integerdiv).
• So the compiler neglects the term after the decimal point and shows
answer 2 instead of 2.3333 and a % b is 1 because the remainder is 1.
• Next, we changed the output data type to float (floatdiv), and also
converted the result to float to get our desired result.
• Please be careful, while using the division Operator
Assignment Operators

• Assignment operators in Programming are useful to assign the values


to the declared variables.
• Equals (=) operator is the most commonly used assignment operator
in C.
• e.g int a=10;
• This means that we define that whenever a is used as an integer, its
value is 10.
• The given table displays most of the assignment operators present in
C Programming with an example.
In the given example below, two integer variables a, Total, were defined
and their values are 7 and 21.
Example
/* Program for Assignment Operators in C*/
#include <stdio.h>
int main()
{
int a = 7;
int Total = 21;
printf(" Value of the Total = %d \n", Total += a );
printf(" Value of the Total = %d \n", Total -= a );
printf(" Value of the Total = %d \n", Total *= a );
printf(" Value of the Total = %d \n", Total /= a );
printf(" Value of the Total = %d \n", Total %= a );
return 0;
}
Explanation
printf(" Value of the Total = %d \n ", Total += a );
Total += a means
Total = Total + a = 21 + 7 = 28
printf(" Value of the Total = %d \n", Total -= a );
Total -= a means
Total = Total – a = 28 – 7 = 21
printf(" Value of the Total = %d \n", Total *= a );
Total *= a means
Total = Total * a = 21 * 7 = 147
printf(" Value of the Total = %d \n", Total /= a );
Total /= a means
Total = Total / a = 147 / 7 = 21
printf(" Value of the Total = %d \n", Total %= a );
Total %= a means
Total = Total + a = 21 % 7 = 0 (Remainder of 21/7 is = 0)
Relational Operators in C
• The Relational operators are some of the Operators, which are mostly
used either in conditional statements especially in If Conditions or
Loops.
• Relational operators in C are commonly used to check the
relationship between the two variables.
• If the relation is true, then it will return value 1. Otherwise, it returns
value 0.
Example
• /* C Relational Operations on integers */
#include <stdio.h>
int main()
{
int a = 9;
int b = 4;
printf(" a > b: %d \n", a > b);
printf("a >= b: %d \n", a >= b);
printf("a <= b: %d \n", a <= b);
printf("a < b: %d \n", a < b);
printf("a == b: %d \n", a == b);
printf("a != b: %d \n", a != b);
}
• Since we didn’t involve any conditional statement, the answer will be
1 if the condition apply and 0 if the condition doesn’t apply.
Increment and Decrement Operators in C
• The Increment and Decrement Operators in C are operators, which
are used to increase or decrease the given value/variable by 1.
• For instance, Incremental operator ++ is used to increase the existing
variable/value by 1 (x = x + 1).
• And decrement operator – – is used to decrease or subtract the
existing value by 1 (x = x – 1).
• The syntax of the increment and decrement operators in C
Programming is:
Increment Operator : ++x or x++
Decrement Operator: --x or x--
Example
• /* Increment and Decrement Operators in C Example */
#include <stdio.h>
int main()
{
int x = 10,y = 20;
printf("----INCREMENT OPERATOR EXAMPLE---- \n");
printf("Value of x : %d \n", x); //Original Value
printf("Value of x : %d \n", x++); // Using increment Operator
printf("Value of x : %d \n", x); //Incremented value
printf("----DECREMENT OPERATOR EXAMPLE---- \n");
printf("Value of y : %d \n", y); //Original Value
printf("Value of y : %d \n", y--); // using decrement Operator
printf("Value of y : %d \n", y); //decremented value
return 0;
}
Prefix and Postfix in C
• If you observe the above syntax, we can assign the C increment and
decrement operators either before operand or after the operand.
• When ++ or — is used before operand like: ++x, --x then we call it as
prefix, if ++ or -- is used after the operand like: x++ or x-- then we
called it as postfix.
Prefix and Postfix of increment and
decrement operators in C
1.++i (Pre-increment): It will increment the value of i even before
assigning it to the variable i.
2.i++ (Post-increment): The operator will return the variable value first
(i.e, i value) then only i value is incremented by 1.
3.--i (Pre decrement): It will decrement the value of i even before
assigning it to the variable i.
4.i-- (Post decrement): The operator returns the variable value first
(i.e., i value), then only i value decremented by 1.
/* Increment and Decrement Operators in C as Prefix and Postfix */
#include<stdio.h>
int main()
{
int x = 10,y = 20, a = 5, b= 4;
printf("---- PRE INCREMENT OPERATOR EXAMPLE---- \n");
printf("Value of x : %d \n", x); //Original Value
printf("Value of x : %d \n", ++x); // using Pre increment Operator
printf("Value of x Incremented : %d \n", x); //Incremented value
printf("----POST INCREMENT OPERATOR EXAMPLE---- \n");
printf("Value of y : %d \n", y); //Original Value
printf("Value of y : %d \n", y++); // using Post increment Operator
printf("Value of Incremented y : %d \n", y); //Incremented value
printf("----PRE DECREMENT OPERATOR EXAMPLE---- \n");
printf("Value of a : %d \n", a); //Original Value
printf("Value of a : %d \n", --a); // using Pre decrement Operator
printf("Value of decremented y : %d \n", a); //decremented value
printf("----POST DECREMENT OPERATOR EXAMPLE---- \n");
printf("Value of b : %d \n", b); //Original Value
printf("Value of b : %d \n", b--); // using Post decrement Operator
printf("Value of decremented b : %d \n", b); //decremented value
return 0; }
Logical Operators in C

• The Logical operators in C are some of the Operators, which are used to
combine two or more conditions.
• And perform the logical operations using && (Logical AND), ||
(Logical OR) and ! (Logical NOT)
• The Relational Operators in C are used to compare two variables, what if
we want to compare more than one condition? Very simple, C logical
operators will do the trick for you.
Logical Operators

OPERATORS NAME DESCRIPTION EXAMPLE

&& It returns true when


logical AND If (age > 18 && age <=35)
both conditions are true

It returns true when at-


II logical OR least one of the If (age > 35 || age < 60)
condition is true

! If the condition is true,


If age = 18! then( age = 18)
logical NOT logical NOT operator
returns false
makes it false
LOGICAL AND (&&) Operator Truth
Table

CONDITION 1 CONDITION 2 CONDITION 1 && CONDITION 2

True True True

True False False

False True False

False False False


LOGICAL OR (||) Operator

CONDITION 1 CONDITION 2 CONDITION 1 || CONDITION 2

True True True

True False True

False True True

False False False


Conditional Operator in C
• The Conditional Operator in C, also called a Ternary operator (?), is
one of the Operators, which is used in the decision-making process.
• The C Programming Conditional Operator returns the statement
depending on the given expression result.
• The basic syntax of a Ternary Operator in C Programming is as shown
below:
Test_expression ? statement1: statement2
• From the above syntax, If the given test condition is true, then it
returns statement1, and if it is false, statement2 will returned.
Example
/* Ternary Operator in C Example */
#include<stdio.h>
int main()
{
int age;
printf(" Please Enter your age here: \n ");
scanf(" %d ", &age);
(age >= 18) ? printf(" You are eligible to Vote ") : printf(" You are not eligible
to Vote ");
return 0;
}
Example
/* Ternary Operator in C Example */
#include<stdio.h>
int main()
{
int maths, ict, geog, science, bios,english,kisw,sum, average;
printf(" Please Enter Marks Scored in Mathematics: \n ");
scanf(" %d ", &maths);
printf(" Please Enter Marks Scored in ICT: \n ");
scanf(" %d ", &ict);
printf(" Please Enter Marks Scored in Science: \n ");
scanf(" %d ", &science);
printf(" Please Enter Marks Scored in Bilology: \n ");
scanf(" %d ", &bios);
printf(" Please Enter Marks Scored in English: \n ");
scanf(" %d ", &english);
printf(" Please Enter Marks Scored in Kiswahili: \n ");
scanf(" %d ", &kisw);
Sum=math+geog+science+bios+english+kisw;
Average=sum/7;
(average >= 65) ? printf(“ You have Passed, Congratulations") : printf(“\n Sorry You are Below Average, You have a Chance to sit Supplimentary Exams ");
return 0;
}
Bitwise Operators in C
• The Bitwise operators in C are some of the Operators, used to perform
bit operations.
• All the decimal values will convert into binary values (sequence of
bits i.e., 0100, 1100, 1000, 1001 etc.).
• Next, the bitwise operators in C will work on these bits, such as
shifting them left to right or converting bit value from 0 to 1.
• The below table shows the different Bitwise operators in C
Programming with example and their meaning.
• For example, Consider x = 6 and y = 8 and their values in binary form
are: x = 0110 and y = 1000.
BITWISE OPERATORS IN C OPERATOR MEANING EXAMPLES

& Bitwise AND X & Y = 0000


| Bitwise OR X | Y = 1110
^ Bitwise exclusive OR X ^ Y = 1110

~X = 00001001 (Bitwise Not operator will


~ Bitwise complement
convert all 0 into 1.)

X << 1 = 00001100 (Bits will move 1 step


<< Shift left left. If we use 2 or 3 then they shift
accordingly)

>> Shift right Y >> 1 = 00000100


Bitwise Operators Truth Table

X Y X&Y X|Y X^Y

0 0 0 0 0

0 1 0 1 1

1 0 0 1 1

1 1 1 1 0
Bitwise operators in C Example
• Let us see one example for a better understanding of bitwise operators
in C Programming.
• In this Program, We are using two variables a and b, and their values
are 9 and 65.
• Next, we are going to use these two variables to show you various
Bitwise operations in C Language
Example
/* Bitwise operators in C Example */
#include <stdio.h>
int main()
{
int a = 9, b = 65;
printf(" Bitwise AND Operator a&b = %d \n", a & b);
printf(" Bitwise OR Operator a|b = %d \n", a | b);
printf(" Bitwise EXCLUSIVE OR Operator a^b = %d \n", a ^ b);
printf(" Bitwise NOT Operator ~a = %d \n", a = ~a);
printf(" LEFT SHIFT Operator a<<1 = %d \n", a << 1);
printf(" RIGHT SHIFT Operator b>>1 = %d \n", b >> 1);
return 0;
}
• In this bitwise operators program, We declared 2 integers a and b and
assigned the values 9 and 65.
• The binary form of 9 = 0001001 and 65 = 1000001.
• The below printf statements will perform the bitwise operations on a
and b then they will display the output
• printf(" Bitwise AND Operator a&b = %d \n", a & b);
• printf(" Bitwise OR Operator a|b = %d \n", a | b);
• printf(" Bitwise AND Operator a^b = %d \n", a ^ b);
• printf(" Bitwise NOT Operator ~a = %d \n", a = ~a);
• printf(" LEFT SHIFT Operator a<<1 = %d \n", a << 1);
• printf(" RIGHT SHIFT Operator b>>1 = %d \n", b >> 1);
• C Bitwise AND Operation = a & b
• 0001001 & 1000001 = 0000001 = 1
• C Bitwise OR Operation = a || b
• 0001001 || 1000001 = 1001001 = 73
• Next, C Bitwise Exclusive OR Operation = a ^ b
• 0001001 ^ 1000001 = 1001000 = 72
• Left Shift Operation of a Bitwise Operator = b >> 1
• 1000001 >> 1 = 0100000 = 32

You might also like