0% found this document useful (0 votes)
2 views17 pages

Open Course Lectures - 08-06-2021

The document provides an overview of fundamental concepts in C programming, including constants, variables, arrays, and various operators such as arithmetic, relational, logical, and bitwise operators. It explains the use of expressions, symbolic constants, assignment operators, and the conditional operator, along with examples and code snippets. Additionally, it touches on type casting and the importance of data types in variable declaration and operations.

Uploaded by

shantanunathnath
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)
2 views17 pages

Open Course Lectures - 08-06-2021

The document provides an overview of fundamental concepts in C programming, including constants, variables, arrays, and various operators such as arithmetic, relational, logical, and bitwise operators. It explains the use of expressions, symbolic constants, assignment operators, and the conditional operator, along with examples and code snippets. Additionally, it touches on type casting and the importance of data types in variable declaration and operations.

Uploaded by

shantanunathnath
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/ 17

Constants & Variables

• A fixed and well-defined number is a constant.


• A variable is nothing but a name given to a
storage area that our programs can manipulate.
Each variable in C has a specific type, which
determines the size and layout of
the variable's memory; the range of values that
can be stored within that memory; and the set of
operations that can be applied to the variable.
Arrays
• An array is defined as the collection of similar
type of data items stored at contiguous memory
locations.
• Array is like a SET in Mathematics. The only
difference of SET and array is homogeneity factor.

• Arrays are the derived data type


in C programming language which can store the
primitive type of data such as int, char, double,
float, etc.
C Instructions, expressions,
statements, symbolic constants

Expression is
the combination
of operators
and operands.

• Symbolic Constant is a name that substitutes for a sequence of characters


or a numeric constant, a character constant or a string constant.
E.g. #define PI 3.141592
#define MAX 500
Operators in C
Unary Operators
Operators that operates or works with a single
operand are unary operators. E.g. (++ , --)
b=++a Vs. b=a++
• ++a means increment (a=a+1) first then
assignment to b
• a++ means assignment and then increment
Question to you?
• Q: If a=7 and b=8, what is the value of c, if
c= ++a + b++

• what is the value of d, if


d= ++a + --b

c= 16 d= 17
Example Program
#include <stdio.h>
int main()
{
int a=7, b=8, c,d;
c=++a+b++;
d=a+++b--;
printf("a=%d and d=%d", c,d);
return 0;
}
Arithmetic Operator %
• It is known as modulus operator. It returns the
integer remainder of a division.
• E.g. 10÷3, Q=3 R=1
• 10 % 3 = 1 //program for sum-of-the-digits of a number
#include <stdio.h>
int main()
{
int a=78,d,sum;
d=a/10;
Sum of the digits=15 a=78%10;
sum=a+d;
printf("Sum of the digits=%d",sum);
return 0;
}
Relational Operators
These are used for comparison of the values of
two operands. For example, checking if one
operand is equal to the other operand or not, an
operand is greater than the other operand or
not etc.
Six Relational operators are (==,!=,>, >= , <, <= )
e.g. A==B, A!=B, A>B, A>=B, A<B,A<=B
Logical Operators
• Logical Operators are used to combine two or more
conditions. The result of the operation of a logical
operator is a boolean value either true or false.
• Logical Opeartors are
• AND represented as ‘&&’
• OR represented as ‘||’
• NOT represented as ‘!’

• a && b returns true when both a and b are true (i.e.


non-zero).
Truth Table
Bitwise operators
• It is used to perform bit-level operations on the operands.
The operators are first converted to bit-level and then the
calculation is performed on the operands. The
mathematical operations such as addition, subtraction,
multiplication etc. can be performed at bit-level for faster
processing.
• Bit-wise operators
– bitwise AND (&)
– bitwise OR (|)
– bitwise NOT (~)
– bitwise XOR (^)
• Bitwise AND, OR, XOR takes two operands while bitwise
NOT takes one operand.
Assignment operator
• Assignment operators are used to assign value
to a variable.
• The left side operand of the assignment
operator is a variable and right side operand
of the assignment operator is a value.
• The value on the right side must be of the
same data-type of variable on the left side
otherwise the compiler will raise an error.
• For example: a = 10;
Few Assignment Exceptions in C
• (a += b) can be written as (a = a + b)
• (a -= b) can be written as (a = a - b)
• (a *= b) can be written as (a = a * b)
• (a /= b) can be written as (a = a / b)
Conditional operator (?:)
• Conditional operator is of the form
Expression1 ? Expression2 : Expression3

Here, Expression1 is the condition to be evaluated. If the


condition(Expression1) is True then we will execute and
return the result of Expression2 otherwise if the
condition(Expression1) is false then we will execute and
return the result of Expression3.
This is an alternative of if-else statement.
e.g. c=a>b?a:b; if a=6,b=8 then c=8
Class Board 08-06-2021
Type Casting Example
#include<stdio.h>
int main()
{
int a=1,b=3;
float d;
d=(float)a/b;
printf("The result is= %f", d);
return 0;
}

You might also like