C Language
Modules-02: Variable, Keyword, Datatypes & Type of Operator
National Institute of Electronics & Information Technology
Ministry of Electronics & Information Technology (MeitY), Government of India
Gorakhpur Center
/GKP.NIELIT
http://www.nielit.gov.in/gorakhpur
@GKP_NIELIT
/NIELITIndia
/school/NIELITIndia
Contents to be covered
/GKP.NIELIT
http://www.nielit.gov.in/gorakhpur
@GKP_NIELIT
/NIELITIndia
/school/NIELITIndia
The fundamental components of a program
/GKP.NIELIT
http://www.nielit.gov.in/gorakhpur
@GKP_NIELIT
/NIELITIndia
/school/NIELITIndia
/GKP.NIELIT
http://www.nielit.gov.in/gorakhpur
@GKP_NIELIT
/NIELITIndia
/school/NIELITIndia
/GKP.NIELIT
http://www.nielit.gov.in/gorakhpur
@GKP_NIELIT
/NIELITIndia
/school/NIELITIndia
Rules for Variables
/GKP.NIELIT
http://www.nielit.gov.in/gorakhpur
@GKP_NIELIT
/NIELITIndia
/school/NIELITIndia
/GKP.NIELIT
http://www.nielit.gov.in/gorakhpur
@GKP_NIELIT
/NIELITIndia
/school/NIELITIndia
Reserved Keywords of C Language
/GKP.NIELIT
http://www.nielit.gov.in/gorakhpur
@GKP_NIELIT
/NIELITIndia
/school/NIELITIndia
Constant in C
Syntax: const data type var_name=expression;
Example: const float pi=3.147
Followings are the different types of constants :
1. Real Constant :
It must have a decimal point which may be positive or negative.
Example: +194.143, -416.41
2. Integer Constant :
It should not contain a decimal place.
It can be positive or negative.
Example: 1990, 194, -394
/GKP.NIELIT
http://www.nielit.gov.in/gorakhpur
@GKP_NIELIT
/NIELITIndia
/school/NIELITIndia
Constant in C Contd..
3. Character Constant :
It is a single alphabet or a digit or a special symbol enclosed in a single quote.
Maximum length of a character constant is 1.
Example: 'T', '9', '$'
4. String Constant :
It may contain letters, digits, special characters and blank space.
Example: “Hello friends"
/GKP.NIELIT
http://www.nielit.gov.in/gorakhpur
@GKP_NIELIT
/NIELITIndia
/school/NIELITIndia
Backslash Character Constant in C
/GKP.NIELIT
http://www.nielit.gov.in/gorakhpur
@GKP_NIELIT
/NIELITIndia
/school/NIELITIndia
Comment in C
/GKP.NIELIT
http://www.nielit.gov.in/gorakhpur
@GKP_NIELIT
/NIELITIndia
/school/NIELITIndia
Translator
Compilers, which translate an entire program at once into an executable file;
Interpreters, which translate and execute code line by line; and
Assemblers, which convert low-level assembly language into machine code.
/GKP.NIELIT
http://www.nielit.gov.in/gorakhpur
@GKP_NIELIT
/NIELITIndia
/school/NIELITIndia
Editor Vs compiler
/GKP.NIELIT
http://www.nielit.gov.in/gorakhpur
@GKP_NIELIT
/NIELITIndia
/school/NIELITIndia
Error in Programming
The C compiler will catch these errors and give you error messages,such as Missing Parenthesis (}), Missing semicolon, Printing the value of variable without declaring it.
Example: x + 1 = x; (should be x = x+1; for a valid assignment statement)
The C compiler will not catch these errors. Such as division by zero error.
Example: User enters the value 0 and your code reads this value into � variable x, and then computes 1/x .
The C compiler will not catch these errors. Program will run and not generate any error messages but results outputted by the program are incorrect.
Example: User programs solution using the wrong formula.
/GKP.NIELIT
http://www.nielit.gov.in/gorakhpur
@GKP_NIELIT
/NIELITIndia
/school/NIELITIndia
Data types in C
/GKP.NIELIT
http://www.nielit.gov.in/gorakhpur
@GKP_NIELIT
/NIELITIndia
/school/NIELITIndia
Data Types in C
/GKP.NIELIT
http://www.nielit.gov.in/gorakhpur
@GKP_NIELIT
/NIELITIndia
/school/NIELITIndia
Data Types in C
“Data type can be defined as the type of data of variable or constant store.”
/GKP.NIELIT
http://www.nielit.gov.in/gorakhpur
@GKP_NIELIT
/NIELITIndia
/school/NIELITIndia
Variable Declaration and definition
Syntax:Data Type Variable Name
Ex: int a;
/GKP.NIELIT
http://www.nielit.gov.in/gorakhpur
@GKP_NIELIT
/NIELITIndia
/school/NIELITIndia
Variable Declaration and definition
/GKP.NIELIT
http://www.nielit.gov.in/gorakhpur
@GKP_NIELIT
/NIELITIndia
/school/NIELITIndia
Initialization and Assignment:��
int a=10
a=100
/GKP.NIELIT
http://www.nielit.gov.in/gorakhpur
@GKP_NIELIT
/NIELITIndia
/school/NIELITIndia
Type Promotion Rules
/GKP.NIELIT
http://www.nielit.gov.in/gorakhpur
@GKP_NIELIT
/NIELITIndia
/school/NIELITIndia
Integer Promotions:�
/GKP.NIELIT
http://www.nielit.gov.in/gorakhpur
@GKP_NIELIT
/NIELITIndia
/school/NIELITIndia
Types of Operator
/GKP.NIELIT
http://www.nielit.gov.in/gorakhpur
@GKP_NIELIT
/NIELITIndia
/school/NIELITIndia
Arithmetic Operator
/GKP.NIELIT
http://www.nielit.gov.in/gorakhpur
@GKP_NIELIT
/NIELITIndia
/school/NIELITIndia
Example of Arithmetic Operator
#include<stdio.h>
int main()
{
int a, b, add, sub, mul, div, rem;
printf("Enter a, b values : ");
scanf("%d%d",&a,&b); // Reading two values
add=a+b; // Addition Operator
sub=a-b; // Subtraction Operator
mul=a*b; // Multiplication Operator
div=a/b; // Division Operator
rem=a%b; // Remainder (Modulo) Operator
printf("Result of addition is=%d\n", add);
printf("Result of subtraction=%d\n", sub);
printf("Result of multiplication is=%d\n", mul);
printf("Result of division is=%d\n", div);
printf("Result of remainder=%d\n",rem);
return 0;
}
/GKP.NIELIT
http://www.nielit.gov.in/gorakhpur
@GKP_NIELIT
/NIELITIndia
/school/NIELITIndia
Logical Operator
/GKP.NIELIT
http://www.nielit.gov.in/gorakhpur
@GKP_NIELIT
/NIELITIndia
/school/NIELITIndia
Example of Logical Operator
Example:
#include<stdio.h>
void main()
{
int a, b;
printf(“Enter values for a and b : ");
scanf(“%d %d", &a, &b);
printf("\n %d",(a<b)&&(a!=b));
printf("\n %d",(a<b)||(b<a));
printf("\n %d",!(a==b));
}
/GKP.NIELIT
http://www.nielit.gov.in/gorakhpur
@GKP_NIELIT
/NIELITIndia
/school/NIELITIndia
Relational Operator
/GKP.NIELIT
http://www.nielit.gov.in/gorakhpur
@GKP_NIELIT
/NIELITIndia
/school/NIELITIndia
Example of Relational Operator
Example:
#include<stdio.h>
void main()
{
int a, b;
printf(“Enter values for a and b : ");
scanf("%d %d", &a, &b);
printf("\n The < value of a is %d", a<b);
printf("\n The <= value of a is %d", a<=b);
printf("\n The > value of a is %d", a>b);
printf("\n The >= value of a is %d", a>=b);
printf("\n The == value of a is %d", a==b);
printf("\n The != value of a is %d", a!=b);
}
/GKP.NIELIT
http://www.nielit.gov.in/gorakhpur
@GKP_NIELIT
/NIELITIndia
/school/NIELITIndia
Increment/Decrement Operator
For example, the expression,
count = count + 1; //increment the value of count by 1 is equivalent to count++;
Operator Use Description
++ i++ Increments i by 1; evaluates to
the value of i before it was incremented
++ ++i Increments i by 1; evaluates to
the value of i after it was incremented
-- i-- Decrements i by 1; evaluates to
the value of i before it was decremented
-- --i Decrements i by 1; evaluates to
the value of i after it was decremented
/GKP.NIELIT
http://www.nielit.gov.in/gorakhpur
@GKP_NIELIT
/NIELITIndia
/school/NIELITIndia
Example of Increment/Decrement Operator
Example:
#include<stdio.h>
void main()
{
int a,b,c;
printf("Enter the values for a and b :");
scanf("%d %d", &a, &b);
printf("\n The value of c is %d", c=++a);
printf("\n The value of c is %d", c=a++);
printf("\n The value of c is %d", c=--b);
printf("\n The value of c is %d", c=b--);
}
/GKP.NIELIT
http://www.nielit.gov.in/gorakhpur
@GKP_NIELIT
/NIELITIndia
/school/NIELITIndia
Conditional Operator
Syntax:
exp1 ? exp2 : exp3;
/GKP.NIELIT
http://www.nielit.gov.in/gorakhpur
@GKP_NIELIT
/NIELITIndia
/school/NIELITIndia
Example of Conditional Operator
Example:
void main()
{
int x;
printf("enter any number");
scanf("%d",&x);
x>0? printf("Positive"):printf("Negative");
}
/GKP.NIELIT
http://www.nielit.gov.in/gorakhpur
@GKP_NIELIT
/NIELITIndia
/school/NIELITIndia
Bitwise Operator
/GKP.NIELIT
http://www.nielit.gov.in/gorakhpur
@GKP_NIELIT
/NIELITIndia
/school/NIELITIndia
Example of Bitwise Operator
let , a=10 b=8
a= 1010 (in binary)
b= 1000 (in binary)
Bitwise AND operator(&)
1010
1000
&
1000
8 | 4 | 2 | 1 |
1 | 0 | 0 | 0 |
output =8
/GKP.NIELIT
http://www.nielit.gov.in/gorakhpur
@GKP_NIELIT
/NIELITIndia
/school/NIELITIndia
Bitwise (OR) operator(|)
1010
| 1000
1010
8 | 4 | 2 | 1 |
1 | 0 | 1 | 0 |
Output= 10
Bitwise (XOR) operator (^)
1010
^ 1000
0010
8 | 4 | 2 | 1 |
0 | 0 | 1 | 0 |
Output=2
/GKP.NIELIT
http://www.nielit.gov.in/gorakhpur
@GKP_NIELIT
/NIELITIndia
/school/NIELITIndia
Bitwise complement operator (~)
If a=10
b = ~a
Then , b= - (a+1)
b=-11
/GKP.NIELIT
http://www.nielit.gov.in/gorakhpur
@GKP_NIELIT
/NIELITIndia
/school/NIELITIndia
Example of Bitwise Operator
x=2 (in binary 0010 )
y=7 ( in binary 0111 )
Right Shift(>>)
Z = x>>2
Z= 0
8 | 4 | 2 | 1 |
0 | 0 | 1 | 0 |
0 | 0 | 0 | 0 |
Remaining fill by 0
/GKP.NIELIT
http://www.nielit.gov.in/gorakhpur
@GKP_NIELIT
/NIELITIndia
/school/NIELITIndia
NOTE:
Left shift (<<)
Z= x<<2
Z= 8
8 | 4 | 2 | 1 |
0 | 0 | 1 | 0 |
1 | 0 | 0 | 0 |
Remaining fill by 0
/GKP.NIELIT
http://www.nielit.gov.in/gorakhpur
@GKP_NIELIT
/NIELITIndia
/school/NIELITIndia
Example of Bitwise Operator
Example:
#include<stdio.h>
#include<conio.h>
Void main()
{
int a,b;
printf(“enter the value of a & b”);
scanf(“%d%d”,&a,&b);
printf(“AND %d\n”, a&b);
printf(“OR %d\n”, a|b);
printf(“XOR %d\n”, a^b);
printf(“COMPLE %d\n”, ~a);
printf(“LeftS %d\n”, a<<2);
printf(“RightS %d\n”, a>>2);
getch();
}
If , a=10 b=8
Output—
AND =8
OR = 10
XOR=2
COMPLE= -11
LeftS= 40
RightS=2
/GKP.NIELIT
http://www.nielit.gov.in/gorakhpur
@GKP_NIELIT
/NIELITIndia
/school/NIELITIndia
Arity operator
/GKP.NIELIT
http://www.nielit.gov.in/gorakhpur
@GKP_NIELIT
/NIELITIndia
/school/NIELITIndia
Operator precedence
/GKP.NIELIT
http://www.nielit.gov.in/gorakhpur
@GKP_NIELIT
/NIELITIndia
/school/NIELITIndia
1. If a = 20 , b=10,c=15,d=5 .find the value of e
e = a + (b * c) / d;
Assignments
2 . Program to add, subtract, multiply, division of two numbers
/GKP.NIELIT
http://www.nielit.gov.in/gorakhpur
@GKP_NIELIT
/NIELITIndia
/school/NIELITIndia
Thank You
Any Query
/GKP.NIELIT
http://www.nielit.gov.in/gorakhpur
@GKP_NIELIT
/NIELITIndia
/school/NIELITIndia