CPP Module 2
CPP Module 2
By MRC
Road Map
• Identifiers • Pre-processor
• Literals
• Constants
• Data Types
By MRC
Computer Programming
By
ByMRC
MT
Computer Programming
By MRC
Computer Programming
• In the very early days of programming, Binary was used but it was too hard to
remember and understand.
By MRC
Computer Programming
• However, Assembly is also very hard to learn, read, understand and maintain.
Assembly is not portable, meaning it can only run on the computer it’s written
on.
• So, along with many other languages, came the C Programming Language.
By MRC
The C Programming Language
By
ByMRC
MT
C Programming Language: Overview
By MRC
C Programming Language: Application
By MRC
C Programming Language: History
• C was evolved from other programming languages at the time such as ALGOL,
BCPL and B.
• C was developed along with the UNIX operating system. In fact, UNIX was almost
entirely developed in C.
By MRC
C Programming Language: History
• As time went on, the language was improved further and further introducing
many new and improved features.
• Many higher level programming languages were inspired by C such as C++, C#,
Java and many more.
By MRC
C Programming Language
• In the slides, we’ll be using MinGW compiler and Visual Studio Code IDE.
By MRC
C Programming Language
By MRC
C Programming Language
• Code in hello.c
By MRC
C Programming Language
• Some lines in the code have their own separate topics and will be explained in
detail in upcoming chapters.
• Here, we’re including stdio.h header file for using features required in our
program.
By MRC
C Programming Language
• int is what’s known as Data Type which will be explained in future chapters.
• The { (opening brace) marks the beginning and the } (closing brace) marks
the end of the main function.
By MRC
C Programming Language
• In this specific case, returning 0 means the program is telling the computer
that it has completed its execution successfully.
By MRC
C Programming Language
By MRC
C Programming Language
• This command will compile the C program into an executable file which contains
the Binary code. This executable file will have the extension .exe
exename
By MRC
C Programming Language
• Most lines of code end with a semicolon. It’s the full stop of C language.
Forgetting the semicolon will result in compilation error.
• Some statements which require a block of code do not generally end with a
semicolon, for example, the main function.
By MRC
Character Set
By
ByMRC
MT
Character Set
• Alphabets
• Uppercase letters (A to Z)
• Lowercase letters (a to z)
• Digits
• 0 to 9
• Special Symbols
By MRC
Character Set
• Each character in the character set has an ASCII value associated with it which
represent how the characters are stored internally in the computer.
By MRC
Character Set
• Escape sequence characters are prepended with a backward slash (\) sign.
• These characters are converted into another character when the output is
displayed.
• For example:
By MRC
Character Set
Character Meaning
\n New line
\r Carriage return
\t Horizontal tab
\v Vertical tab
\b Backspace
\0 NULL
\\ \
\’ ‘
\” “
By MRC
Keywords
By
ByMRC
MT
Keywords
• Keywords are part of the language, they cannot be used for any other purposes.
By MRC
Identifiers
By
ByMRC
MT
Identifiers
By MRC
Identifiers
• For example,
By MRC
Variables
By
ByMRC
MT
Variables
• A variable generally stores data that may change when the program is running,
hence the name “variable”.
• For example,
By MRC
Variables
• Variable names are identifiers, so the rules for identifiers apply when naming a
variable.
• When creating a variable, its Data Type must be specified. That is, what type of
data the variable will store.
By MRC
Variables
Declaration
• Syntax:
• Example:
Assignment
• Putting a value in a variable that has already been declared is called Assignment.
• Syntax:
• Example:
By MRC
Variables
Initialization
• Syntax:
• Example:
By MRC
Literals
By
ByMRC
MT
Literals
By MRC
Constants
By
ByMRC
MT
Constants
• Example:
• When the program is compiled, the compiler replaces the name with the
value defined in the directive.
• Example:
By MRC
Data Types
By
ByMRC
MT
Data Types
• Variables of different types will occupy different number of bytes on the primary
memory.
• Derived types
• User-defined types
By MRC
Data Types
By MRC
Data Types: Primary Types
• Different primary types have different ranges of values that are allowed.
• Integer (int)
• Character (char)
• Void (void)
By MRC
Data Types: Primary Types
• Some of the primary types can be modified to increase and decrease their
ranges and sizes.
• Size-based Modifiers
• long long: Increases the range and size, even larger than long.
• Sign-based Modifiers
By MRC
Data Types: Primary Types
Integer (int)
• Example:
Integer (int)
By MRC
Data Types: Primary Types
Integer (int)
By MRC
Data Types: Primary Types
• Example:
• The suffix “F” (or “f”) is necessary, if it’s not specified then it’ll be considered a
double floating-point number
By MRC
Data Types: Primary Types
• Example:
By MRC
Data Types: Primary Types
Character (char)
• Example:
By MRC
Data Types: Primary Types
Void
• void type variables cannot be created, however void has other uses which will
be covered in future topics.
By MRC
Data Types
Format Specifiers
• All primary types and some derived types have Format Specifiers associated
which them that allows inputting and outputting the value.
By MRC
Strings
By
ByMRC
MT
Strings
• String variables can be created using character array type, for example:
• A string is always, by default, terminated with a NULL (\0) character at the end.
The msg variable in the above example contains the following value:
• It’s possible to declare a string variable without a value, but the expected size of
the string must be specified:
By MRC
Strings
• The format specifier %s can be used with printf( ) and scanf( ) functions to
output and input strings respectively:
• When using scanf( ), the ampersand sign (&) before the variable name is not
required.
By MRC
C Stream
• The initial programmers had a very hard time handling the I/O operations.
• The programs created had to explicitly identify the input device where the input
would be taken from and the output device where the output would be sent to.
• This concept removed the need for a program to identify the kind of devices
used.
• A data stream is an ordered sequence of data bytes that can be read until the
end of the file.
• Three standard streams:: standard input, standard output, and standard error.
By MRC
Standard Input (stdin)
By MRC
Standard Output (stdout)
By MRC
Standard Error (stderr)
• The stream stderr is independent of the standard output and can be redirected
separately.
• A classic usage of this stream is done when the user tries to open a file on a
secondary storage.
By MRC
Input and Output Functions
By MRC
General Syntax of
Formatted Functions
By MRC
Input Function
● Used as
scanf(“format string”, address list of identifiers);
By MRC
Program to illustrate the use of
scanf
#include<stdio.h> int main()
{
int first;
float second;
double third;
char choice;
/* Read and print the values */
printf("\nEnter the values :");
scanf("%d%f%lf%c",&first, &second, &third, &choice);
printf("\nValues of the variables are %d %f %lf %c\n ",first,second,third,choice);
return 0;
}
By MRC
Output Function
● Used as
printf( “format string”, list of identifiers);
printf(“message string”);
#include <stdio.h>
int main( )
{
int i_number;
printf("\n Enter the value of number : ");
scanf("%d",&i_number);
printf(" \n Value in base 10 is : %d :: in base 16 is %x\n", i_number, i_number);
printf(" \n Value in base 8 is : %o :: in character is : %c \n", i_number, i_number);
return 0;
}
By MRC
Unformatted Functions
gets(identifier name);
puts(identifier name);
By MRC
Program to illustrate unformatted
functions
#include<stdio.h>
int main()
{
char data, temp, name[25];
printf("\nEnter data :");
data=getchar();
temp=getc(stdin);
printf("\nOutput of data using putchar:");
putchar(data);
printf("\nOutput of data using putc:");
putc(data,stdout);
By MRC
Operators
By
ByMRC
MT
Operators
• These operations are performed on one or more values. These values are known
as operands.
• Unary operators
• Binary operators
• Ternary operators
By MRC
Operators
• Unary operators:
• Binary operators:
• Example: a + b
• Ternary operators:
• Example:
By MRC
Operators
• Arithmetic operators
• Relational/comparison operators
• Logical operators
• Conditional operator
• Bitwise operators
• Assignment operators
• Special operators
By MRC
Operators
Arithmetic Operators
By MRC
Operators
Relational/comparison Operators
By MRC
Operators
Relational/comparison Operators
By MRC
Operators
Logical Operators
• Logical OR (||)
• The Logical AND (&&) and Logical OR (||) operators are used for combining two
conditions and getting a single result.
By MRC
Operators
Logical Operators
• Example:
• Assume a variable:
By MRC
Operators
Logical Operators
• Logical OR (||):
• Example:
• Assume a variable:
By MRC
Operators
Logical Operators
• Example:
• Assume a variable:
By MRC
Operators
Conditional Operator
• Example:
By MRC
Operators
Bitwise Operators
Expression Result
A&B 0000 1100
~A 1100 0011
By MRC
Operators
Assignment Operators
Assignment Operators
By MRC
Operators
Assignment Operators
By MRC
Operators
• Prefix
• Postfix
By MRC
Operators
• Prefix Increment/Decrement:
• When using prefix increment or decrement, the value of the operand is first
incremented or decremented then its value is returned.
• Example:
• In above example,
By MRC
Operators
• Postfix Increment/Decrement:
• When using postfix increment or decrement, the value of the operand is first
returned and then incremented or decremented.
• Example:
• In above example,
By MRC
Operators
Special Operators
By MRC
Operators
Operator Precedence
By MRC
Operators
Operator Precedence
By MRC
Comments
By
ByMRC
MT
Comments
• Comments are ignored by the compiler and have no effect on the workings of the
program.
• Single-line
• Multi-line
By MRC
Comments
Single-line Comments
By MRC
Comments
Multi-line Comments
• Multi-line comments can span over multiple lines and are enclosed in /* … */
signs.
By MRC
Type Casting
By
ByMRC
MT
Type Casting
• Each data type is either smaller or bigger compared to other types, as mentioned
in the Data Types chapter.
• Implicit
• Explicit
By MRC
Type Casting
• As shown in the above example, num1 (int) and its value will be converted to
float type automatically and assigned to num2 (float).
By MRC
Type Casting
• Since, float is bigger than integer, converting a bigger type to a smaller one
causes data loss here.
• After conversion, the value of num2 will be 10. The real number 10.5 will lose its
fractional part during conversion.
By MRC
Type Casting
• To perform explicit type casting, the target type is written in brackets before
variable or the value during assignment.
• As shown in the above example, num1 (int) and its value will be converted to
float type and assigned to num2 (float) manually.
By MRC
Type Casting
• In the above example, num1 (float) will be converted to integer type and
assigned into num2 (int) manually.
• Since float is bigger than integer, converting a bigger type to a smaller one
causes data loss here.
• After conversion, the value of num2 will be 10. The real number 10.5 will lose its
fractional part during conversion.
By MRC
Pre-processor
By
ByMRC
MT
Pre-processor
• Before the compiler compiles a program, the source code of the program is
processed by a tool called the Pre-processor.
• #include
• #define
By MRC
Pre-processor
#include
• #include <filename>
• #include “filename”
• Searches for the included file first in the same directory as the program
then standard include directories.
By MRC
Pre-processor
#define
• Examples:
By MRC
Structure of a C Program
By
ByMRC
MT
Structure of a C Program
• Each C Program follows a specific structure, each component of the structure can
be divided into sections.
• Documentation Section
• Contains comments that shows the author, date, description and any
other information about the program.
• Link Section
• Definition Section
By MRC
Structure of a C Program
• Subprogram Section
By MRC
Structure of a C Program
By MRC
By MRC