INTRODUCTION TO
BASICS OF
PROGRAMMING
TOPICS DISCUSSED
• The C++ character set, identifiers and keywords, data
types, variables, declarations,
• Best practices for Programming -Naming variables
according to standards, Importance of following
coding standards- file header block, file footer block,
function header block, function footer block,
commenting guideline, Indentation of code,
statements,
• C++ program structure,
• Input and output operations,
• Program Life Cycle
6/22/2015 Department of CSE 2
BASICS OF
PROGRAMMING
• C/C++ History
• C++ Character Set
6/22/2015 Department of CSE 3
History of C
C is a high level language.
Evolved by Dennis Ritchie and Brain Kernighan(1978)
from two previous programming languages, BCPL (Basic
Combined Programming Language )and B
Used to develop UNIX system software routines
Is implemented on UNIX operating system
Structural / Modular Programming
Portable & have compilers for almost all architectures.
6/22/2015 Department of CSE 4
C++
“C with classes” (1979) C++ (1983)
Superset of C developed by Bjarne Stroustrup at
Bell Labs and provides object-oriented capabilities
named “c with classes”.
Object-oriented design is very powerful
10 to 100 fold increase in productivity
Dominant language in industry and academia.
6/22/2015 Department of CSE 5
Simple C++ program
// Display “This is my first C++ program”
// Single line comment
#include <iostream.h> // preprocessor directive
void main( ) // Entry point for program execution
{ // block of statements: Begin
clrscr( ); //Each Statement ends with ;
cout << “This is my frist C++ program”;
} // block of statements: End
6/22/2015 Department of CSE 6
Analogy between learning English
Language and C
Steps in learning English Language:
Alphabets
Words
Sentence
Paragraphs
Steps in learning C
Alphabets, Digits, Special Symbols
Constants, Variables, Keywords
Instructions
Programs
6/22/2015 Department of CSE 8
THE C++
C H A R A C T E R S E T,
IDENTIFIERS
K E Y W O R D S , C O N S TA N T S
D ATA T Y P E S , VA R I A B L E S
6/22/2015 Department of CSE 9
The Character set
Consists of letters, digits, special characters, white spaces.
(i) Letters ‘a’, ‘b’, ‘c’,………..z Or
‘A’, ‘B’, ‘C’,……….Z
(ii) Digits 0, 1, 2,……………………9
(iii) Special characters ;, ?, >, <, &,{, }, [, ]……
(iv) White spaces ex. New line (\n)
6/22/2015 Department of CSE 10
C++ Tokens
Tokens
Special
Keywords Identifiers Operators Strings constants
Symbols
(i) Keywords words that are basically sequence of characters
defined by a computer language that have one or more fixed
meanings. They are also called as reserve words. Key words cannot
be changed. ex. Int, float, do-while, if, else,…………..
(ii) Identifiers words which have to be identified by keywords.
user defined names. ex. int amount, float avg,…………..
(iii) Operators +, -, *, %, /, …………….
(iv) Strings “Manipal”
(v) Constants -15, 10
(vi) Special Symbols { } (,…..
6/22/2015 Department of CSE 11
Keywords
Each keyword has a predefined purpose in the language.
Do not use keywords as variable and constant names!!
Some of the C/C++ keywords are
auto, bool, break, case, catch, class, char,
const, continue, do, default, delete, double,
else, extern, enum, false, float, for, friend,
goto, if, int, inline, long, namespace, new,
operator, private, protected, public, register,
return, short, static, struct, sizeof, switch,
template, this, throw, try, typedef, true,
unsigned, virtual, void, volatile, while …
6/22/2015 Department of CSE 12
Identifiers
An identifier is a name for a variable, constant, function,
etc.
It consists of a letter followed by any sequence of letters,
digits, and underscores.
6/22/2015 Department of CSE 13
Identifiers
A valid identifier is a sequence of one or more
letters, digits or underscore character (_).
Neither spaces nor punctuation marks or symbols
can be part of an identifier
Only letters, digits and underline characters are
valid
variable identifiers always have to begin with a
letter
They can also begin with an underscore character
(_ ), but this is usually reserved for compiler
specific keywords or external identifiers.
6/22/2015 Department of CSE 14
Identifiers
They can not begin with a digit.
The C/C++ is a "case sensitive" language.
An identifier written in capital letters is not equivalent to
another one with the same name but written in small letters.
The “RESULT” variable is not the same as the “result” variable
or the “Result” variable
They cannot match any keyword of the C++ language or your
compiler's specific ones since they could be confused with
these.
6/22/2015 Department of CSE 15
Identifiers
Examples of valid identifiers: First_name, age,
y2000, y2k
Examples of invalid identifiers: 2000y
Identifiers cannot have special characters in them. For
example: X=Y, J-20, ~Ricky,*Michael are
invalid identifiers.
Identifiers are case-sensitive.
For example: Hello, hello, WHOAMI, WhoAmI,
whoami are unique identifiers.
6/22/2015 Department of CSE 16
Constants
A value that IS NOT going to be changed during
the execution of our program;
Constant are specific values that are used in
arithmetic expressions or assigned to variables,
e.g. 2, 5, -10, 2.e+6, 3.14159, and so forth;
Sometimes a constant represents truly a constant
value in nature such as:
Pi 3.14159
speed of light 2.99792+E8 meters/sec
6/22/2015 Department of CSE 17
Constants
Numeric & Character constants
Numeric: Integer & Real.
Integer constants: Refers to a sequence of digits.
Decimal, Octal , Hexadecimal.
Decimal: set of digits 0 to 9, preceded by
optional “–” or “+” sign
Octal: digits 0 to 7 with a leading “0”
Hexadecimal: digits 0 to 9, char A to F
preceded by “0x”
E.g.: 143, -564, 0346, 0x34, 0x8AF
6/22/2015 Department of CSE 18
Constants
Real or Floating point constant:
To represent numbers with fractional part
E.g.; 213.45, .456,234.
Another form mantissa e exponent
0.56e4, 3.12E4 , -4.6E-1
Character constants:
Single character & String constants.
Single character:
Single character with in a pair of single quote (‘ ’) marks,
having integer values known as ASCII values. E.g.: ‘d’, ‘t’,
‘9’
6/22/2015 Department of CSE 19
Constants
String constants:
A sequence of characters enclosed in double quotes
(“ ”). Characters may be letters, numbers, special
characters and blank space.
E.g.: “hello”, “2007”, “T”, “4+5”
Backslash character constants:
Used in output functions
E.g.: ‘\n’ new line, ‘\0’ null char, ‘\a’, ‘\b’, ‘\t’, ‘\”’ …
Also known as escape characters.
6/22/2015 Department of CSE 20
Constants
Symbolic Constants
Which helps us to associate an identifier with a
constant value in your program;
The advantage is that you can refer to the
identifier any time you need to use the
constant value instead of having to repeat
writing the value;
To declare a symbolic constant you must do it
as follows:
const data type identifier = value;
6/22/2015 Department of CSE 21
Summary
• C/C++ History
• Tokens
• Keywords
• Identifiers
• Constants
6/22/2015 Department of CSE 23