CSC-113
Computer Programming
MOBILE ALERT
Kindly
Switch Off
your Mobile/Cell Phone
OR
Switch it to Silent Mode Please
COURSE ASSESSMENT
Home/Assignment --- 20%
Quizzes --- 10%
Mid Term --- 20%
Lab --- 50%
SITE ADDRESS
FOR LECTURE NOTES AND STUDY MATERIAL DOWNLOAD, PLEASE VISIT :
https://sites.google.com/site/sumeerahashmics
Text Book
Computer Science
A Structured Approach : Using C++
Lecture Outline
• Structure of C++ Program C++
• Variables
• Escape Sequences
• I/O Statements
• Operators
• Precedence of Operators
Writing, Editing,
Compiling and Linking
1. Editor to create a program. (source program)2. Compiler translates in to machine language.
Writing, Editing,
Structure of a C++
Program
Pre-compiler directive
Pre-compiler directive
Opening brace
Hello World Program
Without namespace Without namespace
Namespace std contains all the classes, objects and
functions of the standard C++ library.
Namespace std contains all the classes, objects and
functions of the standard C++ library.
#include <iostream> int main () {
std::cout << "Hello world!\n"; return 0;
Preprocessor Directives
#include <iostream>
“I want to use a predefined library
called iostream”
Always start with a ‘#’
Directives
using namespace std;
•“I want to use objects in a name group
‘std’ ”
•Tells the compiler where to look for names
in the library
•Can deal with the situation where two or
Main Function
int main()
•The main body of the program.
•Compiler first tries to locate
“main()” to find where to begin the program
Comments
Start of comment
End of comment Start of comment
End of comment
• Internal program document
Variables in C++
• Named memory locations that have a type
• Named: identifier
• Type: needs declaration
• What you can do with variables
Variables and
Identifiers
Memory
Address of memory:
Hard to remember
Variables and Identifiers
Memory
studentID
studentGrade1
studentGrade2
Variables and Identifiers
Memory
studentID
studentGrade
studentName
Naming
Identifiers
• Allowed characters: A-Z, a-z, 0-9, _ (underscore)
• Not allowed to start with a digit. E.g., 3class (x),
class3(o)
• The identifier cannot duplicate a reserved word.
e.g., if, case, while…
• Good names descriptive but short
Integer and Floating Point
Types
2 or 4 Bytes
4 Bytes 2 Bytes
8 Bytes
Maximum/Minimum of
Integer Value Type
Type Sign Byte Minimum value Maximum value
short int/short signed 2 -32,768 32,767
unsigned 0 65,535
int (PC) signed 2 -32,768 32,767
unsigned 0 65,535
int (Mainframe) signed 4 -2,147,483,648 2,147,483,647
unsigned 0 4,294,967,295
long int/long signed 4 -2,147,483,648 2,147,483,647
Variable Initialization
• Variable declaration ≠ variable initialization
• Should be initialized by a programmer before it is
used e.g.,
int count;
Constants
• Data values that cannot be changed during program
execution
• E.g.,
• 3.141592 • ‘d’
Escape Sequence
• Escape sequences are used to represent certain special characters
within string literals and character literals.
Escape
sequence Description
\' single quote
\" double quote
\? question mark
\\ backslash
\a audible bell
\b backspace
\f form feed - new page
\n line feed - new line
\r carriage return
\t horizontal tab
Standard streams
More about
cout
• width(int) function sets the width for printing a value
• Only works until the next insertion command comes
int x = 42;
cout.width(5);
More about
cout
• fill(char) function sets the fill character.
• The character remains as the fill character until set
again.
int x = 42;
cout.width(5); cout.fill(‘*’);
More about
cout
• precision (int) sets the number of significant digits
of float type numbers
float y = 23.1415; cout.precision(1);
cout << y << '\n'; // Outputs 2e+01 cout.precision(2);
More about
cout
• Output Manipulators (not a function)
endl - outputs a new line character, flushes output
dec - sets int output to decimal
hex - sets int output to hexadecimal
oct - sets int output to octal
#include <iomanip.h>
int x = 42;