SuprBay: The PirateBay Forum

Full Version: Difficulty Understanding the Difference between C and C++
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Title: Difficulty Understanding the Difference between C++ and C

Hello everyone,

I've been trying to grasp the key differences between C and C++, but I'm finding it quite challenging. I've read articles and tutorials, including the one on what is difference between C and C++, but I'm still struggling to fully understand the distinctions.

I was wondering if someone could help clarify the dissimilarities between these two programming languages. Additionally, if possible, could you provide some code examples that highlight the disparities between C and C++? I believe practical examples would greatly enhance my understanding.

I appreciate any assistance or guidance you can provide. Thank you!

Code:
Code:
c
#include <stdio.h>

// Example C code
void greetC(char *name) {
    printf("Hello, %s! This is C programming.\n", name);
}

int main() {
    char name[] = "John";
    greetC(name);
    return 0;
}



Code:
cpp
#include <iostream>
using namespace std;

// Example C++ code
void greetCpp(string name) {
    cout << "Hello, " << name << "! This is C++ programming." << endl;
}

int main() {
    string name = "John";
    greetCpp(name);
    return 0;
}



In the code examples above, you can observe some differences between C and C++. The C code uses the `stdio.h` header and `printf` function, whereas the C++ code includes the `iostream` header and utilizes the `cout` and `endl` statements. Additionally, C++ introduces the concept of a `string` data type and the `namespace` keyword, which are not present in C.

Thank you once again for your help!
Well, there are lots of difference in between C and C++. Well, C++ introduces object-oriented programming, supports the Standard Template Library (STL), allows function overloading, and provides exception handling mechanisms. While C is a procedural language without built-in support for OOP. C does not support function overloading directly, and relies on error codes or custom error-handling approaches.
Well you can understand it with the help of example below:
Function overloading in C

// C does not support function overloading

#include <stdio.h>

void printNumber(int num) {
printf("Number: %d\n", num);
}

void printString(char* str) {
printf("String: %s\n", str);
}

int main() {
printNumber(10);
printString("Hello");
return 0;
}

Function overloading in C++

// C++ supports function overloading

#include <iostream>

void print(int num) {
std::cout << "Number: " << num << std::endl;
}

void print(const char* str) {
std::cout << "String: " << str << std::endl;
}

int main() {
print(10);
print("Hello");
return 0;
}

I hope you are clear now.
Thanks
Is function overloading a common and exclusive feature in object-oriented languages?

Thought that since you brought it up, the OP might also want to know this.
Well, it is not exclusive to object-oriented languages, function overloading is a feature that is frequently present in them. Within a class or module, multiple functions with different parameters but the same name can be defined with function overloading.