You are on page 1of 20

Programming Fundamentals

Maryam Imtiaz Malik


maryam.imtiaz@numl.edu.pk
Keywords

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

 Data types in C/C++ refers to an extensive system  Basic Types


used for declaring variables or functions of
• They are arithmetic types and are
different types. The type of a variable determines
further classified into:
how much space it occupies in storage and how

this variable is used in some operations (using • Integer Type


mathematical and comparison operators).
• Floating-Point Types
Basic Data Types

S.No Data Type Storage Size Range


1 Char 1 Byte -128 to 127 or 0 to 255
2 Integer 4 Byte -32,768 to 32,767 or -
2,147,… to 2,147,..
3 Float 4 byte 1.2E-38 to 3.4E+38
4 Double 8 byte 2.3E-308 to 1.7E+308

Size of data types can be found by function sizeof();


cout<< sizeof(int); // 4
Different 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.

 int: As the name suggests, an int variable is used to store an integer.

 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.

 bool: it is a check variable, with values as ‘True’ and ‘False’.


Data Types

 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

 Set of all integers numbers (whole number, no decimal points)

 The operations: familiar mathematical and comparison operators.

 Most common allocation for int is four bytes


2. Floating Point 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

 Invalid Float values:


5,326.25 24 6,459 $10.29 7.007.645
3. Char Data Type

 Example of data type: char

 Used to store single character

 Letters of the alphabet (upper and lowercase)

 Digits 0 through 9

 Special symbols such as + $ . , - !

 Single character value: letter, digit, or special character enclosed in single quotes.

 Example
4. Bool Data Type

 Represents Boolean (logical) data

 Restricted to true or false values

 Often used when a program must examine a specific condition

 If condition is true, the program takes one action; if false, it takes another action

 Boolean data type uses an integer storage code


ASCII Codes

 A number code used by C++ to store characters internally (it’s easy for a computer to store numbers)

 Each character corresponds to a binary code

 Most commonly used binary code is ASCII (American Standard Code for Information Interchange)
Variable

 Named Memory Location


 A variable is simply a name the programmer assigns to refer to computer storage locations.
 The term “variable” is used because the value stored in the memory locations assigned to the
variable can change, or vary.
 For each name the programmer uses, the computer keeps track of the memory address
corresponding to that name.
Variable Declaration

 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:

 Syntax : type variable_list;

• 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++

 Can contain letters, digits and underscores.


 Digit cannot be the first character.
 Spaces are not allowed.
 May not be same as keyword or function name etc.
 First 40 characters are significant, i.e. Length can be of max. 40 characters, but varies from compiler to
compiler.
 Cannot consist of an underscore alone.
 It can’t contain blank spaces, commas, or special symbols, such as ( ) & , $ # . ! ?.
 Use initial uppercase letters to separate names consisting of multiple words.
Data Types
Practice Problem

 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)

You might also like