Week 1 Self Learning Materials
Week 1 Self Learning Materials
Week 1 Self Learning Materials
Learning Objectives:
Key Topics
1.1 Operator
1.3 Expressions
1.6 Operator
An operator is a symbol that operates on a value or a variable. The types of operators are:
Arithmetic Operators
Increment and Decrement Operator
Relational Operator
Logical Operator
Assignment Operator
Bitwise Operator
Other Operator (Comma, sizeof operator)
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.
// Sample Program
#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10;
printf("%d == %d is %d \n", a, b, a == b);
printf("%d == %d is %d \n", a, c, a == c);
printf("%d > %d is %d \n", a, b, a > b);
printf("%d > %d is %d \n", a, c, a > c);
printf("%d < %d is %d \n", a, b, a < b);
printf("%d < %d is %d \n", a, c, a < c);
printf("%d != %d is %d \n", a, b, a != b);
printf("%d != %d is %d \n", a, c, a != c);
printf("%d >= %d is %d \n", a, b, a >= b);
printf("%d >= %d is %d \n", a, c, a >= c);
printf("%d <= %d is %d \n", a, b, a <= b);
printf("%d <= %d is %d \n", a, c, a <= c);
return 0;
}
Output:
5 == 5 is 1
5 == 10 is 0
5 > 5 is 0
5 > 10 is 0
5 < 5 is 0
5 < 10 is 1
5 != 5 is 0
5 != 10 is 1
5 >= 5 is 1
5 >= 10 is 0
5 <= 5 is 1
5 <= 10 is 1
An expression containing logical operator returns either 0 or 1 depending upon whether expression
results true or false. Logical operators are commonly used in decision making in C programming.
#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10, result;
result = (a == b) && (c > b);
printf("(a == b) && (c > b) is %d \n", result);
result = (a == b) && (c < b);
printf("(a == b) && (c < b) is %d \n", result);
result = (a == b) || (c < b);
printf("(a == b) || (c < b) is %d \n", result);
result = (a != b) || (c < b);
printf("(a != b) || (c < b) is %d \n", result);
result = !(a != b);
printf("!(a == b) is %d \n", result);
result = !(a == b);
printf("!(a == b) is %d \n", result);
return 0;
}
Output:
(a == b) && (c > b) is 1
(a == b) && (c < b) is 0
(a == b) || (c < b) is 1
(a != b) || (c < b) is 0
!(a != b) is 1
!(a == b) is 0
Character arithmetic is used to implement arithmetic operations like addition and subtraction on
characters in C language. It is used to manipulate the strings. When the characters are used with the
arithmetic operations, it converts them into integer value automatically i.e. ASCII value of characters.
Example:
#include <stdio.h>
int main(){
char s = 'm';
printf("%c\n", s);
printf("%d\n", (s+1));
printf("%c\n", (s+1));
printf("%d\n", (s-1));
printf("%c\n", (s-1));
printf("%d\n", t);
// printf("%c", t);
return 0;
Output:
109
110
108
1.3 Expression
Priority This represents the evaluation of expression starts from "what" operator.
Associativity It represents which operator should be evaluated first if an expression is containing
more than one operator with same priority.
C programming has two operators increment ++ and decrement -- to change the value of an operand
(constant or variable) by 1. Increment ++ increases the value by 1 whereas decrement -- decreases the
value by 1. These two operators are unary operators, meaning they only operate on a single operand.
// Sample Program
#include <stdio.h>
int main()
{
int a = 10, b = 100;
float c = 10.5, d = 100.5;
printf("++a = %d \n", ++a);
printf("--b = %d \n", --b);
printf("++c = %f \n", ++c);
printf("--d = %f \n", --d);
return 0; }
Output:
++a = 11
--b = 99
++c = 11.500000
++d = 99.500000
Example
The following expression determines which variable has the greater value, y or z, and assigns the
greater value to the variable x:
x = (y > z)? y:z;
if (y > z)
x = y;
else
x = z;
The conditional operator is also known as a ternary operator. The conditional statements are the
decision-making statements which depends upon the output of the expression. It is represented by two
symbols, i.e., '?' and ':'.
As conditional operator works on three operands, so it is also known as the ternary operator.
The behavior of the conditional operator is similar to the 'if-else' statement as 'if-else' statement is also
a decision-making statement.
o In the above syntax, the expression1 is a Boolean condition that can be either true or false
value.
o If the expression1 results into a true value, then the expression2 will execute.
o The expression2 is said to be true only when it returns a non-zero value.
o If the expression1 returns false value then the expression3 will execute.
o The expression3 is said to be false only when it returns zero value.
Program 1
#include <stdio.h>
int main()
(age>=18)? (printf("eligible for voting")) : (printf("not eligible for voting")); // conditional operator
return 0;
Explanation:
In the above code, we are taking input as the 'age' of the user. After taking input, we have applied the
condition by using a conditional operator. In this condition, we are checking the age of the user. If the
age of the user is greater than or equal to 18, then the statement1 will execute, i.e., (printf ("eligible
for voting")) otherwise, statement2 will execute, i.e., (printf("not eligible for voting")).
Program 2:
#include <stdio.h>
int main()
return 0;
}
Explanation:
In the above code, we have declared two variables, i.e., 'a' and 'b', and assign 5 value to the 'a'
variable. After the declaration, we are assigning value to the 'b' variable by using the conditional
operator. If the value of 'a' is equal to 5 then 'b' is assigned with a 3 value otherwise 2.
L-value: “l-value” refers to memory location which identifies an object. L-value may appear as either
left hand or right hand side of an assignment operator (=). L-value often represents as identifier.
Expressions referring to modifiable locations are called “modifiable l-values“. A modifiable l-value
cannot have an array type, an incomplete type, or a type with the const attribute. For structures and
unions to be modifiable L-values, they must not have any members with the const attribute. The name
of the identifier denotes a storage location, while the value of the variable is the value stored at that
location.
An identifier is a modifiable L-value if it refers to a memory location and if its type is arithmetic,
structure, union, or pointer. For example, if ptr is a pointer to a storage region, then *ptr is a
modifiable l-value that designates the storage region to which ptr points.
In C, the concept was renamed as “locator value”, and referred to expressions that locate (designate)
objects. The L-value is one of the following:
The name of the variable of any type i.e, an identifier of integral, floating, pointer, structure,
or union type.
The result of indirection through a pointer, provided that it isn’t a function pointer.
The result of member access through pointer(-> or .)// declare a an object of type 'int'
int a;
a = 1;
9 = a;
R-value: r-value” refers to data value that is stored at some address in memory. A r-value is an
expression that can’t have a value assigned to it which means r-value can appear on right but not
on left hand side of an assignment operator(=)
() Function call
[] Array subscripting
* Indirection (dereference)
& Address-of
sizeof Size-of
12 || Logical OR
= Simple assignment
15 , Comma Left-to-right
#include <stdio.h>
main() {
double mean;
Output:
References
1) https://www.javatpoint.com/c-array
2) https://beginnersbook.com/