Difference between
C and C++
Prof. Tejashree V. Pawar
Cummins College of Engineering
Objective
⚫ Understanding the basic differences between C and C++
programs.
⚫ Appreciate the ease of programming in C++
Contrast differences between C and C++ programs:
⚫ I/O
⚫ Variables
⚫ Math library
⚫ Standard library – headers
⚫ Loop
⚫ bool type
C program C++ program
#include <stdio.h> I/O Header #include <iostream>
using namespace std;
int main() //main function Comments Defines a Scope
{ int main() //main fun for identifier
printf(“Hello World!”); /*c {
state.*/ cout << “Hello world!\n”; /*c++ state.*/
return 0; return 0;
} }
Stream to
Print to console,
console, Console is cout
Console is ostream
stdout file
Predefined object that
represent the Standard Output operator called
output stream as insertion or put to
operator
Namespace:
● Defines a scope for the identifiers that are used in the program.
● For using the identifiers defined in the namespace scope we
must include the using directive.
● std is the namespace where ANSI C++ standard class libraries
are defined. This will bring all the identifiers defined in std to
the current global scope.
Use of cin – input operator
C Program area of rectangle C++ Program
Dynamic
initialization
Cascading of I/O operators
of variable
or cin >> l >>b;
⚫ scanf is used to scan from console. ⚫ cin corresponds to standard input stream.
⚫ Address of l & b needed in scanf. ⚫ >>:extraction or get from operator.
⚫ All variables l,b and area are declared first ⚫ l and b can be directly used w/o address
and then use in program. ⚫ variables l,b and area declared whenever required
⚫ Format specifiers like %d, %f are required in program.
for respective variables. ⚫ Formatting is derived from declaration of
respective variables.
Use of endl
endl : line feed operator
endl is a manipulator, used in output
statement to insert a new line like \n.
Program without using namespace
Scope resolution
operator
Math library
C Program for finding square root C++ Program
⚫ Math header is math.h (C standard library) ⚫ Math header is cmath (C standard library in C++)
⚫ Formatting %lf is needed ⚫ Formatting not required
⚫ Sqrt function from C std. Library ⚫ std::sqrt function from C std. Library in C++
⚫ Default precious in print is 6 ⚫ Default precious in print is 5
C standard library C++ standard library
⚫ All names are global ⚫ All names are within std namespace
⚫ stdout, stdin, printf, scanf ⚫ std::cout, std::cin, std::sqrt, std::pow
Standard library Header conventions
Commonly used old-style Header files
Loop
C Program for sum of n natural no.s C++ Program
⚫ i must be declared at the beginning ⚫ i declare locally in for loop.
⚫
Data type
It is type of data that variable in the program may hold.
Data Types
Primary / Built-in Derived Data User Defined
Data Types Types Data Types
1. void
2. char 1. Arrays
3. int 1. Structures
2. Functions 2.Unions
4. float 3. Pointers
5. double float 3. Enumerations
4. Reference 4. class
6. bool
7. wchar_t
Data type Range No. of Bytes in 16- No. of Bytes in 32-bit
bit compiler
compiler
1. signed char -128 to +127 1 byte 1 byte
2. unsigned char 0 to 255 1 byte 1 byte
1. int (singed) -2147483648 to +2147483647 2 byte 4 byte
1. short signed int -31768 to 32767 2 byte 2 byte
2. short unsigned int 0 to 65535 2 byte 2 byte
3. Long signed int -2147483648 to +2147483647 4 byte 4 byte
4. Long unsigned int 0 to 4294967295 4 byte 4 byte
1.float 3.4e-38 to 3.4e+38 4 byte 4 byte
1. double 1.7e-308 to +1.7e+308 8 byte 8 byte
2. long double 3.4e-4932 to 1.1e+4932 10 byte 12 byte
wchar_t - 2 byte 4 byte
bool - 1 byte 1byte
Using bool
bool data type
wchar_t Data type
➢ Wide-character types can be used to represent a character that
does not fit into the storage space allocated for a char type.
➢ A wide character is stored in a two-byte space for 16-bit
compiler and a four-byte space for 32-bit compiler.
➢ Japanese language has more than 255 characters which is not
fit in 1 byte.
➢ Wide character literals begin with the letter L, as follows:
➢ wchar_t ch = L'A';
− wchar_t *str = L"ABCD"; //The memory allocation for a string
is four bytes per character
Summary