Most people get confused with C and C++. in comparison, C++ and C are different language in practice, they share similar elements with each other
think C++ as C only, with some extra features ( oops, Abstraction, inheritance, encapsulation, Polymorphism) and C++ is a Super set of C
Any C program is also a C++ program as we know C++ is a superset of C
A basic C++ program
#include <iostream> using namespace std; int main() { char str [50]; cout<< "Enter the characters:"; cin>>str; cout << str<< endl; return 0; }
Output
Enter the characters: hello_there Hello_there
Here the operator >> means an extraction operator
The operator<< means an insertion operator
Cout is the same as prinf() in C but the benefit of using cout is that you no need to use format specifiers(%f for float, %d for integer), and it is pronounced as “ C out”
Cin is the same as scanf() in C but the benefit of using Cin is that you no need to use format specifiers (%s for string, %f for float), and it is pronounced as “C in”
If you don’t want to use namespace std; then, in that case, you need to use to std:: where (::) is a scope resolution operator
#include <iostream> int main() { char str [50]; std::cout<< "Enter the characters:"; std::cin>>str; std::cout << str<<std::endl; return 0; }
Declare variable anywhere in the program
C++ allows the declaration of variables anywhere in the program
#include <iostream> using namespace std; int main() { int arr[5]={50,20,88,90,45}; for(int i=0;i<5;i++) { cout<<arr[i]<<"\n"; } return 0; }
A general conception in C that first, we declare a variable then we use the variable in a statement
For example, in C we iterate like this
int i; For(i=0;i<5;i++);
where as in c++ the variable can used where it is declared
for(int i=0; i<5;i++);
Resource
- Let us C++ by Yashavant Kanetkar
- Let us C by Yashavant Kanetkar
- Data structures using C by Reema Thareja
Leave a Reply