Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

C vs C++ in terms of simplicity?


  • Please log in to reply
6 replies to this topic

#1 NullPointerException

NullPointerException

  • Banned
  • 125 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:07:56 AM

Posted 18 November 2014 - 12:23 PM

Who is better in terms of simplicity? My vote goes for C.

/* Hello World program */

#include<stdio.h>

main()
{
printf("Hello World");


}
// C++ version
#include <iostream>

int main()
{
  std::cout << "Hello World!";
}



BC AdBot (Login to Remove)

 


#2 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:07:26 PM

Posted 18 November 2014 - 03:44 PM

Note that your first example is invalid in C99 (and C11), which drops the "default to int" rule requiring:

#include <stdio.h>

int main()
{
    printf("Hello World");
}
This is really an IO system comparison -- and even C++ fanbois like myself would tell you that iostream is not a great IO library; if nothing else because it breaks the "overloaded operators should behave like nonoverloaded counterparts" rule. Luckily, you can use the cstdio IO system in C++ just fine:

#include <cstdio>

int main()
{
    std::printf("Hello World");
}
The one place I know of where iostream can be really nice is when you want to read a line of input from the user. For example:

#include <iostream>
#include <string>

int main()
{
    std::string userEntry;
    if (std::getline(std::cin, userEntry)) {
        std::cout << "You entered: " << userEntry << "\n";
    } else {
        std::cout << "An IO error occurred.\n";
    }
}
is trivial to write in C++ but is tedious to do in C (because there's no finite size you can give to scanf that's safe to use because the user's input size is unbounded).

Billy3
Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

#3 NullPointerException

NullPointerException
  • Topic Starter

  • Banned
  • 125 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:07:56 AM

Posted 21 November 2014 - 08:34 AM

Note that your first example is invalid in C99 (and C11), which drops the "default to int" rule requiring:
 

#include <stdio.h>

int main()
{
    printf("Hello World");
}
This is really an IO system comparison -- and even C++ fanbois like myself would tell you that iostream is not a great IO library; if nothing else because it breaks the "overloaded operators should behave like nonoverloaded counterparts" rule. Luckily, you can use the cstdio IO system in C++ just fine:

#include <cstdio>

int main()
{
    std::printf("Hello World");
}
The one place I know of where iostream can be really nice is when you want to read a line of input from the user. For example:

#include <iostream>
#include <string>

int main()
{
    std::string userEntry;
    if (std::getline(std::cin, userEntry)) {
        std::cout << "You entered: " << userEntry << "\n";
    } else {
        std::cout << "An IO error occurred.\n";
    }
}

is trivial to write in C++ but is tedious to do in C (because there's no finite size you can give to scanf that's safe to use because the user's input size is unbounded).

Billy3

 

 

I've learnt C from the C Programming Language Second Edition. Unfortunately, I don't know modern C.

Yes, it is true like C++ has advantages. C has no exception-handling, and the pointers were a pain. As you've pointed out, it is more tedious to get user input

in C than in C++. However, I find the use of << and >> a little bit strange, and can be confusing for new programmers. In C#, the advantages of C++ is kept but its disadvantages and many errors like no garbage collection were fixed. (Which, unfortunately, C does not have. I hope C++ next version does adopt garbadge collection. It is true that I sometimes forget to delete objects and everyone just reads garbage) And then again, here's a C# try-catch block.

int GetInt(int[] array, int index)
{
    try
    {
        return array[index];
    }
    catch(System.IndexOutOfRangeException e)
    {
        throw new System.ArgumentOutOfRangeException(
            "Parameter index is out of range.");
    }
}

Here's an example C++ code.

// exceptions
#include <iostream>
using namespace std;

int main () {
  try
  {
    throw 20;
  }
  catch (int e)
  {
    cout << "An exception occurred. Exception Nr. " << e << '\n';
  }
  return 0;
}

C++ is more tedious, even if it has additional features. Plus, it is well-known fact that Linus hates C++ and thinks it's better to write a driver or a kernel in C. And it's true, because C has its place. The eighties' are considered to be the golden age of programming. 



#4 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:07:26 PM

Posted 21 November 2014 - 02:02 PM

Of course, the C# example should avoid throwing an exception for the easily preventable condition:

int GetInt(int[] array, int index)
{
    if (index < 0 || index >= array.Length)
    {
        throw new ArgumentOutOfRangeException("index", index, "Parameter index is out of range.";
    }
    
    return array[index];
}

C++ is more tedious, even if it has additional features.

This is merely opinion. Personally I find C far more tedious for missing basic features like dynamically allocated arrays. (C++'s std::vector or std::unique_ptr<T[]>) Deterministic constructors and destructors reduce errors by making it impossible to initialize state incorrectly or fail to release resources.

Being able to throw things that don't derive from std::exception is an unfortunate misfeature.

Linus hates C++ and thinks it's better to write a driver or a kernel in C

So what? (Also, the kernel has special rules in that it is often directly exposed to demand paging, which isn't something that 99% of user code cares about)

And it's true, because C has its place.

Did anyone claim otherwise? There is no one language / system / methodology / tool that is more "correct" than any other. You have to look at the problem you intend to solve and choose the right tool to solve that problem. If I'm writing a kernel driver, sure, I'd probably use C for that. If I'm writing anything else? C++ hands down.

The eighties' are considered to be the golden age of programming.

By whom? I'd strongly disagree.

Billy3
Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

#5 NullPointerException

NullPointerException
  • Topic Starter

  • Banned
  • 125 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:07:56 AM

Posted 22 November 2014 - 04:37 AM

Of course, the C# example should avoid throwing an exception for the easily preventable condition:
 

int GetInt(int[] array, int index)
{
    if (index < 0 || index >= array.Length)
    {
        throw new ArgumentOutOfRangeException("index", index, "Parameter index is out of range.";
    }
    
    return array[index];
}

While, yes, the code you mentioned is more readable (and more of a better practice), it's merely an example. It wasn't mean to be applied to real-world programming.

 

While writing a kernel, we require additional syntax. Then again, yes, it's my opinion. But Vector is a merely fancy pointer (But then again, it varies from person to person). I am pretty sure you can always trick C++ compilers.

 

As I am an intermediate programmer still learning good practices and learning Assembly, I find Linus' opinion as a guide. While it is obvious he is not a mage of programming, he has wrote one of the best OSes ever. I find kernel somewhat strange  and thus I don't fully trust Unix kernel.  Yes, C is a mere tool and it depends upon the wielder. if I do not know how to use a shotgun, I cannot say the shotgun is useless. It really depends on the programmer and how they handle things.

 

The 80's had C. And back then programmers were used to write programs in Assembly. New programmers learnt it the hard way and therefore begin good programmers. While I started off with C# (Python if you count the few days), I believe in learning lower level languages is better. But obviously, not every person has a time to learn what a heap is and what works for chip 6005 and what doesn't for chip 55511 at the same time.



#6 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:07:26 PM

Posted 23 November 2014 - 10:13 PM

But Vector is a merely fancy pointer

And yet I see lots of C code that implements linked lists (a terrible data structure in most cases) because implementing the geometric expansion and handling copying correctly can be tedious. Memory leaks are also typically an issue with C code, which tools like vector completely eliminate.

I find Linus' opinion as a guide. While it is obvious he is not a mage of programming, he has wrote one of the best OSes ever.

Ok, if you want to use Linus opinion as a guide, go ahead. But that's not an argument in favor of C.

I believe in learning lower level languages is better.

On what technical basis? Choosing a programming language is a technical decision, not a belief based decision.

not every person has a time to learn what a heap is and what works for chip 6005 and what doesn't for chip 55511 at the same time.

Thus the problem with assembler. You write it for one machine, and its useless on other machines. Forget what may work on one chip and not another, different chips often have completely different ISAs. For example, x86 vs amd64 vs ARM vs ARM Thumb mode are all completely different, and that's just comparing two different chip implementations (Haswell and A53, for example)

Couple that with the fact that compiler written assembly is generally better than human written assembly, and presto; C is generally as low a level as serious software should be written outside of embedded devices (e.g. a PIC).

 

If you want someone to say "C is the best language for everything" you're not going to find it here. Do you have a serious question at this point or are we done?

Billy3
Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

#7 NullPointerException

NullPointerException
  • Topic Starter

  • Banned
  • 125 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:07:56 AM

Posted 01 December 2014 - 12:41 PM

Thanks for the clarification. :) 

(Plus : I meant "I find Linus as a guide", because after all, everybody needs a teacher. However, I have no idea how I wrote "I find Linus' opinion as a guide")






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users