Shop OBEX P1 Docs P2 Docs Learn Events
Why C is better than BASIC, can anyone provide live example? — Parallax Forums

Why C is better than BASIC, can anyone provide live example?

CuriousOneCuriousOne Posts: 931
edited 2015-04-13 06:13 in General Discussion
Hello.

I've often hear that C is way better than basic, because it is faster and more robust language. Indeed, C WAS FASTER once ago, when there was compilers for C and interpreters for BASIC. But currently, both being very effectively compiled. So I'm interested, can someone provide some simple task, and code in C and BASIC, so it will be possible to see, why C is better than BASIC ?
«13456712

Comments

  • idbruceidbruce Posts: 6,197
    edited 2015-03-30 22:26
    I would say that when asking such a question, you should provide a context. The biggest difference I believe you will see is when it comes to computer programming, as compared to working with an embedded design. In computer programming, there is just a much wider array of source code and functions, and I would assume this would be because of the popularity of each language, and because Microsoft went the direction of C for the most part.

    You must always consider the advent of the PC, the main contributors of promoting the PC, and the languages that were mostly used. The one that was used the most, would of course be the most powerful, because it would be the most developed. Now if you are looking for specific examples, it would probably take hours to find such examples.

    I would imagine, that if you were truly interested, you could probably find a huge difference between C and BASIC in image control and manipulation. To really see the difference, you would have to take a look at PC programming, but everything is always changing.
  • jmgjmg Posts: 15,173
    edited 2015-03-30 22:39
    CuriousOne wrote: »
    .... So I'm interested, can someone provide some simple task, and code in C and BASIC, so it will be possible to see, why C is better than BASIC ?
    You are correct that compilers determine the code efficiency, not the editor-syntax used.

    C is not what I'd call robust, and not even that well suited to Embedded control, lacking a native boolean type for a very long time, until it was dragged screaming, to join other languages.

    The thing that determines C's success, is not syntax, or any of it's warts, it is something quite separate called 'critical mass'.
    Basic, by comparison, is too divergent with no two the same.
  • idbruceidbruce Posts: 6,197
    edited 2015-03-30 22:51
    In reference to my ealier post, as I said, things are always changing. When I started getting out of PC programming and more into embedded programming, there was a big push for interface access. So basically both C and Basic had access to the same interfaces, methods, and members, just by gaining a a reference to the interface. I forget the exact name of this stuff, but difficult programming.

    EDIT: I believe it was the COM interfaces.
  • Heater.Heater. Posts: 21,230
    edited 2015-03-31 00:06
    @jmg,
    C is not what I'd call robust...
    I might have thought so to. Were in not for little projects like the Linux kernel with it's millions of lines of C that is being updated with thousands of patches per day year after year but turns out to be an incredibly reliable piece of software. Or the fact that C's big brother C++ is used in safety critical systems the Joint Strike Fighter http://www.stroustrup.com/JSF-AV-rules.pdf

    In what way is C not robust? What do you call robust?
    ...not even that well suited to Embedded control...
    In what way not suited?
    ...lacking a native boolean type for a very long time, until it was dragged screaming, to join other languages.
    That bool type seems such a minor convenience detail. I don't recall anyone having major difficulty without it.

    Certainly there are more features of C inherited by languages that came after it than features like bool that have been "back ported" C. I think C should scream if anyone tries to "modernize" it. It is what it is and grafting modern features on to it will only make it worse :)



    Oh, whilst I'm here. The only good thing about BASIC is that you get to C at the end of it :)
  • Cluso99Cluso99 Posts: 18,069
    edited 2015-03-31 00:29
    Because Basic was too easy for non-programmers to understand. So programmers invented C to make it more difficult for non-programmers to understand. ;)
  • CuriousOneCuriousOne Posts: 931
    edited 2015-03-31 01:16
    Yep, everyone is saying "C is better", but none provides practical example.
  • LoopyBytelooseLoopyByteloose Posts: 12,537
    edited 2015-03-31 01:30
    Well, Linux is written in C.. as is or was Unix.

    As IDBruce mentioned, much dependends on the context. Small architectures suffer an overburden from C. Big projects with adequate resources in terms of libraries do well with C.

    Historically, C is a very important milestone in programing. It allowed programers to not have to learn a new lanaguge everytime they moved to a new platform, though Intel soon became the dominant platform for C.

    Basic quickly turns to 'which Basic?' Some adhere to the principles of the original Dartmouth Basic and try not to do too much; while others have become huge and not really basic in spirit -- like VisualBasic.

    C can easily be frustrating for new learners, just because the tool chain and error reporting is sophisticated. Spin and PASM are provided by Parallax to relieve the learning curve to some extent. Propeller Basic? Maybe even easier -- not sure.
  • David BetzDavid Betz Posts: 14,516
    edited 2015-03-31 01:37
    Basic quickly turns to 'which Basic?' Some adhere to the principles of the original Dartmouth Basic and try not to do too much; while others have become huge and not really basic in spirit -- like VisualBasic.
    This is exactly the problem. There is a standard for C and most implementations either implement that or a superset. There is also a standard for Basic but no one follows it. Every Basic is different such that you can't easily port a Basic program from one version to another.
  • idbruceidbruce Posts: 6,197
    edited 2015-03-31 01:55
    Once again.... Things are always changing.

    Back in the day, there was no such thing as Visual Basic.NET, Visual C#.NET, or Visual C++.NET.....

    But here is Microsofts rundown on the state of these languages: Reference 2003

    https://msdn.microsoft.com/en-us/library/aa292164(v=vs.71).aspx

    EDIT: Look at the list of services under C++
  • jmgjmg Posts: 15,173
    edited 2015-03-31 02:28
    David Betz wrote: »
    This is exactly the problem. There is a standard for C and most implementations either implement that or a superset. There is also a standard for Basic but no one follows it. Every Basic is different such that you can't easily port a Basic program from one version to another.
    That's why I think it helps to follow a PC-established Basic, when doing an embedded basic..
    Which one?, is the chestnut (as above)?
    FreeBasic ? Somewhat piecemeal, with separate IDE / Compiler / Debug efforts, but maybe good enough ?
    Some talk of a RaspPi version.

    VB ? Comes with a free, nicely polished IDE, but cannot create exe files ?
    No talk of a RaspPi version ? or is there ?
    http://blogs.msdn.com/b/laurelle/archive/2013/06/21/net-microframework-on-raspberrypi-part-1.aspx
  • CuriousOneCuriousOne Posts: 931
    edited 2015-03-31 03:23
    So, there's no performance issue, but the standardization issue in case of BASIC ?
  • TorTor Posts: 2,010
    edited 2015-03-31 03:51
    When I started writing BASIC programs way back in time, I very quickly ran into the limitations. BASIC didn't have functions, you had to play tricks with line numbers in case you needed to extend the code, it was not possible to do any kind of structured programming, in short it was a mess for anything complex.
    At a later time I was eyeballing BBC BASIC which had extensions like subroutines, loop control, etc. And no line numbers
    Now there are several "Basic" variants which aren't BASIC in the Dartmouth sense.
    I was anyway thoroughly disgusted by BASIC and left it behind, even Fortran 66 was infinitely more structured than BASIC. Fortran 77 much more so. Well, even assembly language is more structured. So I moved on to Fortran and Pascal and other structured languages, and eventually to C. They are all infinitely more pleasurable to work with than BASIC. Now, "Basic" (in lots of modern more or less incompatible non-BASIC variants) are better, but I see no point in using that when there are alternatives which don't give me that low down feeling I get with BASIC. So for me, it's the experience with BASIC in the past. Programming should be fun, whenever I touch (even new) Basic I get that bad feeeling from old and nothing good can come out of it.
  • Heater.Heater. Posts: 21,230
    edited 2015-03-31 03:58
    CuriousOne,

    There is no "better". There is no answer to your question until you can say what it is you need be better. Easy to learn for beginners? Faster? Smaller? More portable? Easier for complex/large code? Wide user base? Better documentation? Better libraries? More widely available? Etc etc etc.

    Program enough machines in enough languages for enough different applications and you will come to to find that all programming languages are flawed and annoying. That's why people keep inventing new ones!

    If you are in this for the long haul you want your code to be useful on many different machines (large and small), with many different CPU architectures, with many different operating systems for years into the future. To that end the simplest DNA that runs all around the computing world today is C.

    At the end of the day choice of language to use for any particular project is not all about technical evaluations, it's quite a lot about human social interaction. Despite what Loopy says programming languages are a means of communication between humans as much as English or Chinese. The one you use may depend on the one everyone else uses.

    But as you have put your question as a challenge about performance I present my Fast Fourier Transform in C. It's a rude and crude FFT but works pretty well. Runs on any machine for which there is a C compiler. On the Propeller it can automatically make use of 1, 2, 4 or even 8 COGs to boost performance. It is impossible that any BASIC FFT is as quick or can easily use multiple COGs.

    https://github.com/ZiCog/fftbench/blob/master/fftbench.c

    Edit: I use the word "impossible" above in the usual sense of the word on this forum.
  • pmrobertpmrobert Posts: 673
    edited 2015-03-31 04:09
    It seems to me that a programming language is very much an abstract concept which solely exists to allow humans to be able to more easily implement machine code. The language is the language, it's just a bunch of syntactical and mathematical rules and definitions. The compiler/interpreter/optimizers/etc. can make a huge difference in speed, efficiency, code density and suitability to a task. OP, with all due respect and as IDBruce pointed out in his first message, context is everything. On a DOS machine, sure Watcom C would blow the doors off an equivalent Qbasic program. The main difference was not the language, it was the fact one was interpreted, the other compiled. Bean's PropBasic on the other hand, is a very lean Basic with Prop specific commands that seems to me to more resemble a very human friendly macro assembler and emits very fast, efficient code. In that case you could compare PropGCC in XMM mode to PropBasic and conclude that Basic is a much faster language than C - but you'd be wrong. It's only those specific implementations that were the basis of the speed disparity, not the language. The tradeoff is that the XMM case has a veritable plethora of code and/or data space where PropBasic has at most an LMM extension with the 32K limit. Horses for courses, tradeoffs, pros/cons. such is life as well as programming languages, their implementations, the car you drive, pretty much everything. A more fair question would be to ask yourself what is most suitable after you define your existing skill set, define your goal and then choose the path to get to the goal from where you are that personally fits you.
  • Heater.Heater. Posts: 21,230
    edited 2015-03-31 04:10
    Loopy,
    Small architectures suffer an overburden from C.
    They do? I don't think so. I think we have had this conversation before but here we go:

    C was developed on machines with very small memory spaces, the PDP-8, PDP-11 era.

    C (actually C++) is used on the tiny 8 bit AVR based Arduino. It is used to program even smaller devices.
    Big projects with adequate resources in terms of libraries do well with C.
    True. But when projects get big enough you start yearn for a language that can organize large code bases better. Enter C++.
  • Heater.Heater. Posts: 21,230
    edited 2015-03-31 04:36
    pmrobert,

    To a large extent you are correct. One might say that "a language is a language and the differences in performance depend on if it is compiled or interpreted, or how efficient those compilers and interpreters are"

    After all we have compiled BASICs which must be faster than interpreted, and we also have interpreters for C/C++ that must be slower than compilers. https://root.cern.ch/drupal/content/cint

    However, the actual syntax and semantics of a language design can also determine it's performance. Or even whether it can be compiled at all.

    Languages like FORTRAN, ALGOL, C and the many children in that linage are designed to be easily compiled to small, efficient, fast code.

    Language like Lisp, Scheme, JavaScript are designed with different criteria that actually make them very hard or impossible to compile to an efficient binary executable.

    For example, in JavaScript I can write:
    function myFunc (x) {
    	return x + 2;
    }
    
    Well, what code should that be compiled to? If x is a number we want that "+" to be a floating point addition and then return an number. If that "x" is a string we want to do a conversion of the number 2 to a string and then do a string concatenation and return a string. If there were a JS compiler it would have no idea what to do. An interpreter is the only way to deal with this.

    How BASIC fits in the scheme of things I'm not sure. Is it possible to make an efficient BASIC compiler?
  • Martin_HMartin_H Posts: 4,051
    edited 2015-03-31 05:06
    I'll repeat the "which BASIC?" question. Older dialects lacked pointers, structured data types and variable scoping. Newer dialects are usually proprietary and not portable to other platforms. Heck Visual BASIC isn't portable between versions! That's no way to run a railroad.
    Heater. wrote: »
    How BASIC fits in the scheme of things I'm not sure. Is it possible to make an efficient BASIC compiler?

    Most BASIC dialects I've worked with weren't sufficiently dynamic to inhibit compilation. So I would guess that BASIC has more in common with C than JavaScript.
  • Heater.Heater. Posts: 21,230
    edited 2015-03-31 05:26
    Martin_H,
    Older dialects lacked pointers...
    Do what? BASIC has pointers now a days? I missed a telegram some time (or is it a FAX you young guys have today?) Any chance of a code snippet as an example?

    If BASIC has descended into pointer madness then I see no reason for it. Just use C.
  • David BetzDavid Betz Posts: 14,516
    edited 2015-03-31 05:35
    Heater. wrote: »
    Martin_H,

    Do what? BASIC has pointers now a days? I missed a telegram some time (or is it a FAX you young guys have today?) Any chance of a code snippet as an example?

    If BASIC has descended into pointer madness then I see no reason for it. Just use C.
    Who needs pointers when you have peek and poke? :-)
  • Heater.Heater. Posts: 21,230
    edited 2015-03-31 05:45
    David,
    peek and poke
    Now you are talking my kinda BASIC.
  • Heater.Heater. Posts: 21,230
    edited 2015-03-31 06:06
    Clearly C is superior to BASIC in terms of code size, execution speed, portability, clarity of algorithm expression, ubiquity, mind share, availability of useful libraries and so on and so forth.

    However, none of that matters. What matters is that you can write things like this in C:
    main() { printf(&unix["\021%six\012\0"],(unix)["have"]+"fun"-0x60);}  
    
  • prof_brainoprof_braino Posts: 4,313
    edited 2015-03-31 06:27
    CuriousOne wrote: »
    ...why C is better than BASIC ?

    No, C is not better than BASIC, nor is BASIC better than C. Each is a tool for a specific job. BASIC is more basic, and therefore less flexible. C is more flexible in that you can do lots of intricate stuff, but allow you to shoot yourself in the foot more often. Sometimes you can miss, and shoot yourself in the head.

    Here's your example use cases:

    If you are starting out and just want to make a program work, BASIC is fine. But know how to slap together a program in BASIC does NOT qualify one as a programming expert.
    If you know a LOT about programming and good programming practices, and know C; then use C. Otherwise, avoid it until you can learn good programming practices.

    Right tool for the right job, as allways.
  • localrogerlocalroger Posts: 3,451
    edited 2015-03-31 06:32
    The elephant in the room, which nobody has mentioned, is garbage collection. There are very good compiled BASICs, such as QuickBASIC for DOS and VB 4, 5, and 6, which create standalone executables that have good performance. (dot-NET was a huge step backward from the numbered VB's and is an entirely different and inferior animal.) I am still using VB6 for Windows development.

    Those BASICs are slower than C because they are better than C, in that they have native string handling immune to buffer overruns. The later VB's even manage to handle arbitrarily long strings effectively without noticeable garbage collection pauses which would glitch the 16-bit string libraries. C is of course faster but that's because it isn't doing as much copying and isn't doing all that bound checking. That's an issue for things like operating systems or web servers that have to handle thousands of concurrent requests, but for applications the performance is adequate and the convenience wins the day.

    BASIC also separates itself from languages like Javascript because that string handling is native, not part of a library, and yet the language is strongly typed, which is an advantage when one wants to manage resources closely or catch certain kinds of error. It's very sad that development essentially stopped when Microsoft stabbed the VB community in the back and that nothing new has happened since VB6, but then 6 was a very mature language that still works almost flawlessly and works in emulation better than .NET.

    The other main advantage C has is threading, which shouldn't be an issue ("Threading is for people who don't understand state machines") but is because some OS functions require it for optimization. VB5 could be coaxed to spawn threads and there were some interesting demos but 6 was deliberately crippled to make that impossible. On the other hand 6 introduced some very powerful string array functions which go a long way toward making up for that.

    The main difference between the languages is the focus of their designers. C was designed to give maximum power and performance in very minimal hardware and it shows. BASIC was designed to be easy to use. Now that the hardware has gotten thousands of times more powerful than the original designers ever dreamed, one of those goals is not as important as it once was.
  • Heater.Heater. Posts: 21,230
    edited 2015-03-31 06:54
    localroger,

    I was following along with you there and mostly agreeing until I came to this:
    The other main advantage C has is threading, which shouldn't be an issue ... but is because some OS functions require it for optimization.
    Well, C, the language, has no concept of threads. Unlike Erlang or Occam or xc or Ada for example. So I'm not sure what you mean.

    Also I'm curious as to what OS functions require threads for optimization? It's generally accepted that the context switching involved in threaded code is a major performance drag. That's why languages that can handle many concurrent tasks efficiently, Erlang, JavaScript, are not threaded.

    Then I was agreeing some more until I came to:
    Now that the hardware has gotten thousands of times more powerful than the original designers ever dreamed, one of those goals is not as important as it once was.
    That is all true but efficiency matters. When you scale up your energy and hardware bills become significant and small savings can save millions of dollars. That is why Facebook messaging uses Erlang for example.

    On the other end of the spectrum we are scaling down. To battery powered mobile devices and those "Internet of Things" things where efficiency is very desirable.
  • MJBMJB Posts: 1,235
    edited 2015-03-31 06:59
    just to add again,
    you can programm ARDUINO hardware also with the very easy to use BASCOM.
    www.mcselec.com
    And one of the great things is the inline ASM should you want to do something really time critical.

    And to add to LocalRoger -
    I also still use VB6 to create small standalone applications.
    Or even VBA inside EXCEL which is almost identical to VB6 and VERY similar to BASCOM (if you don't use the objects and extensions).
    So I have VBA - Visual Basic for Applications code that I did easily port to BASCOM.

    When I came to the Prop I was thinking about PropBASIC,
    but then Tachyon started and it's gravity pulled me in ;-)

    In general: have a look into your toolbox. Is there only one hammer or also maybe a screwdriver, a saw ...
    It is good to have the choice :-)
  • mindrobotsmindrobots Posts: 6,506
    edited 2015-03-31 07:03
    Heater. wrote: »
    ... and so forth.

    @heater, your sentence seems to have ended prematurely! "...and so Forth" what? I'm sure you were bringing up an important point! :D

    As discussed ad nauseum, it ends up being horses for courses. Whatever you think fits you and the task best works best until you find it doesn't.

    I'll ask back to the original poster......Why is chocolate ice cream better than vanilla??
  • idbruceidbruce Posts: 6,197
    edited 2015-03-31 07:13
    In general: have a look into your toolbox. Is there only one hammer or also maybe a screwdriver, a saw ...
    It is good to have the choice :-)

    C/C++ is like owning an entire hardware store :)

    And Basic has a Dim future. :)
  • Heater.Heater. Posts: 21,230
    edited 2015-03-31 07:23
    MJB,

    BASCOM, do what? Nearly one hundred Euros. Allows me to spend hours of my life creating code in a language that is never going to be useful anywhere else but on the AVR. And I can only do it if I use a certain operating system to run the IDE. I think not. This is not 1990 any more.

    Or have I missed a point about BASCOM some how?
    In general: have a look into your toolbox....It is good to have the choice.
    Yep. Totally with you there.

    If anyone wants to know what programming language is "best", there is only one way, try them, see what they offer, see where they work, see what you can do.
  • Heater.Heater. Posts: 21,230
    edited 2015-03-31 07:34
    mindrobots,
    "...and so Forth" what? I'm sure you were bringing up an important point!
    Ha, as I typed that the same thought did cross my mind. I decided to let it be. (By the way if you are going quote me please don't change the capitalization, it can change the meaning totally)

    But yes, horses for courses.

    At least if somebody flogs a dead horse long enough we might get some good salami :)
Sign In or Register to comment.