2 Basics C
2 Basics C
PROGRAMMING
FUNDAMENTALS
Faiza Rasheed
1
The Task of Programming
2
The Task of Programming
3
Introduction
4
Structure of C++ programs
5
6
Structure of C++ programs
i. Preprocessor directives
ii. The main( ) Function
iii. C++ statement
7
Structure of C++ programs
1. Preprocessor Directives
8
Structure of C++ programs
Example 1-01
#include<iostream>
int main( )
{
cout <<"this is my first program";
return 0;
}
9
Structure of C++ programs
10
Structure of C++ programs
2. Header file
•C++ source file
•Contains definitions of function/objects
•Added into the program at the compilation of the
program.
•Added if the function/object defined in it is to be used in
the program
•"Include" is used to add "iostream“ into the program
11
Structure of C++ programs
Header file
•The Header file name is written in angle brackets (<>)
after “#include” directive.
•It can also be written on double quotes.
•C++ has large number of header files in which library
functions are defined.
•It must be included in the program before calling its
function in the program.
•A single header file may contain a large number of
built-in library functions.
12
Structure of C++ programs
Example 1-01
#include <iostream>
using namespace std;
int main ( )
{
cout <<"this is my first program";
return 0;
}
13
Structure of C++ programs
14
Structure of C++ programs
Example 1-01
#include<iostream>
#include<conio.h>
void main( )
{
cout <<"this is my first program";
getch( );
}
15
Structure of C++ programs
Example:
• “iostream”
• Has definitions of several built-in input and output
objects and functions
•Which was its object in the last example?
"cout" (see out)
•Syntax
◦ #include <name of the header file>
16
Structure of C++ programs
Example 1-01
#include<iostream>
int main( )
{
cout <<"this is my first program";
return 0;
}
17
Structure of C++ programs
19
Structure of C++ programs
C++ Statements
Synta
x
◦ int main ( )
{
program statements…
}
C++ Statements
21
Structure of C++ programs
Key
words
•Words used by languages for special purposes.
•Also called reserved words.
•Cannot be used as variable names in program
•For instance:
Main is used to indicate the starting of program
Include is used to add header files
Int to declare an integer type variable
22
Structure of C++ programs
Example 1-02
#include<iostream>
int main( )
{
int a, b;
}
23
Structure of C++ programs
Tokens
24
Structure of C++ programs
Example 1-02
main ( )
{
int a, b;
}
25
Data types in C++
Example 1-03
#include<iostream>
using namespace std;
int main ( )
{
int abc = 4, b = 1997;
float xy = 3.4;
char name[15] =
Marriam Ahmed;
cout <<name<<endl;
cout<<abc<<endl;
cout<<b<<endl;
cout<<xy<<endl;
return 0;
} 26
Data types in C++
27
28
Programming Universals
29
Variables
Not a constant
Ideally, variables have meaningful names, although no
programming language actually requires that they meet
this standard.
A variable may have only one value at a time, but it is
the ability of memory variables to change in value during
execution that makes computers and programming
worthwhile.
In many computer programming languages, including
C++, variables must be explicitly declared, or given a data
type as well as a name, before they can be used.
30
Variables
31
Variables
32
Structure of C++ programs
Types:
C++ has five basic data types.
i. int Integer 25, 100, 5000
ii. float Floating Point 3.4×105
iii. double double precision 3.4×105
iv. char characters almost all
v. bool Boolean logic type variables
35
Data types in C++
1. The int Data Type
Represents the integer type data.
Used to declare integer type variables.
Integer is a whole number. i.e. without fraction or decimal.
◦ 601, 250, -6, 501
The range of values that can be stored in int data type variables
depends upon the computer system being used.
◦ In MS-DOS, an integer type variable takes tow bytes in the
memory and range is -32768 to 32767 (1Byte = 8Bits)
Range can be changed by using integer qualifiers.
◦ short int
◦ long int
◦ unsigned int
36
Data types in C++
Integer qualifiers
i. The short int:
The storage capacity of a short int type variable is two
bytes(16bits, 216). It can store integer values from -32768 to
32767.
38
39