3.diff_c_c++
3.diff_c_c++
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
⚫ 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
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
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