0% found this document useful (0 votes)
13 views

2 Basics C

This document discusses key concepts in programming fundamentals including variables, data types, and the structure of C++ programs. It explains that programming involves writing instructions to enable computers to perform tasks. It also describes various programming languages and how compilers check programs for errors. The document outlines the typical structure of C++ programs which includes preprocessor directives, the main function, and C++ statements. It provides examples of simple C++ programs and describes variables, data types, tokens, and key words.

Uploaded by

Gul sher Baloch
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

2 Basics C

This document discusses key concepts in programming fundamentals including variables, data types, and the structure of C++ programs. It explains that programming involves writing instructions to enable computers to perform tasks. It also describes various programming languages and how compilers check programs for errors. The document outlines the typical structure of C++ programs which includes preprocessor directives, the main function, and C++ statements. It provides examples of simple C++ programs and describes variables, data types, tokens, and key words.

Uploaded by

Gul sher Baloch
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 39

CS-211

PROGRAMMING
FUNDAMENTALS

Faiza Rasheed

1
The Task of Programming

Programming a computer involves writing instructions


that enable a computer to carry out a single task or a group
of tasks.
A computer programming language requires learning
both vocabulary and syntax.
Programmers use many different programming
languages, including BASIC, Pascal, COBOL, RPG, and
C++.
The rules of any language make up its syntax.
Machine language is the language that computers can
understand; it consists of 1s and 0s.

2
The Task of Programming

A translator (called either a compiler or an interpreter)


checks your program for syntax errors.
A logical error occurs when you use a statement that,
although syntactically correct, doesn’t do what you
intended.
You run a program by issuing a command to execute
the program statements.
You test a program by using sample data to determine
whether the program results are correct.

3
Introduction

•C++ (See plus pluss)


•To follow syntax and rules of Turbo C++ and Borland
C+
•Text editors e.g. WordPad and note pad
•These are used to write the code
•Compilers are used to run the code
•Source code written and stored in first.cpp
•Compiled code stored/saved as first.obj
•Executed code stored as first.exe

4
Structure of C++ programs

The C++ compiler translates a C++ source program into the


machine code. The machine code is called object code. It is
stored in a new file with extension obj. The object code is
then linked to the libraries. After linking the object code to
the libraries, an executable file with extension exe is created.
The executable program is then from operating system
command line.
For example, a source code of program in C++ is written and
stored in file first.cpp. After compilation, the object code is
saved in file first.obj and the executable code is stored in
file first.exe.

5
6
Structure of C++ programs

Contains following three parts.

i. Preprocessor directives
ii. The main( ) Function
iii. C++ statement

7
Structure of C++ programs

1. Preprocessor Directives

•Instructions given before beginning of actual program


•Also called Compiler Directives
•To define certain actions or special instructions for the
compiler
•The compiler adds special instructions of code from these
directives into the program at the time of compilation.
•Normally begin with number sign (#)
•May include keywords like "include" or "define"

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

Example 1-01 (output)

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

Example 1-01 (output)

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

3. The main ( ) Function


•Indicates the beginning of a C++ program
•After the preprocessor directives
•Must be included in every program
•When a program is executed, control goes directly to
main() function.
 Syntax
◦ int main ( )
{
program statements…
}
18
Structure of C++ programs

The main ( ) Function

•The statements written within this function are the


main body of the C++ program.
•If main() is not included, the program is not compiled
and an error message is generated.

19
Structure of C++ programs

C++ Statements
Synta
x
◦ int main ( )
{
program statements…
}

The statements are written under main( ).


Between { }
These statements are the body of the
program.
Each statement ends with";"
20
Structure of C++ programs

C++ Statements

•C++ is a case sensitive language (so sensitivity must be


kept in mind always).
•Statements are usually written in lower case letters.
•In exceptional cases, can be written in upper case.

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

•A program statement consists of variable names,


keywords, constants, punctuation marks, operators etc.
•In C++, these elements of a statement are called tokens.

24
Structure of C++ programs

Example 1-02

main ( )
{
int a, b;
}

Main, {, }, int, a, b, and punctuation marks (,) and (;) are


tokens of the program.

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

Example 1-03 (output)

27
28
Programming Universals

All programming languages provide methods for


directing output to a desired object, such as a monitor
screen, printer or file.
Similarly, all programming languages provide
methods for sending input into the computer program so
that it can be manipulated.
In addition, all programming languages provide for
naming locations in computer memory.
These locations commonly are called variables.

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

The type determines what kind of values may be


stored in a variable.
Most computer languages allow at least two types:
one for numbers and one for characters.
 Numeric variables hold values like 13 or -6.
 Character variables hold values like ‘A’ or ‘&’.
Many languages include even more specialized types,
such as integer (for storing whole numbers) or floating
point (for storing numbers with decimal places).

31
Variables

Represented by a symbol or a name. Nadeem, fox pro, x,


y etc.
It represents a storage or memory location in a computer
memory.
Data is stored in memory location.
Data stored may change, the variable does not (the name
of the memory location, i.e. the variable name, remains
fixed during program execution).
Also known as object.
In C++, variable names consists of alphabets and digits.

32
Structure of C++ programs

Rules for writing a variable name


•First character must be alphabetic
character, exception "_”.
•No blank spaces.
•Special characters or arithmetic characters
not allowed #, ^, etc.
•Reserved words are not allowed.
•Maximum length depends upon the
compiler of C++.
•Variable name declared for one data-type
cannot be used to declare another data-type.
•Again, case sensitive language. 33
Structure of C++ programs

Variables Valid/Invalid Remarks


Nadeem valid
perform valid
double invalid C++ reserved word
foxpro valid
switch invalid C++ reserved word
Marriam valid
int invalid C++ reserved word
3taq invalid Starts with numeral
unsigned invalid C++ reserved word
Special character is not
x-y invalid allowed
Taq Ahd invalid Space is not allowed
34
Data types in C++

•The variable type specifies the type of data that can be


stored in it.
•Each variable is declared by its type.

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.

ii. The Long int:


The storage capacity of a short int type variable is four
bytes(32bits, 232). It can store integer values from -2147483648
to 2147483648.

iii. The Unsigned int:


It can store only positive whole numbers. Its storage capacity
is two bytes(216 = 65536). It can store integer values from 0 to
65,535. 37
Data types in C++

2. The float Data Type

Represents real or floating type data.


Real, decimal or exponential notation.
Float data type may be signed or unsigned. e.g. 23.35,
56.45, -98.34 etc.
The storage capacity for float type variable is four
bytes and it can store real values from 3.4 × 10-38 to 3.4×
10+38

38
39

You might also like