You are on page 1of 14

Programing In C [SMT. V.B.

NANDOLA COMPUTER SCIECNE COLLEGE, BHACHA]

Chapter- 2 DATA TYPES & OPERATORS

Q.1 Explain datatype of ‘C’ language

 The constant and variables used in a C program should be declared in it before


using them for execution.
 C language is rich in its data types. Data types indicate which kind of data is
to be stored in a variable.
 C supports several data types. All compilers supports four fundamental
primary data types namely
1. Integer (int)
2. Character (char)
3. Floating points(float),
4. Double precision floating point(double)
 In brief, all data types whose range or capacity, storage bytes and format
specifier is shown in the following table one by one data type.

INTEGER
FORMAT
NO DATATYPE RANGE BYTES
SPECIFIER
1 int -32768 TO +32767 2 %d
2 short signed int -32768 TO +32767 2 %d
3 short int -32768 TO +32767 2 %d
4 signed int -32768 TO +32767 2 %d
5 unsigned int 0 TO 65535 2 %u
6 short.unsigned int 0 TO 65535 2 %u
 Primary Data Type is integer (int) whose range is -32768 To +32767 and
memory storage capacity is 2 bytes and for accessing and storage purpose %d
is used as a format specifier.
 With int data type short signed keywords are used, short and signed integer is
the same as int , but unsigned int provided range 0 To 65535 whose format
specifier is %u and for printing address of a RAM unsigned int is used .
 With a signed and unsigned keyword short keyword is also used but it will not
affect over a range.

DATA FORMAT
NO RANGE BYTES
TYPES SPECIFIER
7 long int -2147483648 To+2147483647 4 %ld
long signed
8 -2147483648 To +2147483648 4 %ld
int
long
9 0 to 4294967295 4 %lu
unsigned int
 Long is keyword it provides double range with respect to int datatype .
 Storage capacity becomes also twice with respect to int datatype (4 byte).
Prepared by Mr. Pankaj M. Zalera 1
Programing In C [SMT. V.B. NANDOLA COMPUTER SCIECNE COLLEGE, BHACHA]

 Long unsigned provides double range for positive numbers only.


 Long int and long signed int both declaration are same in its range.bytes and
format specifier.
FLOAT / DOUBLE

DATA FORMAT
NO RANGE BYTES
TYPES SPECIFIER
1 float -3.4 e 38 to +3.4 e 38 4 %f
2 double -1.7 e 308 To +1.7 e 308 8 %lf
3 long double -1.7 e 4932 to +1.7 e 4932 10 %Lf

 Float, double and Long double store fractional numbers.


 It provides higher range with respect to int and character data types.
 Analyses of scientific data can be done the help of double and long double
data types.
 Floating point’s numbers are stored in 32 bits I.e. 4 bytes. With 6 digits of
precision and storage purpose %f is used as format specifiers.
 Double occupies 64 bits i.e. 8 bits with 14 digits of precision, whose format
specifier is %lf.
 Long double floating points, occupies 10 bytes i.e 80 bits in memory, whose
format specifier is same as well as double i.e %Lf.

CHARACTER

FORMAT
NO DATA TYPES RANGE BYTE
SPECIFIER
1 char -128 to +127 1 %c
2 signed char -128 to +127 1 %c
3 unsigned char 0 to 255 1 %c

 Character and signed character both declaration are same whose ASCII range
is -128 to +127 and unsigned character supports 0 to 255 ASCII range.
 Memory storage capacity is 1 byte for a single character and format specifier
is %c with the help of char keyword and declaring fix size string can be scan.
e.g. char s[10];
 String can be declared with char keyword and pointer *.
e.g. char *s;

Q.2. Write a note on “Operators.”

 C supports a rich set of Operators an operators is symbol that tells the


computer to perform certain mathematical or logical manipulations. Operators
are used in programs manipulate data and variable. They usually form a part
of the mathematical or logical expressions.

Prepared by Mr. Pankaj M. Zalera 2


Programing In C [SMT. V.B. NANDOLA COMPUTER SCIECNE COLLEGE, BHACHA]

 C operators can be classified into eight categories. They include:

