0% found this document useful (0 votes)
22 views

Hsslive-xi-cs-siraj-06 Chapter-CS

Chapter 6 covers data types and operators in C++. It explains fundamental, derived, and user-defined data types, along with type modifiers, variables, and various operators including arithmetic, relational, logical, and assignment operators. The chapter also discusses expressions, type conversion, and the structure of a C++ program, highlighting the importance of comments and variable initialization.

Uploaded by

haasinijv2008
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)
22 views

Hsslive-xi-cs-siraj-06 Chapter-CS

Chapter 6 covers data types and operators in C++. It explains fundamental, derived, and user-defined data types, along with type modifiers, variables, and various operators including arithmetic, relational, logical, and assignment operators. The chapter also discusses expressions, type conversion, and the structure of a C++ program, highlighting the importance of comments and variable initialization.

Uploaded by

haasinijv2008
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/ 4

Chapter 6 : Data types and Operators.

Data types: They are the means to identify the nature of the data and the set of operations that can be
performed on the data. Data types are necessary to declare variables. There are three types of data types.
(A) Fundamental data types (Built-in data types ): They are defined in the C++ compiler. They are
( i ). Int data type. Whole numbers without fractional part. In GCC, it uses 4 bytes of memory and can store
whole numbers from -2147483648 to 2147483647 (232 = 2147483647 ).
(ii) char data type: They are symbols covered by the character set of C++ language. It uses only one byte of
memory. (storing in memory with its ASCII value).
( iii) float data type: They are numbers with fractional part. It uses 4 bytes of memory.
(iv) double data type : It is used for handling large floating point numbers with high precision. It uses 8 bytes
of memory. ( v) void data type: it uses zero bytes (null) of memory. If a function does not return a value then
the type should be void.
(B) Derived data types: They are derived from fundamental data types by some grouping or alteration in
size. Eg. Array, pointer etc.
(C) user defined data types. The programmer can define their own data types. Eg. struct, enum, union ,
class etc.
Type modifiers : These are used along with the basic data types to alter the size, range and precision of the
variable. Important type modifiers are signed, unsigned, long and short.
Variables: The name given to a memory location is known as variable. It is use to
101 102 103 104
store and retrieve data.
15
It have three part: a) variable name b). Memory address(starting address of the allocated
memory – also called L-value [ Location value] )and Content (the value stored in that int n
location – also called R – value [ read value ] ). In the figure 'n' is a variable name with
int data type. 101, 102 etc are the memory address. The content of the variable is 15. R- value is 15 and
L-value is 101.
Operators: They are symbols that trigger some kind of operation.Based on number of operands, operators
are classified into
(a) Unary operators - one operands( positive, negative, increment [ ++], decrement [ - - ] ).
(b). Binary operators: ( two operands- arithmetic, relational etc.)
(c). Ternary operator ( conditional operators – three operands ? : ).
According to types of operation, there are
(a) Arithmetic operators : + , - , * , /, % ( modulus operator – getting remainder after integer
division . Eg. 5%2=1)
(b)Relational Operator : Here we can compare numeric data and the result will be either TRUE or FALSE.
< (less than), <= (less than or equal to ), > ( greater than ), >= (greater than or equal to ),== ( equal to ), !=
( not equal to )
(c)Logical operators : the operators are && (logical and), || (logical or ), ! (logical not). Here we can
relational expressions. The result will be either TRUE (1) or FALSE (0).
Eg. (5>2) && (3 < 4) evaluates to 1. (7<9) || (5> 8) evaluates to 1 . ! ( 5<6) evaluates to 0.
(d) Assignment operator: The assignment operator is ' = ' . It is used to store a value in a memory
location.
Eg. X=5 ( the constant 5 is stored in variable X ).
(e) Input/ Output operator : The standard input device is the keyboard and the out put device is monitor. In
the input operation, the data from the keyboard is stored in a memory location. For this C++ provides the
operator ' >> ' ( get from or extraction operator. ). In output operation, the data is transferred from RAM to
monitor. The operator ' << ' (put to or insertion operator ) is used.
(f)Arithmetic assignment operator(short hand): The operators are +=, – = , * = , /=, %= . a+=10 means
a=a+10, b - =15 means b= b – 15, c =5 means c=c * 5 and so on.
(g) Increment (++ ) /decrement ( - - ) operator: a++ (post increment ) means a=a+1. ++a (pre
increment ) means a=a+1; b- - (post decrement ) means b= b – 1 and - -b ( pre decrement ) means
b = b – 1.
There are two versions of these operators.
Prefix form : The rule is change, then use. Eg. a= b = 10, c=++a, then the value of the variable c will be
11and a =11 ( Here the value of a is incremented by one at first and then the changed value of a is assigned to
c . ) . Let d = - -b, we get d =9 and b = 9 .
Post fix form: The rule is use, then change.
Eg. a= b = 10, c=a++, then the value of the variable c will be 10 and a =11. ( Here the value of a
is assigned to c first and then the value of a is incremented by one. That is a = b++ means a= b, b= b++) .
Let d = - -b, we get d = 10 and b = 9.
( h) Conditional Operator ( ? : ) : Here there are three operands.
Eg. Result = (mark > 30 ) ? “Passed” : “Failed” ; if the mark is greater than 30, then the result is
passed else result is failed.
( i ) sizeof operator: This operator returns the amount of memory space in bytes allocated for the operand.
The operand can be a constant, a variable or a data type.
Eg. Sizeof(int); (which is 4), sizeof (4.5); sizeof(a); etc. ( j ).
Precedence of operators: It is the order of priority given to the operators. It begins from parenthesis ( ) ,
unary operators, increment , decrement, multiplication, division, modulus, addition, subtraction, relational
operators, assignment, logical and lastly comma operator.
Eg. 7 +3 - ( 4 – 2 ) + 3 *2 - 8 = 10 – 2 + 6 - 8 = 6.
Expressions: They are composed of operators and operands. All expressions can be evaluated to get a value.
This result is known as the value returned by the expression.
They are divided into
(a) arithmetic expressions: here arithmetic operators are used. They are again classified as
(i) integer expressions: The values given will be integer only.
Eg. Let a=8, b = 3, c= a%b; gives c = 2.
(ii) Floating point expression) : Here modulus operator cannot be used.
Eg. a=5.0, b=2.0, c=a/5, then c= 2.5.
( b) Relational expression: here relational operators are used and produces boolean type results like true (1)
or false ( 0 ).
Eg a = 5, b= 2, (2*b > a ) will be 0(false ) .
(c) Logical expression: one or more relational expressions are combine with logical operator and produces
either true or false.
Eg a=5, b = 3 . ( a = b ) || ( a > b ) will be 1(True) .
Type conversion : The data type of one operand can be converted into another and it is called type
conversion. There are two ways
( a)implicit type conversion(Type Promotion) : In expression where different types of operands involved,
C++ compiler convert the lower sized operand to higher sized operand internally. . So it is known as type
promotion.
Eg. (5/2)*3+2.5 = 8.5, (5.0/2)*3+2.5=10.
(b)Explicit type conversion: Here the programmer may decide the data type of the result. Type casting is
applied on variables. Eg. Float a=5.0, b=2.0, c ; c=(int)(a/b); the output will be of he type int.
Statement : They are the smallest executable unit of a programming language. It uses a semi column ( ; ) as
delimiter. Executable statements are the instruction to the computer. There are mainly four types of
statements.
(a) Declaration Statement: It is used to declare the type of the variable or constant used in the program.
Each variable should be declared before its use.
Eg, int a, b; float avg;
Giving value to a variable at the time of its declaration is called variable initialization.
Eg. int a=5; float p=1000, r=6.75; If the variable initialized during the execution of the program is
known as dynamic initialization.
Eg. Int i=p*n*r/100; To make value of a variable remains unaltered through out a program, we should the
key word const . const is known as the access modifier. It is used to create constants whose value can
never be changed during execution. Eg const float pi=3.14;
( b)Assignment statement: It is used to assign a value to a variable
Eg. A=10; b= c+d;
(c)Input statement : It allows the user to store data in the memory (RAM) during the program execution. In
C++ get from operator(>>) is used for it. Before this an object ' cin' should be used.The operand after the >>
operator should be a variable.
Eg cin >> number;
(d)output statement : It allows the user to make available the results through any output device. In C++ put
to operator(<<)is used for it. Before this an object named ' cout ' should be used.
Eg cout>>sum;
Cascading I/O operators. : Multiple use of Input / Output operators in a single statement is called
cascading of I/O Operator. Here the values are assigned to the variables from left to right.
Eg. cin>>a>>b>>c; cout<<a<<b<<c;
Structure of a C++ program: Pre-processors are compiler directive statements #include<iostream>
which direct the compiler to process the information provided before actual using namespace std;
compilation starts. It is used to link the header files to the source program. They are int main( )
lines starts with #include in the program. {
........... ;
Eg. #include, #define, etc. They are not C++ statements. Functions are the body of the
set of instructions to perform a particular task referred to by a name. program;
The essential function in a C++ program is the main( ) function. The execution starts ............. ;
at main function and and end within main( ) function. return 0;
The header file contain information about functions, objects and predefined derived }
data types that are available along with the compiler. If we want to use cin, cout we
have to use the statement '#include<iostream> at the beginning.
The namespace is designed to overcome the difficulty to differentiate similar functions, variables etc., with
the same names in different libraries. In C++ ' std ' is the an short form of standard. If we do not use a
namespace in the program, we have to use the format std::cout , std::cin, std::endl. Etc. ' iostream ' is
an header file which is included in this program to use cin, cout in the statement.
The ' return ' statement stops the execution of the main( ) function.
Comments : It is used to document the program internally. They are added to describe the program. They
are ignored by the compiler.
They are two types.
(a)Single line comment: The two slashes ( // ) is used to write single line comment.
(b)Multi line comments: It write within / * and */.
Garbage value of a variable is the unpredictable value assigned to a variable at the time of declaration. When
we give names to the variables, such names can help us to remember the quantity they possess. Eg.
roll_num, emp_code, mark1, etc. These kind of identifiers are called mnemonic names.

Questions: // program without using namespace std.


#include<iostream>
1. Predict the out put of the following C++ statements ? Int a = -5, int main( )
b = 3, c = 4 ; c + = a++ + - - b; cout << a<< b <<c; {
2. What is dynamic initialization of variables? int cout=10;
std::cout<<"Value of cout is "<<cout;
3. What is the difference between ( a) p = 5; ( b) p = = 5 ? }
4. What would happen if main( ) is not present in a program?
5. What is preprocessor directive statement?Explain with an
example?
6. What is the difference between num+1 and num++1 in C++
7. What is type casting ?
8. Write C++ examples for the following? a).Declaration statement b).
Assignment statement c) Type casting Name of operator Symbol
9. Raju wants to add value 1 to the variable ' p ' and store the value in ' p ' a). modulus 1. + +
itself. Write four different statements in C++ to do this task? b). logical 2. = =
10. Match the following. c). relational 3. =
d). assignment 4. ? :
11. Explain the two methods of providing comments in the C++ program ? e). increment 5. &&
12. What do you mean by type conversion? Explain the two methods of type f). conditional 6. %
conversion?
13. What are data types? Write a short note on data types in C++?
14. Which are the relational operators in in C++? Explain with suitable examples?
15. What will be the result of a = 5/3 if a is ( i ) float (b) int ?
16. What is the difference between the tokens 0, ' \0 ' , ' 0 ' , “0 “ ?
17. Name the header file to be included to use cin and cout in programs
18. Briefly explain the three components in the structure of a C++ program? (3)
19. Some of the literals in C++ are given below. How do they differ? ( 5, '5', 5.0, “5” )
20. Comments are useful to enhance readability and understandability of a program. Justify this statement
with examples?
21. Write value returned by the following C++ expressions if x = 10 and y = 20.
a). x>15 || y>15 b). x>15 && y>15 c). ! (x > y ) (3)
22. Which among the following is equivalent to the statement series b=a, a=a+1 ?
[ a). b+= a; b). b = a++; c). b = ++a; d). b + = a+b; ]
23. Pick the odd one out from the following and give reason:
a). signed int; b). char ch = 2.5; c). long int; d). unsigned int;
24. Identify and write the errors in the following variable declaration statements:
a). INT num; b). char ch = 2.5; c). signed float x ; d). long double ;
25. Observe the following data and fill up the following table using these data:
256, 3.14, 25000, '9', 50000, 0, '\n' , '+' , 24.9875

int char unsigned float long

26. Observe the C++ expressions given below and answer the following questions: a). 15/10+5 b).
(2.5 + 3 ) % 4 c). 12 % 5 - 5 / 2 d). 7 / 7 % 7 ( i ) Some of the above
expressions are invalid. Identify them and state reason for the invalidity. ( ii ). Identify the valid
expressions and find the output of each.
27. Choose the correct output from the following expression: 12 / 10 + 12 % 10 ;
28. Write C++ programs for the following : a). To display a message ' protect school and public
properties'. ( b). To convert temperature from Celsius to Fahrenheit ( c ). To find the
ASCII value of a program ( d). To convert time in seconds to hr:min:sec format. ( e ) . To
convert measures in meters & centimetres into feet and inches.
29. Identify the errors in the following program and write reason for each: #include iostream
30. Observe the following code segment: void main( )
What are the values of a, b, p and q after the execution of these int a =5, b=10, p, q; {
p = a++ + - -b; int a,b ;
statement What will be the result q = ++a – b- -; cin << a , b;
31. Name any two preprocessor directives in C++. s= a +b;
32. Classify data types used in C++ (2) cout << s
33. What is the role of relational operator in C++? Give suitable examples. (3) }

You might also like