Professional Documents
Culture Documents
Keywords in C++ are also reserved words, which means they must be used only for their specified
purpose. Attempting to use them for any other purpose generates an error message.
Data Types
char: The most basic data type in C++. It stores a single character and requires a single byte of memory in almost
all compilers.
float: It is used to store decimal numbers (numbers with floating point value) with single precision.
double: It is used to store decimal numbers (numbers with floating point value) with double precision.
A Data type is defined as a set of values and operations that can be applied to particular data.
Numerical Data Types
Built-in-data type (primitive type)
Consist of basic numerical types
Majority of operations are symbols (e.g. +,-,*,…)
1. Integer Data Type
A floating-point number, more commonly known as a real number, can be the number 0
or any positive or negative number containing a decimal point.
The following are examples of floating point numbers:
+10.625 5.0 -6.2 3251.92 0.0 0.33 -6.67 +2
Digits 0 through 9
Single character value: letter, digit, or special character enclosed in single quotes.
Example
4. Bool Data Type
If condition is true, the program takes one action; if false, it takes another action
A number code used by C++ to store characters internally (it’s easy for a computer to store numbers)
Most commonly used binary code is ASCII (American Standard Code for Information Interchange)
Variable
A variable declaration tells the compiler where and how much storage to create for the variable.
A variable declaration specifies a data type and contains a list of one or more variables of that
type as follows:
• int i, j;
• char b, c;
• float f, salary;
• double d;
• The line int i, j, k; declares and defines the variables i, j, and k; which instruct the
compiler to create variables named i, j and k of type int.
Variable Declaration and Initialization
#include <iostream>
using namespace std;
int main ()
{
int a; // declaring integer a.
a = 5; // initializing integer.
float b = 2.3; // declaring and initializing float b.
char x, y; // declaring two characters x and y.
x=‘A’, y=‘B’; //initializing two characters.
cout<<“Values = “ << a << “, “<< b<<“, “<< x<< “, “<<y;
return 0;
}
Naming Rules in C++
Write a program to assign values to different variables at the time of declaration. Print the
assigned values on the computer screen.
Write a C++ program that declares a variable, stores fees and display the student fees.
References
• Gary j. Bronson, A first book of C++, Garry Bronson, 4th edition (Chapter 2)