1. Arithmetic.
2. Relational
3. Logical.
4. Assignment
5. Increment & Decrement.
6. Conditional.
7. Bitwise.
8. Special
1. Arithmetic Operator:

 ‘C’ provides all basic arithmetic operators. The operators +,-,* and / all work
in the same way as they do in other languages.
 These operators can operate on any datatype allowed in ‘C’.
 The modulor division produces the remainder of an integer division.
 ‘C’ does not have any operator for exponentiation.
 There are five types of an arithmetic operation as given below:

NO. OPERATOR MEANING


1 + Additional or plus
2 - Subtraction or minus
3 * Multiplication
4 / Division
5 % Modulo division

2. Relational operator / Comparison Operator

 The value of a relational expression is either one or zero. [true/false],


 C supports six relational operators in all.

No. Operators Meaning


1. < Is less than
2. <= Is less than or equal to
3. > Is greater than
4. >= Is greater than or equal to
5. == Is equal equal to
6. != Is not equal to
 For Example,
a<b or 1<20
where in a and b are the arithmetic expression , which may be simple constants
or variables or combination of them.

Prepared by Mr. Pankaj M. Zalera 3


Programing In C [SMT. V.B. NANDOLA COMPUTER SCIECNE COLLEGE, BHACHA]

3. Logical Operators:-
In addition to relational operators , C has following three logical operators :
NO. OPERATOR MEANING
1 && Logical AND
2 || Logical OR
3 ! Logical NOT
 The logical operators && and || are used when we want to test more than one
condition and make decisions.
 Some examples of the usage of logical expressions are:
1. if(age>55 && salary<1000)
2. if(number < 100 || number > 0)
3. if(year % 4 !=1)
 NOT operator to test only one condition and make decision

4. Assignment Operators:
 Assignment operators are used to assign the result of an expression to a
variable. We have seen the usual assignment operator “=”.
 In addition, C has a set of short hand assignment operators.

No. Operators Longhand Shorthand


1. += x=x+5 x+=5
2. -= x=x-5 x-=5
3. *= x=x*y x*=y
4. /= x=x/y x/=y
5. %= x=x%y x%=y

a=10, b=10 , c=10


a=b=c=10.

5. Increment & Decrement Operators :


 Increment and decrement operator is an unary operator. An unary operator is
work with only one operator . There are two types of unary operators.

Unary Operators

Prefix Operators Postfix Operators

Increment Decrement Increment Decrement


Operator Operator Operator Operator
++a --a a++ a--

Prepared by Mr. Pankaj M. Zalera 4


Programing In C [SMT. V.B. NANDOLA COMPUTER SCIECNE COLLEGE, BHACHA]

1. Prefix Operator :

 Prefix operator is an operator if the ++ sign or – sign is used before the


operand, it is called as prefix operator.
 For example:
++1 is a pre-increment
--1 is a pre-decrement
(First increment/decrement & then transferred.)

2. Postfix Operator :

 Postfix Operator is an operator if the ++ sign or – sign is used after the


operand , it is called as Postfix operator.
 For example:
1++ is a Post-increment
1-- is a Post-decrement
(First initialization & then increment/decrement.)
 NOTE:
 The increment operator ++ adds 1 to the operand value.
 The decrement operator – decrease 1 to the operand value.

6. Conditional Operator :

The Conditional operators ? and : are sometimes called Ternary


Operators since they take three arguments. In fact, they form a kind of
foreshortened if – then- else, Their general form is,

Syntax:

Expression1 ? Expression2 ? : Expression3;

What this expression says is :

“ If Expression1 is the true (That is , if its value is non-zero), then the value
returned will be.

Or

Variable=exp1 ? exp2 : exp3;

Expression 2 otherwise the value returned will be expression 3”.

For example:-

Y=(x > 5 ? 3 : 4);

This statement will store 3 in y if x is greater than 5, otherwise it will store


4 in y if x is greater than 5, otherwise it will store, 4 in y.

Prepared by Mr. Pankaj M. Zalera 5


Programing In C [SMT. V.B. NANDOLA COMPUTER SCIECNE COLLEGE, BHACHA]

7. Bitwise operators:

 One of C’s powerful features is a set of bit manipulation operators. These


permit the programmer to access and manipulate individual bits within a piece
of data.
 The various Bitwise operators available in C are shown in table.
