Tokens,Data Type,Operators,Condition,Loops
Tokens,Data Type,Operators,Condition,Loops
An escape sequence always starts with backslash followed by one or more special characters.
Global scope: visibility of a variable to all functions of a program Scope resolution operator in “::” .
This is used to access global variables if same variables are declared as local
-----------------------------------------------------------------------------------------------------------------------------------------
DATA TYPES:
A data type is used to indicate the type of data value stored in a variable.
int- store whole numbers. range of integer values is - 32,768 to +32,767. Occupies 2 bytes
float- store number with decimal. 6 digits precision occupies 4 bytes of memory
char- store characters. Occupies one byte
double-Double floating point data type occupies 8 bytes of memory giving 14 digits of precision.
void-used with function. void type has no values.
bool-Boolean or logical data type having two values (usually denoted true and false).
2.Derived data types- these are derived from fundamental data types
Array
Pointer
reference
structure
union
class
enumeration
-----------------------------------------------------------------------------------------------------------------
1.Sequential control statements - Sequential control statements ensures that the instructions(or
statements) are executed in the same order in which they appear in the program. i.e. By default system
executes the statements in the program in sequential order.
2.Conditional control statements –Statements that are executed when a condition is true.
1.Decision making statements - These statements are used to control the flow of execution of a
program by making a decision depending on a condition, hence they are named as decision
making statements. Decision making statements are of four types
1.Simple if
2.if else
3.nested if else
2.Switch case control statement – used for condition values. Only integer and character values
it can accept
2.do while loop statement – at least executed once. Exit controlled loop
# include Directive
The # include directive instructs the compiler to read and include another file in the current file.
The compiler compiles the entire code.
A header file may be included in one of two ways.
include <iostream.h> or include "iostream.h"
The header file in angle brackets means that file reside in standard include directory.
The header file in double quotes means that file reside in current directory.
C++ Tokens:
The smallest individual unit of the program is known as Token .
Keywords Words reserved by the language with a special meaning. Like int, float, switch,
case etc
Identifiers Long sequence of letters & digits to identify a memory block, program unit or
program objects. Like name, a, x, A, X, date, file1, file2 etc.
Literals Constants that never changes their value during the execution of a program.
Type : Integer Constant (Decimal ,Octal (preceded by 0) Hexadecimal –
(preceded by 0x or 0X)
• Character Constant („z‟, „A‟, „\a‟, „\t‟, „\‟‟, „0‟, „3‟ )
• Floating Constant (-13.0, -0.00065, 1.52E07, 0.172E-3)
• String Constants (“abc\0”, “Computer Science\0”)
Punctuators [ ] ( ) { } , ; : * = #
Operators <<, >>, +, -, *, /, % , ++, --, ==, <, >, <=, >=, !=, &&, ||, !
#include<iostream.h>
#define PI 3.14159 // This defines two new
constants: PI and NEWLINE. #define NEWLINE '\n' // Once they are defined,
they can be used
// in the code in place of their values
int main ()
{
double r=5.0; // radius
double circle;
circle = 2 * PI * r;
cout << circle;
cout << NEWLINE;
return 0;
}
Output :
31.4159
1. Illustrate the use of #define in C++ to define a macro.
Ans #define preprocessor directive is used to define symbolic constant in a program.
The #define identifier replacement is also known as a macro definition or simply a
macro macros may be define with zero or more arguments. A macro without argument
is used to define a symbolic constant and a macro with arguments is used to replace
expressions or code fragments in a program. A macro define is not terminated by
semicolon. Example:
#define LIMIT 10 //Symbolic constant
#define sqr(x) (x*x) //Macro
2. Explicit type conversion (Type Casting): Type casting operators allow us to convert
adatum of a given type to another. There are several ways to do this in C++. The simplest one,
which has been inherited from the C language, is to precede the expression to be converted by
the new type enclosed between parentheses (()):
int i;
float f = 3.14;
i = (int) f;
The previous code converts the float number 3.14 to an integer value (3), the remainder is lost.
Here, the typecasting operator was (int).
Question : What is the effect of absence of break in switch case statement in C++ ?
Answer :
The break keyword causes the entire switch statement to exit, and the control is passed to
statement following the switch.. case construct. Without break, the control passes to the
statements for the next case. The break statement is optional in switch..case construct.
Question : What is the difference between while and do-while loop in C++ ?
Answer : While is an Entry Controlled Loop, the body of the loop may not execute even once if the
test expression evaluates to be false the first time, whereas in do..while, the loop is executed at least
once whether the condition holds true the first time or not.
Question : What is the difference between call by value and call by reference in a user defined
function in C++?
Answer : The value of the actual parameters in the calling function do not get affected when the
arguments are passed using call by value method, since actual and formal parameters have different
memory locations. The values of the formal parameters affect the values of actual parameters in the
calling function, when the arguments are passed using call by reference method. This happens since
the formal parameters are not allocated any memory, but they refer to the memory locations of their
corresponding actual parameters
Question What is the difference between typecasting and automatic type conversion? Explain
with suitable example.
Ans:Type Casting: It is an user define explicit type conversion in which operand in an
expression is forced to be of a specific data type e.g. (type)expression
Automatic type conversion: In case of an expression in which operands different data type
are mixed an implicit type conversion is performed by the compiler which automatically converts
all operands upto the type of largest operands.
-------------------------------------------------------------------------------------------------------------------
sizeof( )operator:
This operator accepts one parameter, which can be either a type or a variable itself and returns
the size in bytes of that type or object:
a = sizeof (char);
This will assign the value 1 to „a‟ because char is a one-byte long type. The value returned by
sizeof is a constant, so it is always determined before program execution.
typedef keyword:
Using the keyword typedef, we can create an alias (a synonym) for existing fundamental or
compound datatypes in C++. Syntax:
typedef existing_type new_type_name ;
where existing_type is a C++ fundamental or compound type and new_type_name is the name
for the new type we are defining. For example:
typedef char C;
typedef unsigned int WORD;
typedef char * pChar;
typedef char field [50];
typedef does not create different types. It only creates synonyms of existing types. That
means that the type of myword can be considered to be either WORD or unsigned int, since
both are in fact the same type.
-------------------------------------------------------------------------------------------------------------
1 Differentiate between a Logical Error and Syntax Error. Also give suitable examples of
each in C++.
Ans Logical Error. A logical error is that error which causes a program to produce incorrect or
undesired output.
An incorrectly implemented algorithm or use of a variable before its initialization, or unmarked
end for a loop, or wrong parameters passed are causes logical errors. These must be handled
carefully.
For instance, if we are trying to print the table of a number 5 and if we say
counter=1;
while(counter>8)
{ cout<<n*counter;
counter=counter+1;
}
Here the loop would not be executed even once as the condition (counter>8) is not fulfilled at
all. Therefore, no output will be produced. Such an error is logical error.
Syntax Error: Syntax errors occur when rules of a programming languages (syntax) is misused.
Ie when a grammatical rule of C++ is violated.
Eg (i) c=a+b
In this statement, since there is no semicolon at the end of the statement, there will occurs a
syntax error.
(ii)cin<<a; In this statement, since stream insertion operator (<<) has given instead of stream
extraction operation(>>), there will occurs a syntax error.
2 What do you understand by Syntax Error, logical Error & Run time error.
Ans: Syntax Error: Those error that comes at the time of compilation are known as syntax error like
as statement terminator missing.
Logical Error: Logical errors occur due to wrong analysis of the program. Due to logical error
program does not give the desired result.
Runtime Error: Those errors that comes during execution are known as run-time error. Example:
1. Division by zero
2. Stack over flow
3. Array index out of bound
-------------------------------------------------------------------------------------------------------------------------------