You are on page 1of 13

Fundamentals of Programming

CSC-110

Lecture: 02
(Introduction to Programming)

Engr.Muhammad Asad
MS Computer Software Engineering NUST
BS Computer System Engineering GIKI

1
Agenda for Today
 Intro. to Programming, Structure of C++, Preprocessor
Directives, Header Files
 Reading

2
Computer Languages
 High‑level Language Translators

compiler
source
or object program
program
interpreter

3
Phases of C++ Programs
Program is created in
Editor Disk the editor and stored
on disk.

1. Edit Preprocessor Preprocessor program


Disk
processes the code.
2. Preprocess Compiler creates
Compiler Disk object code and stores
3. Compile it on disk.

Linker links the object


4. Link Linker Disk code with the libraries,
creates an executable file
and stores it on disk
5. Load Primary
Memory
Loader
6. Execute Loader puts program
in memory.
Disk ..
..
..

Primary
Memory
CPU
  CPU takes each
instruction and
executes it, possibly
storing new data
..
values as the program
.. executes.
..
4
First program

#include <iostream>

int main()
{
std::cout << "Welcome to C++";

return 0;
}
5
6
Another Basic Step for Java
Programming
 Debugging
– Check program execution and output to ensure
program compiles and runs as expected
– If it doesn’t, make corrections in the edit
phase and repeat the remaining steps

7
Basics Input/output functions
 Common Input/output functions
– cin
 Standard input stream
 Normally keyboard
– cout
 Standard output stream
 Normally computer screen
– cerr
 Standard error stream
 Display error messages

8
Before writing the programs
– Comments
 Document programs
 Improve program readability

 Ignored by compiler

 Single-line comment

– Use C’s comment /* .. */ OR Begin with // or


– Preprocessor directives
 Processed by preprocessor before compiling
 Begin with #

9
Before writing the programs (Cont..)
 Header Files
 Header files contain
– Function prototypes
– Definitions of data types and constants
 Header files ending with .h
– Programmer-defined header files
#include “myheader.h”
 Library header files
#include <cmath>

10
Quick Example
// A first program in C++.
//Printing a Line of Text
#include <iostream>

// function main begins program execution


int main()
{
cout << "Welcome to C++!\n";

return 0; // indicate that program ended successfully

} // end function main

11
CSC-110
Explanation
 Standard output stream object
– std::cout
– “Connected” to screen
– <<
 Stream insertion operator
 Value to right (right operand) inserted into output
stream
 Namespace
– std:: specifies using name that belongs to
“namespace” std
– std:: removed through use of using
statements

12
Escape Sequences
Escape Sequence Description

\n Newline. Position the screen cursor to the


beginning of the next line.
\t Horizontal tab. Move the screen cursor to the next
tab stop.
\r Carriage return. Position the screen cursor to the
beginning of the current line; do not advance to the
next line.
\a Alert. Sound the system bell.
\\ Backslash. Used to print a backslash character.
\" Double quote. Used to print a double quote
character.

13

You might also like