You are on page 1of 36

National Institute of Electronics & Information Technology

Gorakhpur Center
Ministry of Electronics & Information Technology (MeitY), Government of India

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Contents to be covered
• Variable
• Keyword
• Data Type
• Types of Operators
• Basic programming Examples

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Variable
 Character Set
• A character refers to the digit, alphabet or special symbol used to data
representation.
 Alphabets :          A-Z, a-z
 Digits : 0-9
 Special Characters : ~!@#$%^&*()_+{}[]-<>,./?\|:;"
'
 White Spaces : Horizontal tab, Carriage return, New line

 Identifier
• Identifier is the name of a variable that is made up from combination of
alphabets, digits and underscore.

 Variable
• It is a data name which is used to store data and may change during
program execution. It is opposite to constant. Variable name is a name
http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia
Rules for Variables
 First character should be letter or alphabet.
 Keywords are not allowed to use as a variable name.
 White space is not allowed.
 C is case sensitive i.e. UPPER and lower case are significant.
 Only underscore, special symbol is allowed between two characters.

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Reserved Keywords of C Language
 There are totally 32 (Thirty Two) keywords used in a C programming.

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Reserved Keywords of C Language
 Keywords are the system defined identifiers.

 All keywords have fixed meanings that do not change.

 White spaces are not allowed in keywords.

 Keyword may not be used as an identifier.

 It is strongly recommended that keywords should be in lower case letters.

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Constant in C
 A constant is an entity that doesn't change during the execution of a
program.
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
http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @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"

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Backslash Character Constant in C
 C supports some special escape sequence characters that are used to do special
tasks.

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Comment in C

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Error in Programming
 Syntax Errors
The C compiler will catch these errors and give you error messages,s uch 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)
 Run-time Errors
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 .
 Logical Errors
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.
http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia
Data Types in C

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Data Types in C
“Data type can be defined as the type of data of variable or constant store.”
 Followings are the most commonly used data types in C.

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Types of Operator
 Operator is a symbol that is used to perform operations.
 Followings are the most commonly used data types in C.

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Arithmetic Operator
 Arithmetic operators are used to perform numerical calculations among the
values.

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @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;
} http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia
Logical Operator
 These operators are used for testing more than one condition and making
decisions.
 C has three logical operators they are:

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @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));
}

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Relational Operator
 Relational Operators are used to compare two quantities and take certain decision
depending on their relation.
 If the specified relation is true it returns one.
 If the specified relation is false it returns zero.

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @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);
}

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Increment/Decrement Operator
 C includes a unary increment operator (++) and decrement operator (--).
 Increment and decrement operators increase and decrease a value stored in a number variable by 1.
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

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @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--);
}

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Conditional Operator

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @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");
}

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Bitwise Operator
 These operators works on bit level
 Applied to Integers only.

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Example of Bitwise Operator
Let A=0x56 and B=0x32

A & B (Bitwise AND) A | B (Bitwise OR)


0101 0110 0101 0110
0011 0010 0011 0010
---------------------
0001 0010 --------------------- 0
--------------------- 111 0110

A ^ B (Bitwise XOR) ---------------------


0101 0110
0011 0010 ~ A (Complement)
--------------------- 0101 0110
0 1 1 0 0 10 0
--------------------- --------------------- 1
http://www.nielit.gov.in/gorakhpur /GKP.NIELIT
010 1001
@GKP_NIELIT /NIELITIndia /school/NIELITIndia
Example of Bitwise Operator
x=2 #Binary Equivalent of 2 is 0010
y=7 #Binary Equivalent of 7 is 0111

Right Shift
Left Shift
z=y>>1
z=y<<1
print(z) Output
Output print(z)
z=y>>2 3 14
z=y<<2
print(z) 1 28
print(z) 56
z=y>>3 0
z=y<<3
print(z)
print(z)
NOTE:
For multiply given number by two, left shifted by one time, i.e., a<<1
For divide given number by two, right shifted by one time, i.e., a>>1
http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia
Example of Bitwise Operator
Example:
void main()
{
int x,y;
clrscr();
printf(“Enter value of x:”);
scanf(“%d”,&x);
y=x<<3;
printf(“Left shifted data=%d”,y);
printf(“Right shifted data=%d”,x>>3);
}

Output: Enter value of x:16


Left shifted data=128
Right shifted data=2
http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia
Quiz
Time

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Quiz
Time

1. Which of the following is allowed in a C Arithmetic instruction


A. [] B. {}
C. () D. None of the above Answer : C
 
2. Which of the following shows the correct hierarchy of arithmetic operations in C
A. / + * - B. * - / +
C. + - / * D. * / + - Answer : D
 
3. Bitwise operators can operate upon?
A. double and chars B. floats and doubles
C. ints and floats D. ints and chars Answer : D

4. Among unary operation which operator represents increment?


(A) -- (B) ++
(C) - (D) ! Ans: B 
http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia
Quiz
Time

5. What is C Tokens?
A. The smallest individual units of c program
B. The basic element recognized by the compiler
C. The largest individual units of program
D. A & B Both Answer : D
 
6. What is Keywords?
A. Keywords have some predefine meanings and these meanings can be changed.
B. Keywords have some unknown meanings and these meanings cannot be changed.
C. Keywords have some predefine meanings and these meanings cannot be changed.
D. None of the above Answer : C
 

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Quiz
Time

7. What is constant?
A. Constants have fixed values that do not change during the execution of a program
B. Constants have fixed values that change during the execution of a program
C. Constants have unknown values that may be change during the execution of a program
D. None of the above Answer : A
 
8. Which is the right way to declare constant in C?
A. int constant var =10; B. int const var = 10;
C. const int var = 10; D. B & C Both Answer : D
 
9. What will be the output of the following arithmetic expression ?
5+3*2%10-8*6
A. -37 B. -42
C. -32 D. -28 Ans: a

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Quiz
Time

10. What will be the output of the following statements ?


int i = 3;
printf("%d%d",i,i++);
A. 34 B. 43
C. 44 D. 33 Ans: b
 

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Assignments
 Program to add, subtract, multiply, division of two numbers
 Program to find exponentiation (power of a number).

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


References

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Thank You
Any Query

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia

You might also like