Tokens in C
A token is a smallest individual part in a program which is ready for execution.
Tokens are classified as follows.
1. Variables or Identifiers 2. Constants
3. Keywords 4. Data types 5. Operators
Variables or Identifiers
A variable is an identifier which is used to store data and it can take different values during the program execution.
Identifiers are name of variables, functions and arrays. Identifiers are user defined names consisting of a sequence of letters and digits.
Syntax:
<data type><variable_name>;
Rules to declare the variables
The following are the rules for declaring the variables.
Rules for constructing variable names
1. The first character of the variable name must either be alphabet or underscore. It should not start with the digit
2.No commas and blanks are allowed in the variable name
3.No special symbols other than underscore are allowed in the variable name
4. They can take maximum of length 31 characters.
5. Upper case and lower case variables are different i.e., int a is different from int A.
We need to declare the type of the variable name before making use of that name in the program. Type declaration can be done as follows:
To declare a variable as integer, follow the below syntax:
Here int is the type of the variable named variable_name. ‘int’ denotes integer type.
Following are the examples of type declaration statements:
E.g.:
int p, n;
float r;
int p, n;float r;
Constants:
A constant is an entity whose value does not change throughout the program execution. To declare the constants in C, it uses a key word called ‘const’.
Syntax to declare the constants in C
const <data type>< variable_name>= <value>;
Example: const int a=10;
In the above syntax ‘const’ is a keyword used to declare constant and now the variable ‘a’ here can be treated as constant and its value cannot be changed in the program during the program execution.
Types of Constants in C
Integer Constants:
These are the sequence of numbers from 0-9 without decimal points or fractional part or any other symbols.
The constants are formed with numbers including 0, and which takes either positive as well as negative values.
Example: 123, 144, -35, 0 etc.
Floating or Real Constants
A constant which is formed with floating point or real values is called a floating constant. This takes positive as well as negative values.
Example: 12.5, -10.45
Single Character Constant:
A character constant is a single character that is enclosed with a pair of single quotes. They are also represented with a single digit or a single special symbol or white space enclosed within a pair of single quotes.
Example: ‘s’, ‘9’, ‘ ‘
String Constant
A string constant is a sequence of characters enclosed within a double quotes. The string may be combination of all kinds of symbols.
Example: “BVRICE”, “a”, “11121”.
Keywords
Keywords are standard identifiers that have standard predefined meaning in C. Keywords are all lowercase, since uppercase and lowercase characters are not equivalent it's possible to utilize an uppercase keyword as an identifier but it's not a good programming practice.
Points to remember
1. Keywords can be used only for their intended purpose.
2. Keywords can't be used as programmer defined identifier.
3. The keywords can't be used as names for variables.
auto double int struct
break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while
Data types in C
All compilers support a variety of data types which enables the programmer to select the appropriate data type as for the need of application.
C data types are defined as the data storage format that a variable can store a data to perform a specific operation.
Data types are used to define a variable before to use in a program.
The following are the different data types that are available in C programming.
These are explained below.
Primitive data types are the first form – the basic data types (int,char,float,double).
Derived data types are a derivative of primitive data types known as arrays, pointer and function.
The primitive data types are explained in below.
Data type Size Min Value Max Value Formatted Specifier
int/short int 2 -32768 +32767 %d or %i
long/long int 4 -2147483647 +2147483647 %ld
unsigned int 2 0 65535 %u
unsigned long int 4 0 4294967295 %lu
char 1 -128 +127 %c
unsigned char 1 0 255 %c
float 4 3.4 e-38 3.4 e+38 %f
double 8 1.7 e-308 1.7 e+308 %lf
long double 10 3.4 e-4932 3.4 e+4932 %lf
Type Casting in C
When variables and constants of different types are combined in an expression then they are converted to same data type. The process of converting one predefined type into another is called type conversion.
Type conversion in c can be classified into the following two types:
Implicit Type Conversion
When the type conversion is performed automatically by the compiler without programmer’s intervention, such type of conversion is known as implicit type conversion or type promotion.
The compiler converts all operands into the data type of the largest operand.
Consider the following code:
int x=7, y=5 ;
float z;
z=x/y; /*Here the value of z is 1*/
If we want to get the exact value of 7/5 then we need explicit casting from int to float:
Explicit Type Conversion
The explicit type conversion is also known as type casting.
Type casting in c is done in the following form:
(data_type)expression;
where, data_type is any valid c data type, and expression may be constant, variable or expression.
For example,
x=(int)a+b*d;
The following program illustrates the type casting in C.
/*To write a c program that illustrates the type casting*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a=5, b=2;
float c,d;
clrscr();
c=a/b;
printf("\n a/b=%f",c);
d=(float) a/b;
printf("\n (float) a/b=%f",d);
getch();
}
Output:
a/b=2.000000
Operators In C
Operators in C
C uses different kinds of operators. An operator indicates an operation to be performed on data. Every programming language needs to perform varies kinds of operations to process the data. For that purpose every programming language designed with a set of operators.
Definition of an Operator
An operator is a symbol that performs operations on the operands. Operands are the data items that are operated by the operator.
Types of operators
The following are the different types of operators available in C.
1. Arithmetical operators
2. Relational or Comparison Operators 3. Logical or Decision Making Operators 4. Comma Operator
5. Assignment Operator
6. Increment and Decrement Operators. 7. Bitwise Operators
8. Conditional Operators. 9. sizeof operator
1. Arithmetical Operators
In some cases we need to perform addition, subtraction, multiplication, division and modulo division operations which are called Arithmetical operations. These are performed by using the arithmetical operators in C.
There are two types of arithmetical operators. They are
Unary operators
Binary operators.
Unary operators
An operator manipulates on only one operand is called as Unary operator. The following are the some of the unary operators in C.
Operator Description Example
- Unary minus x=-2
++ Unary Increment x++ or ++x
-- Unary Minus x—or –x
& Address operator &x sizeof Gives the size of the variable sizeof(x) Binary Operators
An operator which takes minimum of two operands is called as a binary operator.
Syntax: operand1 operator operand2;
Example: a + b;
These operators are commonly used in the most of the programming languages. The following are the available arithmetical operators in C.
Operator Description Example
+ Addition a+b
- Subtraction a-b
* Multiplication a*b
/ Division a/b
% Modulo Division a%b 2. Relational or Comparison Operators
These operators are used to distinguish between two values depending upon their relations. These operators provide the relationship between two expressions.
A relational operator compares the values and returns either 0 or 1. It returns ‘1’ if the relational expression is true and it returns ‘0’ if the relational expression is false.
Following table shows all the relational operators supported by C language. Assume variable A holds 10 and variable B holds 20, then:
Operator Description Example
== Equal to (A == B) is not true.
3. Logical operators
The most important operators in ‘C’ are logical operators. They are basically used to combine two or more relational expressions. The main objective of logical operators is to work with multiple expressions.
Following table shows all the logical operators supported by C language. Assume variable Aholds 1 and variable Bholds 0, then:
Operator Description Example
&&
Called Logical AND operator. If both the operands are non-zero, then condition becomes true.
(A && B) is false.
||
Called Logical OR Operator. If any of the two operands is non-zero, then
condition becomes true. (A || B) is true.
!
Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false.
!(A && B) is true.
The truth tables for the logical operators are as follows
a b a&&b a||b !a
1 1 1 1 0
1 0 0 1 0
0 1 0 1 1
0 0 0 0 1
4. Comma Operator
Comma operator is used to separate two or more expressions. Comma operator has the lowest priority among all the operators.
Example:
int a,b;
scanf(“%d%d”,&a,&b);
5. Assignment Operator
In C programs, values for the variables are assigned using assignment operators.
Other assignment operators in C language are given below.
Operators Example Explanation
Simple assignment
operator
= sum=10 10 is assigned to variable sum
Compound assignment operators
+= sum+=10 This is same as sum=sum+10…
-= sum-=10 This is same as sum = sum-10 *= sum*=10 This is same as sum = sum*10 /+ sum/=10 This is same as sum = sum/10
%= sum%=1
0 This is same as sum = sum%10
&= sum&=1
0 This is same as sum = sum&10 ^= sum^=10 This is same as sum = sum^10
6. Increment and Decrement Operators
Increment operator is used to increment the current value of variable by adding integer 1 and decrement operator is used to decrement the current value of variable by subtracting integer 1. Increment operator can be applied to only variables.
Increment operator is denoted by ++ and decrement operator is denoted by --.
Different Types of Increment and Decrement Operators:
In C Programming we have two types of increment and decrement operator i.e Pre-Increment and Post-Pre-Increment, Pre-decrement and Post decrement Operators.
A. Pre Increment and Pre Decrement Operators
Pre-increment and pre-decrement operators are used to increment and decrement the value of variable before using in the expression. In the Pre-Increment value is first incremented and then used inside the expression.
b = ++y;
B. Post Increment and Post-decrement Operators
Post-increment and Post-decrement operators are used to increment and decrement the value of variable as soon as after executing expression completely in which post increment and decrement is used. In the Post-Increment value is first used in an expression and then incremented.
b = x++;
In this example suppose the value of variable ‘x’ is 5 then value of variable ‘b’ will be 5 because old value of ‘x’ is used.
Live Program : C Program to Perform Increment Operation in C Programming
#include<stdio.h> void main() {
int a,b,x=10,y=10; a = x++;
b = ++y;
printf("Value of a : %d",a); printf("Value of b : %d",b); }
Output:
Value of a : 10 Value of b : 11
7. Bitwise Operators
C compiler uses bit-wise operators to perform operations on bits(0`s and 1`s). these are basically used to transfer the data from source to destination where shifting of data is required. The following table shows the list of bit-wise operators.
Operators Meaning of operators & Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR ~ Bitwise complement << Shift left
Bitwise AND(&)
The output of logical AND is 1 if both the corresponding bits of operand is 1. If either of bit is 0 or both bits are 0, the output will be 0.
Example:
12 = 00001100 (In Binary) 25 = 00011001 (In Binary) Bit Operation of 12 and 25 00001100
& 00011001 _______________
00001000 = 8 (In decimal)
Bitwise OR(|)
The output of bitwise OR is 1 if either of the bit is 1 or both the bits are 1. In C Programming, bitwise OR operator is denoted by |.
12 = 00001100 (In Binary) 25 = 00011001 (In Binary)
Bitwise OR Operation of 12 and 25 00001100
| 00011001 ________
00011101 = 29 (In decimal)
Bitwise XOR(exclusive OR) operator
The output of bitwise XOR operator is 1 if the corresponding bits of two operators are opposite.
12 = 00001100 (In Binary) 25 = 00011001 (In Binary)
Bitwise XOR Operation of 12 and 25 00001100
^ 00011001 ________
Bitwise compliment operator
Bitwise compliment operator is an unary operator(works on one operand only). It changes the corresponding bit of the operand to opposite bit,i.e., 0 to 1 and 1 to 0. It is denoted by ~.
35=00100011 (In Binary)
Bitwise complement Operation of 35 ~ 00100011
________
11011100 = 220 (In decimal)
Right Shift Operator
Right shift operator moves the all bits towards the right by certain number of bits which can be specified. It is denoted by >>.
Left Shift Operator
Left shift operator moves the all bits towards the left by certain number of bits which can be specified. It is denoted by <<.
8. Conditional Operators
Conditional operator contains a condition followed by two statements or values. If the condition is true the first statement is executed otherwise the second statement is executed.
Syntax:
Exp=condition ? exp1 : exp2;
Working process of Conditional operator
First the compiler evaluates the ‘condition’; if it is true exp1 will become resultant value for the entire expression. If condition is false exp2 eill be executed by ignoring exp1 i.e., exp2 will become the resultant value of the entire expression.
The conditional operator is also called as “Ternary Operator” because it uses three expressions at a time.
The following program illustrates the use of conditional operators in C.
/* C-Program to find the given number is even or odd */
#include<stdio.h>
int main()
int num;
printf("Enter the Number : ");
scanf("%d",&num);
(num%2==0)?printf("Even"):printf("Odd");
}
Output:
Enter the Number : 10
Even
9. Sizeof operator
The sizeof operator returns the size of its operand in bytes. The sizeof operator always precedes its operand. The operand may be an expression or it may be a cast.
Example:
Suppose i is an integer variable and c is a character type variable then printf(“Size of integer : %d”,sizeof(i));
printf(“Size of character : %c”,sizeof(c));
Might generate the following output : Size of integer : 2
Size of character : 1
*************** Operator Precedence and Associativity
Precedence of operators
If more than one operators are involved in an expression then, C language has predefined rule of priority of operators. This rule of priority of operators is called operator precedence.
In C, precedence of arithmetic operators(*,%,/,+,-) is higher than relational operators(==,!=,>,<,>=,<=) and precedence of relational operator is higher than logical operators(&&, || and !). Suppose an expression:
(a>b+c&&d)
This expression is equivalent to: ((a>(b+c))&&d)
Associativity of operators
Associativity indicates in which order two operators of same precedence(priority) executes. Let us suppose an expression:
a==b!=c
Here, operators == and != have same precedence. The associativity of both == and != is left to right, i.e, the expression in left is executed first and execution take pale towards right. Thus, a==b!=c equivalent to :
(a==b)!=c
Important Questions
Long Answer Questions1. Explain about the Tokens in C.
2. Define an Operator and explain different types of Operators in C. Short Answer Questions
1. Define a variable and explain the rules for declaring a variable. 2. Explain about the Constants in C.
3. Write about the Type conversion in C with example.
4. Define Data type and explain different data types available in C. 5. Explain Relational and Logical Operators in C with examples. 6. Write about the Conditional Operators in C.
7. Explain about the Bit-wise operators in C with examples.