No. Operators Meaning
1. ~ One’s Complement
2. >> Right Shift
3. << Left Shift
4. & Bitwise AND
5. | Bitwise OR
6. ^ Bitwise XOR (Exclusive OR]

 There are most of three types of bitwise operators are available in


programming ‘C’.
1. Bitwise Logical operators:
1. Bitwise AND[&] [Ampersand sign]
2. Bitwise OR[|][Vertical bar]
3. Bitwise Ex-OR[^]
2. Bitwise shift operators:
1. Left shift <<
2. Right shift >>
3. Bitwise Complement operators:
1. One’s complement ~
These operators can operate upon int and char but not on float and double.

1. Bitwise Logical operators:

 These are three types of Logical operators in C.

A. Bitwise AND operator [&]


 This operator is represented as &[Ampersand sign]. The & operator operates
on a pair of bits to yield a resultant bit.
 The rules that decide the value of the resultant bits are shown below.

Input Output (AB)


A B F
0 0 0
0 1 0
1 0 0
1 1 1
 Above that table, we can say that when both inputs are equal 1 then output is
1 otherwise 0.

Prepared by Mr. Pankaj M. Zalera 6


Programing In C [SMT. V.B. NANDOLA COMPUTER SCIECNE COLLEGE, BHACHA]

 The best is use of the AND operator is to check whether a particular.bit of an


operand is ON or OFF.
 For example:

A=10 1010
B=11 1011
A&B 1010

B. Bitwise OR Operator [|]:


 Another important Bitwise operator is the OR operator which is represented as
|.The rules that govern the value of the resulting bit obtained after ORing of
two bit shown in the following truth table.

Input Output (A+B)


A B F
0 0 0
0 1 1
1 0 1
1 1 1
 Above that table, we can say that when both inputs are equal to 0 then output
is always 0, otherwise 1.
 Using the Truth table confirm the result obtained on ORing the two operands
or shown below.
A=10 1010
B=11 1011
A&B 1011
C. Bitwise XOR operator: [^]
 The XOR operator is represented as ^ and is also called an Exclusive OR
operator.
 The OR operator returns 1, when any one of the two bits or both the bits are
1, whereas XOR returns 1 only ,when two inputs(bits) are unequal , otherwise
0.
 The truth table for the XOR operator is given below.
Input Output
(A’B+AB’)
A B F
0 0 0
0 1 1
1 0 1
1 1 0

 XOR operator is used to toggle a bit ON or OFF.


2. Bitwise Shift operators:
 There are two types of shift operators namely.
A. Left shift
B. Right shift
Prepared by Mr. Pankaj M. Zalera 7
Programing In C [SMT. V.B. NANDOLA COMPUTER SCIECNE COLLEGE, BHACHA]

A. Right shift [>>]:


 Right shift operator is represented by >> and it shifts each bit in the operand
to the right.
 Thus, Ch>>3 would shift all bits in ch three places to the right.
 For example, if the variable shift ch contains the bit pattern 11010111, then
ch>>1 would give 001101011 and ch>>2 would give 00110101.
 Note that as the bits are shifted to the right,blanks are oreanted on the
left.They are always filled with zeros.
B. Left Shift [<<] :
 This is similar to the right shift operator the only difference being that the bits
are shifted to the left and for each bit shifted, a 0 is added to the right of the
number.
 For example if the variable ch conatins the bit pattern 11010111 then ch<<1
would give 10101110 and ch<<2 would give 0101110.

3. Bitwise Complement operators [~]:


 There are given facility in the complement operator. Like One’s complement
operator.
 One’s Complement Operator[~] :
 On taking one’s complement of a number, all 1’s present in the number are
changed to 0’s and all 0’s are changed to 1’s.
 For example one’s complement of 1010 is 0101.
 One’s complement operator is represented by the symbol ~[tildi]
 For example
K=~j;

8. Special Operators:

 In ‘C’ some of the special operators are supported.


 There are mainly three special operators are available.
A. sizeof()
B. Indirection operator/direction operator
C. Comma operator.

A. sizeof():
 Return the size , in bytes of the given expression or type
 Syntax:-
1. sizeof<Expression>
2. sizeof(<type>)
 For example:
1. a=sizeof(int);
Output=2

2. b=sizeof(double);
Output=8

