Chapter - 2 Cp1 (Notion)
Chapter - 2 Cp1 (Notion)
bool: Represents boolean values, either true or false . Example: bool flag =
true;
2. Integer Modifiers:
The int data type can be modified to handle different ranges of values:
int: Standard integer (can store both positive and negative whole numbers).
unsigned int: Can store only positive whole numbers (without negative
values).
short: A smaller integer, typically using less memory than int (can store a
smaller range of values).
3. Memory Considerations:
Different data types consume different amounts of memory. For example:
The correct number of bytes must be accessed for accurate data processing.
By understanding the proper data types and their modifiers, you can ensure your
program stores and processes data correctly.
Variations:
short: This is a smaller integer data type, typically using 2 bytes. The
range is from 32,767 to +32,767.
unsigned short: This is a version of short that can store only positive
numbers, ranging from 0 to 65,535.
Key Points:
short is used when the range of numbers is small (e.g., -32,767 to +32,767).
unsigned short is used when only positive numbers are needed and when you
want to store a larger range (e.g., 0 to 65,535).
long is used when a larger range is required, like when working with large
values.
float:
double:
Key Points:
float is good for rough approximations or when memory is a concern, as it
uses less memory.
double is used when you need more precision and accuracy in floating-point
calculations.
Note: Even though float is used for calculations, internally, all floating-point
numbers are treated as double during operations.
Summary
Use int for counting whole numbers.
Use float for approximations with decimals and when memory is limited.
Use double for higher precision and more accurate floating-point calculations.
Use short and long when you need to limit or expand the range of integers.
Choosing the right data type ensures better memory usage and accuracy in your
programs.
The char type is used to store a single character. This can be any letter,
number, or special character (such as a , 1 , @ , etc.).
Range: The char type typically holds a 1-byte value, which can represent an
ASCII value. It can store values from 127 to +127 (or 0 to 255 in the unsigned
version).
2. Character Strings:
Character constants like 'A' refer to individual characters, while strings like
"A" refer to a collection of characters.
#include<iostream.h>
main()
{
// Declare and initialize char variables
char ltr_1 = 'C', ltr_2 = ' ', ltr_3 = 'i', ltr_4 = 's',
ltr_5 = ' ',
ltr_6 = 'g', ltr_7 = 'r', ltr_8 = 'e', ltr_9 = 'a',
ltr_10 = 't', ltr_11 = '!';
Output:
C is great!
In this example, the program uses separate char variables to store individual
characters of the string "C is great!". The characters are displayed in sequence to
form the sentence.
void main()
{
// Define individual characters for the string "I Love ET
HIOPIA!"
char charvar1 = 'I'; // 'I'
char charvar2 = ' '; // space
char charvar3 = 'L'; // 'L'
char charvar4 = 'o'; // 'o'
char charvar5 = 'v'; // 'v'
char charvar6 = 'e'; // 'e'
char charvar7 = ' '; // space
char charvar8 = 'E'; // 'E'
char charvar9 = 'T'; // 'T'
char charvar10 = 'H'; // 'H'
char charvar11 = 'I'; // 'I'
char charvar12 = 'O'; // 'O'
char charvar13 = 'P'; // 'P'
char charvar14 = 'I'; // 'I'
char charvar15 = 'A'; // 'A'
char charvar16 = '!'; // '!'
Output:
This example shows how to store each character in its own char variable and then
display them to form a sentence.
void main()
{
char charvar1 = 'A'; // Define Char Variable as Character
char charvar2 = '\t'; // Define Char Variable as tab (hor
izontal tab)
Output:
A B
The output shows the character 'A', followed by a tab (produced by \t ), and then
the character 'B' after the tab space.
Key Takeaways:
char is used to store a single character.
You can use special characters like tabs ( \t ) and newlines ( \n ) with char .
EXERCISE
Here is a C++ program that uses char data type and character strings to
display the required output:
void main()
{
// Output for "A) C D E F"
char letter1 = 'C';
char letter2 = 'D';
char letter3 = 'E';
char letter4 = 'F';
cout << letter1 << " " << letter2 << " " << letter3 <<
" " << letter4 << endl; // Print with spaces and move to
the next line
cout << part1 << part2 << part3 << part4 << part5 << p
art6 << part7 << part8
<< part9 << part10 << part11 << part12 << part13
<< part14 << part15 << endl;
cout << letter5 << " " << letter6 << " " << letter7 <<
" " << letter8 << " "
<< letter9 << " " << letter10 << " " << letter11
<< endl;
}
Output:
P R O G R A M
Explanation:
1. Part A: We use individual char variables for each character ( C , D , E , F ),
and print them with spaces in between using cout .
2. Part B: For the sentence, each word is stored in a separate char array
(character string), and we concatenate them using cout to form the full
sentence.
The program leaves a blank line between each output part as requested.
#include<iostream.h>
#include<conio.h>
int main() {
clrscr(); // Clears the screen
int b, n;
b = 10;
cout << "Enter the number n: ";
cin >> n;
if (b == n) {
cout << "This is executed." << endl;
} else {
cout << "This is not executed." << endl;
}
return 0;
}
Explanation:
Relational Operators: In the program, the conditions b == n , b > n , and b < n
b == n checks if b is equal to n .
Output Example:
Important Points:
Storage of Boolean Values: Although bool only has two logical states ( true
and false ), it is typically stored using 1 byte of memory, where false is
represented by 0 and true by 1 .
Convenience: Using the bool data type makes your code more readable and
clear when working with logical expressions, rather than using int or other
numeric types to represent truth values.
Size of Integers
The sizeof() operator in C++ is used to determine the size (in bytes) of a data
type or variable on your system. Different systems might have different sizes for
data types, but the sizeof() operator provides the exact size based on the system
you're running your program on.
Here’s a breakdown of how the sizeof() operator works in the example program
you provided:
#include <iostream.h>
#include <conio.h>
int main() {
clrscr (); // Clears the screen
return 0;
}
Explanation of Output:
sizeof(int) : The size of an int can vary depending on the system. On many
modern systems, it is 4 bytes, but it can also be 2 bytes on older or less
common systems.
Key Points:
Consistency on One Machine: The size of a data type is consistent on a given
machine. Once you determine the size, it doesn’t change during program
execution.
System Dependency: The actual size of a data type may differ from one
machine to another, so using sizeof() helps determine the exact size for the
current system.
char size: char is always 1 byte in C++, even though its value can be positive
or negative, based on the system’s representation.
This program allows you to check and understand how much memory each data
type takes up on the system you're working on.
Constants (Literals)
In C++, constants (also known as literals) are used to store values that remain
unchanged throughout the execution of a program. These constants can either be
literal constants (directly used values in the program) or symbolic constants
(named constants using the const keyword). Here's a detailed explanation:
1. Literal Constants
A literal constant is a fixed value used directly in the program, like 3.14 , 100 , or
'A' . These are simply written in the code and represent a fixed value without the
2. Symbolic Constants
A symbolic constant is defined with a specific name and can be used throughout
the program. Using the const keyword, symbolic constants cannot be changed
after they are assigned.
Example:
The const keyword ensures that the value of MAX cannot be modified once it is
initialized.
#include<iostream.h>
main() {
const int MAX = 100; // Define a symbolic constant MAX
cout << "MAX = " << MAX << endl;
int z, y;
z = (MAX) * 2; // Using the constant MAX in calculations
cout << "Z = " << z << endl;
Output Example:
MAX = 100
Z = 200
Y = 310
Key Points:
Literal Constants: Fixed values directly used in expressions or assignments,
such as 3.14159 or 100 .
Improves Readability and Safety: Using symbolic constants makes the code
more readable and avoids mistakes caused by changing values accidentally.
By using constants, programs become easier to maintain, more readable, and less
error-prone.
#include<iostream.h>
main() {
cout << "He said, \"This is a quote\".\n"; // Use \" to p
rint quotes inside a string
cout << "\"This displays on one line, and this on the nex
t\".\n"; // Newline after text
cout << "\"The new line Character is \".\n"; // Newline w
ith explanation
cout << "\"She said \"I Love\" in reply to Hirut’s Questi
on.\""; // Escaping double quotes inside a string
}
2. Second line: The string \"This displays on one line, and this on the next\".\n uses
the escape sequence for double quotes and also creates a new line at the end
with \n .
Key Points:
Escape Characters are used to represent characters that can't be typed
directly in a string (e.g., quotes, backslashes, new lines).
\"and \’ allow you to use double and single quotes inside strings,
respectively.
By using these escape characters, you can format strings and include special
characters in your output.
Mutable Nature: Unlike constants (which have fixed values), variables are
designed to hold values that can change over time.
data_type variable_name;
Example:
3. Variable Initialization:
Initialization is the process of giving a variable an initial value when it is declared.
For example:
4. Naming Variables:
Variable names in C++ must follow specific rules:
5. Variable Scope:
Variables have different scopes, which define where they can be accessed in the
program.
Global Scope: A variable declared outside any function can be accessed from
anywhere in the program.
#include <iostream.h>
main() {
int localVar = 50; // Local variable
cout << "Global Variable: " << globalVar << endl;
cout << "Local Variable: " << localVar << endl;
}
Global variables can be used in any function that appears after their
declaration.
Local variables are only accessible within the block where they are defined.
#include<iostream.h>
main() {
Output:
65
90
Garbage Value: If a variable is not initialized, it holds a random value that can
lead to errors.
Best Practice: Always initialize your variables, and give them meaningful
names to improve readability.