2a Fop

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 24

Fundamentals of Programming

Lecture 2
Agenda
❑Basic C++ Program
❑Data Types and Memory Concepts
❑Arithmetic operations
❑Decision making : Equality and Relational Operators

2
Solution

3
Basics of C++ Program
Comments:
• ignored by computer
• multiline comment /* … */
• single line comment //

4
Basics of C++ Program (Preprocessor Directives)
#include <iostream>
• Preprocessor directive
• Tells computer to load contents of a certain file
• <iostream> allows standard input output operations

The instructors that are given to the compiler before the beginning of
the actual program are called preprocessor directives or compiler
directives

5
Basics of C++ Program (Header Files)
❑C++ source file containing definitions of library functions
❑Added in a program
❑File name written in angle brackets < > or double quotes “ ”
❑Example: #include<iostream>, #include<fstream>

6
Basics of C++ Program (main)
int main()
◦ Only one main program
◦ Parenthesis used to indicate a function
◦ Start and end with a curly bracket {…}
◦ main block
◦ int means that main "returns" an integer value
◦ Body of main contains code written within the brackets

7
Basics of C++ Program (main)
• Beginning of a C++ program
• Every C++ program has a main function
• main function is a unique function
• When program executes control transfers to main function
• Program is not compiled without a main function
Syntax:
int main()
{
//Program statements
}

8
Basics of C++ Program (C++ Statement)
cout << “Hello World”<<endl;
◦ Entire line called a statement
◦ All statements must end with a semicolon (;)
◦ Associated with ostream
◦ << insertion operator or put to operator
◦ “”print whatever is written in the inverted commas
◦ cout << “Hello World\n”;
◦ \n is the newline character

9
Basics of C++ Program (C++ Statement)
❑C++ statements are written under the main() function between
curly braces
❑C++ statements end with a semicolon ;
❑C++ language is case sensitive
❑C++ language written in lower case

10
Basics of C++ Program
Right brace }
◦ Indicates end of main

Linker
◦ When a function is called, linker locates it in the library
◦ Inserts it into object program
◦ If function name is misspelled, the linker will produce an error
because it will not be able to find function in the library

11
Variables & Data Types

12
Variable
❑A quantity whose value may change during execution of the
program is called variable.
❑Represents storage or memory location
❑variables are implemented as memory locations and assigned
certain memory address. The exact address depends on computer
and compiler.
❑Variable name remains fix during program execution but data
stored in that location may change
❑Variable name consists of alphabets and digits

13
Rules for Variable Names
❑First character of variable must be an alphabetic character
❑Underscores are allowed
❑Underscore can be used as first character
❑Blank spaces are not allowed
❑Special characters e.g +, #, @ are not allowed
❑Reserved words are not allowed e.g int main
❑Variable name declared for one data type cannot be used for another
data type
❑C++ is case sensitive therefore, X and x are considered different variables

14
Data Types
❑A data type is a classification that specifies the type of value a
variable can hold
❑General syntax of declaration of data type is
❑data_type variable_name;
❑Example : int a;

15
Data Types in
C++

User Defined Built-in Data Derived Data


Data Types Types Types

16
Data Types in
C++

Built-in Data
Types

int,
char,
float,
bool,
void,
wide char

17
Data Types
❑int Integer
❑float Floating Point
❑double Double Precision
❑char Character
❑bool Boolean

18
Type Size in Bytes

int 16 bits (2 bytes)


short int 16 bits (2 bytes)
long int 32 bits (4 bytes)
float 32 bits (4 bytes)
long float 64 bits (8 bytes)
double 64 bits (8 bytes)
long double 80 bits (10 bytes)
char 8 bits (1 byte)

19
Code Example (int)

20
Output

21
Code Example (float)

22
Output

23
Books Used to Create Slides

24

You might also like