Home / Programming / Some difference between C and C++ programming languages

Some difference between C and C++ programming languages

Although C++ is a superset of C, there are some small differences between the two, and a few are worth knowing from the start. Before proceeding, let’s take the time to examine them.so read and understand the difference between C and C++

First, in C, when a function takes no parameters, its prototype has the word void inside its function parameter list. For example, in C, if a function called f1() takes no parameters ( and returns a char, its prototype will look like this:

difference between C and C++
difference between C and C++

char f1(void) for difference between C and C++

However, in C++, the void is optional. Therefore, in C++, the prototype for f1() is usually written like this: char f1();

C++ differs from C in the way that an empty parameter list is specified. If the preceding prototype had occurred in a C program, it would simply mean that nothing is said about the parameters to the function. In C++, it means that the function has no parameters. This is the reason that the preceding examples did not explicitly use void to declare an empty parameters list. (The use of void to declare an empty parameter list is not illegal; it is just redundant. Since most C++ programmers pursue efficiency with a nearly religious zeal, you will almost never see void used in this way.) Remember, in C++, these two declarations are equivalent:

char f1();
char f1(void);

Another subtle difference between C and C++ is that in a C++ program, all functions must be prototyped. Remember, in C, prototypes are recommended but technically optional. In C++, they are required. As the examples from the previous section show, a member function’s prototype contained in a class also serves as its general prototype, and no other separate prototype is required.

The third difference between C and C++ is that in C++ if a function is declared as returning a value, it must return a value. That is, if a function has a return type other than void, any return statement within that function must contain a value. In C, a non-void function is not required to actually return a value. If it doesn’t, a garbage value is ”returned”.

In C, if you don’t explicitly specify the return type of a function, an integer return type is assumed. C++ has dropped the ”default-to-int” rule. Thus, you must explicitly declare the return type of all functions.

One other difference between C and C++ that you will commonly encounter in C++ programs has to do with where local variables can be declared. In C, local variables can be declared only at the start of a block, prior to any ”action” statements. In C++, local variables can be declared anywhere. One advantage of this approach is that local variables can be declared close to where they are first used, thus helping to prevent unwanted side effects.

Finally, C++ defines the bool data type, which is used to store Boolean (i.e., true/false) values. C++ also defines the keywords true and false, which are the only values that a value of type bool can have. In C++, the outcome of the relational and logical operators is a value of type bool, and all conditional statements must evaluate to a bool value. Although this might at first seem to be a big change from C, it isn’t. In fact, it is virtually transparent. Here’s why: As you know, in C, true is any nonzero value and false is 0. This still holds in C++ because any nonzero value is automatically converted into false when used in a Boolean expression. The reverse also occurs: true is converted to 1 and false is converted to 0 when a bool value is used in an integer expression. The addition of bool allows more thorough type checking and gives you a way to differentiate between Boolean and integer types. Of course, its use is optional; bool is mostly a convenience.

Read- types of comments in c++ – Single line comments in c++ (Explained)


In a C program, it is common practice to declare main() as shown here if it takes no command-line arguments:

int main(void)

However, in C++, the use of void is redundant and unnecessary.

This short C++ program will not compile because the function sum() is not prototyped:

// This program will not compile

#include <iostream> using namespace std;

int main()


int a, b, c;

cout << “Enter two numbers: “; cin >> a >> b; c = sum(a, b); cout << “Sum is: ” << c;

return 0 ;


// This function needs a prototype sum(int a, int b)


difference between C and C++


return a+b;


Here is a short program that illustrates how local variables can be declared anywhere within a block:

#include <iostream> using namespace std;

int   main()


int i; // local var

cout << “Enter number: cin >> i;

// compute factorial

declared at


start of block
int j, fact=1;   // vars

for(j=i; j>=1;   j–) fact = fact   * j;

declared after action statement
cout << “Factorial is ” << fact;
} return 0 ;

The declaration of j and fact near the point of first use is of little value in this short example; however, in large functions, the ability to declare variables close to the point of their first use can help clarify your code and prevent unintentional side effects.

The following program creates a Boolean variable called outcome and assigns it the value false. It then uses this variable in an if statement.

#include <iostream> using namespace std;

int main()


bool outcome; outcome = false;

if(outcome) cout << “true”;

else cout << “false”;

return 0 ;


As you should expect, the program displays false.


The following program will not compile as a C++ program. Why not?

// This program has an error.

#include <iostream> using namespace std;

int main()

{ f();

return 0 ;


void f()


cout << “this won’t work”;


On your own to understand difference between C and C++ try declaring local variables at various points in a C++ program. Try the same in a C program, paying attention to which declarations generate errors.Then read again my post – difference between C and C++

Read My Other Posts


1. Blogging For Starters – Learn How to start a successful blog in 2018

2.Learn C++ Programming Within 14 Days ( Introduction )

About rhtoufiq

Check Also


WHAT IS OBJECT ORIENTED PROGRAMMING?   Object-oriented programming is a powerful way to approach the …