Dec 15, 2011

Differences between C and C + +


I have been getting comments and emails with questions, corrections and all kinds of insults (no, not that last one) and I decided to answer them all with this post which will clarify what is C and what is C + + and their differences.
C was created in 1972 by Dennis M. Ritchie at Bell Labs as an evolution of earlier language B, in turn based on BCPL.
C + + , meanwhile, was created in the mid-1980s by Bjarne Stroustrup. The intent of its creation was to extend the successful C programming language mechanisms that allow the manipulation of objects.
So C is the original language, while C + + is an extension of C, so the + +.
To me when I learned to program told me I'd learn to program in C + +, but I really only taught C, so many professors do not really know what it is programmed in C + +. Here is a hello world in two programs:
In C
 1
 2
 3
 4
 5
 6
 7
 # Include <stdio.h>

 int main () 
 {
         printf ("Hello World");
         return 0;
 } 
In C + +
 1
 2
 3
 4
 5
 6
 7
 8
 9
 # Include <iostream>

 using namespace std;

 int main ()
 {
     court <<"Hello World";
     return 0; 
 } 
For the type of programs that are showing on this blog the most important difference is the input and output. So let's see an example of input and output for each program:
In C
 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 # Include <stdio.h>

 int main ()
 {
     int radius;
     float area, perimeter;

     / / OUTPUT: a screen message
     printf ("Enter the radius of the circle:");

     / / INPUT: receive data from keyboard
     scanf ("% d", & radius);

     / / Load
     area = 3.1416 * radius * radius;
     perimeter = 3.1416 * radius * 2;

     / / OUTPUT: result on screen
     printf ("The area is% .2 f and the perimeter% .2 f", area, perimeter);
     getch ();

     return 0;
 } 
In C + +
 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 # Include <iostream>
 using namespace std;

 int main ()
 {
     int radius;
     float area, perimeter;

     / / OUTPUT: a screen message
     court <<"Enter the radius of the circle:";

     / / INPUT: receive data from keyboard
     cin>> radio;

     / / Load
     area = 3.1416 * radius * radius;
     perimeter = 3.1416 * radius * 2;

     / / OUTPUT: result on screen
     court <<"The area is" <<area <<"and the perimeter" <<perimeter
     cin. get (); cin. get ();

     return 0;
 } 
What I noticed these two little programs to do is to ask for an entry in C + + is much much simpler than in C. However, the issue of the <<and>> can get to 'scare' to newcomers to programming.
Request an entry in C
 scanf ("switch", & name of the variable); 
Ask a datum in C + +
 cin>> name of the variable; 
Display a data in C
 printf ("Data: switch" variable name); 
Display a data in C + +
 court <<"Data:" <<name of the variable; 
The modifiers are:% d for int,% f for float,% s for string,% c for char.

Libraries in C + +

For simple convention in C libraries ending in '. H' (point hache). All C libraries used for C + +, however, also by convention, the termination is removed. 'H' and best add 'c' at first.
Bookstore Bookstore C C + +
cmath math.h
string.h cstring
time.h ctime
etcetera.

The 'namespace'

C as a language has a set of reserved words, such as: if, for, while, int, float, ... C + + is an extension, so you have to add new keywords. These reserved words are in a 'namespace' (namespace). Specifically court and cin are the namespace std (standard).
If finds that we will use the std namespace (using namespace std;), whenever we wanted to use court,would have to write std:: court.
I hope that from now can identify whether a program is in C or C + +. I also hope to be publishing my programs in both languages ​​from now.

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Premium Wordpress Themes