Prepared by Mr. Pankaj M. Zalera 8


Programing In C [SMT. V.B. NANDOLA COMPUTER SCIECNE COLLEGE, BHACHA]

B. Indirection/ Direction operators:


 There are some of the Direction and Indirection operators are included in C.
Indirection/Direction operators are also called Referencing/Dereferencing
operators.
 Referencing/ Dereferencing operators:
 The & and * operator work together for referencing and dereferencing.
 Referencing operator(&):
 It is a address of operator
 Ti is also e bitwise AND operator
 In the expression
& expression
scanf("%u”,&i);
 Dereferencing operator(*):
 The asterisk (*) in a variable expression, creates a pointer to a type or
as the multi-operator.
 It is a value of operator/ value at address.
 In the expression,
*expression;
char *s;
 Selection [Structure-Access] Operators:
 Direct Member Selector[.][dot Operator]:
 Syntax:-
Postfix-expression.identifier
 The postfix expression must be of normal type union or structure.
 The identifier must be the name of a member of that structure or union
type.
 Indirect member operator [->][ Arrow Operator] :
 Syntax:-
Postfix-expression-> identifier
 The postfix-expression must be of type pointer to structure or pointer to
union.
 The identifier must be the name of a member of that structure or union
type.
 For example:
void main()
{ struct student
{ int roll_no;
char name[20];
float per;
};
struct student r1;
struct student *r2;

printf(“Enter Roll_no,Name,Per”);
scanf(“%d%s%f”,&r1.roll_no,&r1.name,&r1.per);
Prepared by Mr. Pankaj M. Zalera 9
Programing In C [SMT. V.B. NANDOLA COMPUTER SCIECNE COLLEGE, BHACHA]

scanf(“%d%s%f”,&r2->roll_no,&r2->name,&r2->per);

printf(“%d%s%f”,r1.roll_no,r1.name,r1.per);
printf(“%d%s%f”,r2->roll_no,r2->name,r2->per);
getch();
}
C. Comma operator:
 The comma operator is used for separates the element of a function
argument/variable,list
 Syntax:
exp1,exp2,…..expN;
 For example:
f=(a=5,a+10,a+20);
Output: f=35
for(a=5,a=10,a=20;……;…….)
 Result in the left-to-right evaluation of each a, thus a is contain different
values in step by step.
Q.3. write a note on following:
1. printf():
 The printf() function is used to print out a message either on screen or on
paper.
 Syntax:
printf(“<control string>”,var1,var2);
-Or-
printf(“<format string>”,<list of variables>);

 The <control string/format string> of printf() function/statement


covers text message , escape sequence and %codes.
 Any variables in printf() is optional.
 The printf() statement in C should be ended with a semicolon.
 <format string >codes:
 The printf() statement is used to provide formatted output on the
terminal.The format codes plays important rule in generating formatted
output.The commonly used printf() format codes are listed here in below.

Code Meaning
%d Prints a decimal integer
%c Prints a single character
%f Prints a floating point value
%u Prints a unsigned decimal integer
%lu Prints a long unsigned decimal integer
%ld Prints a long decimal integer
%lf Prints a double floating point value
%s Prints a string

Prepared by Mr. Pankaj M. Zalera 10


Programing In C [SMT. V.B. NANDOLA COMPUTER SCIECNE COLLEGE, BHACHA]

 Following are some example of usage of printf() statement:


printf(“Principle=%d\nRate=%f\nYear=%d”,p,r,n);
printf(“\n Simple Interest=Rs. %f”,si);
2. scanf ():
 scanf() is used to input data through input stream like keyboard.
 This function stops program execution until a value is typed in and the
return key is pressed.
 The general format of scanf() :
scanf("control string”,&vari1,vari2,…);
Where control string contains the format of data being received and
specifies what type of data being stored into variable.
The & [Ampersand] before such variable names address
 Examples:
Printf(“enter value of p,r,n”);
Scanf(“%d%f%d”,&p,&r,&n);
 Commonly used scanf() format codes:

Code Meaning
%d Reads a decimal integer
%c Reads a single character
%f Reads a floating point value
%u Reads a unsigned decimal integer
%ld Reads a long decimal integer
%lf Reads a double floating point value
%s Reads a string
%lu Reads a long unsigned decimal integer

3. clrscr ():
 Used cleans text mode window
 Declaration:-
clrscr();
 Remarks:
clrscr() clears the current text window and places the cursor in
the window corner (at postion1,1).
 Return value: none.
4. getch() :
 getch() gets a character from console but does not echo to the screen
 Declaration:-
getch();
 Remarks:
getch() reads a single character directly from the keyboard, without echoing
to the screen .so this function return the character reads from the keyboard.

Prepared by Mr. Pankaj M. Zalera 11


Programing In C [SMT. V.B. NANDOLA COMPUTER SCIECNE COLLEGE, BHACHA]

Q.4 Write short not on storage class in C.


 Variables in ‘C’ can have not only data type but also storage class that
provides information about their location and versatility. So each variable
has its own class and data type.
 Generally we can write:
class + data type + variable_name;
 The storage class decides the portion of the program within which the
variables are recognized so, variables have certain default storage classes
 Storage class tells us:
1. Where the variable is stored.
2. What will be initial value of a variable without initialization?
3. What is the scope of variable; it means which function the value of
variable would be available.
4. What is the life of variable, it means how long would the variable exist.
 ‘C’ supports four storage class:
1. Automatic or auto storage class
2. Register or regist storage class
3. Static storage class
4. External or Extern storage class
1. Auto storage class:
 The features of a variable defined to have an Automatic storage class are
as given under:
Storage : memory [RAM]
Default value : Garbage
Scope : within main ()
 For example :
void main()
{
auto int i:
printf(“\n value of I %d”,i);
getch();
}
Output : -28752 (garbage)

 The fact that if the variable is not initialized, the auto storage class
contains a garbage value.

2. Register Storage Class :


 The features of variable defined to be of Register Storage Class are as
Under.

Storage value : CPU Registers


Default value : Garbage.
Scope : within main()

Prepared by Mr. Pankaj M. Zalera 12


Programing In C [SMT. V.B. NANDOLA COMPUTER SCIECNE COLLEGE, BHACHA]

 For example :
void main ()
{
register int i;
printf(“\n value of I %d “,i);
getch();
}
Output: 852
 A value stored in a CPU register can always be accessed faster than the one
which in stored in a memory.
 Therefore, if a variable is used at many places in a program it is better to
declared its storage class as register.
 CPU register size is allowed 8 or 16 bits, so with register keyword
float,long,double can’t be use.

3. Static Storage Class :


 The features of a variable defined to have a static storage class are as
under:
Storage : Memory [RAM]
Default value : 0 [zero]
Scope : within main ()
 For example :
void main()
{ static int i;
printf(“\n value of i %d”,i);
getch();
}
Output : 0

 Scope of the variable is restricted to a particular work in which it is defined.


 Value of the variable exists between different function calls.

4. External storage class:-


 The features of a variable whose storage class has been defined as
external are as follows:
Storage : Memory[RAM]
Default value : 0[zero]
Scope : Global, Outside main()
 For example:
int a;
void main()
{
printf(“\n value of a:%d”,a);
getch();
}
Output: 0
Prepared by Mr. Pankaj M. Zalera 13
Programing In C [SMT. V.B. NANDOLA COMPUTER SCIECNE COLLEGE, BHACHA]

 Here, the scope of external variable is global, no local external variables


are declared outside all functions [main ()].
Q.5 What is escape sequence? Explain it.
 C recognizes the following escape sequence when used with output function
like printf (), putc (), fprintf (), puts (), putchar () etc.
 The escape sequences help in formatting output.
 The backslash symbol (\) is considered an ‘escape’ character. It causes an
escape character is recognized as one having a special meaning.
Escape
Meaning
Sequence
\n New line
Tab-moves the cursor to next tab stop(10 tabs*8 columns=80
\t
columns
Backspace-moves the cursor one position to the left of its
\b
current position.
Carriage return \Enter - takes a cursor to the beginning of
\r
the line in which it is currently placed.
Alert-alerts the used by sounding the speaker inside the
\a
computer.
Form feed –it advances the computer stationery attached to
\f
the printer to the top of the next page.
\\ Backslash-print forward slash
\’ Single quote

\” Double quotes

Prepared by Mr. Pankaj M. Zalera 14

You might also like