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

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

2456712

Comments

  • potatoheadpotatohead Posts: 10,261
    edited 2015-03-31 07:38
    Well, C is better than BASIC because:

    -more portability
    -is available for everything I've ever encountered
    -lots of code out there
    -lots of code libraries out there
    -can get close to the metal
    -in line assembly
    -often comes with sophisticated tools, debugger, linker, assembler, etc...

    A well produced C tool chain is a systems level package. See Linux, and a lot of embedded things. C can be packaged up as a rapid application development tool too. See the Arduino, for a great example of that.

    BASIC is better than C because:
    -one can get things done knowing very little
    -is more approachable than a lot of things, and this depends a lot on the BASIC and what it's running on
    -in line assembly, on the better BASICS
    -nostalgia (serious here, it's fun in some implementations)
    -is interactive
    -can form basis for primary interface to a device / computer
    -can offer convenience type instructions
    -can offer lots of libraries
    -for some dialects, there is a lot of code out there

    Some BASICS are application tool kits / packages. There are lots of people doing embedded things, and specialized business applications / and such with BASIC tool box, rapid application type packages, featuring compilers, and other tools.

    From here, depends on what you want to do. PropBASIC is spiffy! Fast, works well, and there are fans here. PropGCC is spiffy, fast, works well, there are fans here. SPIN, some say slow, others say fast enough, includes PASM, works well, and there are fans here.

    Also Forth, Because Small and Fast.
  • localrogerlocalroger Posts: 3,451
    edited 2015-03-31 07:41
    Heater. wrote: »
    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.

    True, C doesn't explicitly support threads but because of its underlying simplicity it doesn't get in the way of using threads via libraries and in fact a lot of important applications that rely on threading are written in C. The BASIC language libraries, especially for things like garbage collected string handling, are not thread safe and the VB6 libraries are poisoned so that *nothing* works if you try to spawn a thread.
  • idbruceidbruce Posts: 6,197
    edited 2015-03-31 07:43
    Okay....

    Let's go back to the thread title and post....

    Why C is better than BASIC, can anyone provide live example?
    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 ?

    One of my preferences to C is that there is an abundance of code out there. If you need a snippet, you will have a much better chance of finding it in C as compared to Basic.

    I suppose that is one specific area we could test. How fast can relevant code be obtained for a given task.
  • mindrobotsmindrobots Posts: 6,506
    edited 2015-03-31 07:49
    Heater. wrote: »
    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.

    Agree 100%!

    People's brains work differently and accept things differently and visualize thing differently. It took me a while before Perl just clicked one day and things I had been struggling with all of a sudden made sense.

    Some people never get Forth (can't understand that one), for some, Javascript is hard to grasp with callbacks and such.

    Try all the flavors, just because you are told it's the wrong language, it may actually work for you!

    Take some of that with a grain of salt. According to them, Perl doesn't have arrays until you get to Perl 6. They've also left out Dictionaries (or Hashses) as a data type - it's an incredibly useful data type in modern languages. Python also has named tuples which are a great way to implement records.

    Combining dictionaries and the fact that functions are first class objects is a really cool way to make a switch statement.....which officially, Python doesn't have.
  • Heater.Heater. Posts: 21,230
    edited 2015-03-31 07:51
    idbruce,
    Worth a quick glance:
    Wow, that linked article is a serious OCD nerd fest of language keywords and types etc.

    It totally misses the point of any meaningful language comparison in terms of semantics and functionality.

    There is no mention of: closures, immutable data, first class functions, prototypical inheritance, threads. Hardly mentions dynamic types or generators or lambdas. I could go on.

    Totally useless.
  • idbruceidbruce Posts: 6,197
    edited 2015-03-31 07:55
    Totally useless.

    LOL... Like I said a quick glance... There are some comparisons there.
  • User NameUser Name Posts: 1,451
    edited 2015-03-31 08:10
    potatohead wrote: »
    Well, C is better than BASIC because:
    ...
    -can get close to the metal
    ...

    That is the principal reason for me! C was a much more convenient form of assembly. Yet it was high-level, too. For an assembly language programmer like me it was miraculous. Basic was hopelessly awkward for close-to-the-metal programming. Its authors did everything they could to separate you from what was really going on at the machine level.
  • potatoheadpotatohead Posts: 10,261
    edited 2015-03-31 08:30
    User Name wrote: »
    That is the principal reason for me! C was a much more convenient form of assembly. Yet it was high-level, too. For an assembly language programmer like me it was miraculous. Basic was hopelessly awkward for close-to-the-metal programming. Its authors did everything they could to separate you from what was really going on at the machine level.

    Yep. And it's interesting how some macro assemblers end up centering on code that looks a lot like C.

    That separation is a good thing, depending. That's why a question like this needs context. "better" is subjective.
  • Heater.Heater. Posts: 21,230
    edited 2015-03-31 08:40
    potatohead,
    Yep. And it's interesting how some macro assemblers end up centering on code that looks a lot like C.
    Yep, and it's interesting how many subsequent high level languages centre on code that looks like C: C++, Java, Objective C, C#, JavaScript, Lua, etc etc.

    Although their semantics have diverged a lot.

    C is the "latin" of the programming language landscape.
  • Martin_HMartin_H Posts: 4,051
    edited 2015-03-31 09:01
    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.

    I'm fairly sure that PBasic arrays work like pointers. I seem to recall exceeding the array allocation and stomping on the next variable. Also VisualBasic has objects with reference to them. They lack pointer arithmetic, but are otherwise just like pointers.
  • Dave HeinDave Hein Posts: 6,347
    edited 2015-03-31 09:07
    Give a man Basic, and he can play Star Trek. Teach a man to program C, and he can write a Basic interpreter -- and then he can play Star Trek.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2015-03-31 09:14
    heater wrote:
    C is the "latin" of the programming language landscape.
    ... at least as far as the Romance languages go. There's also the Teutonic family that includes Algol, Pascal, Modula-2, Oberon, Python, Spin, etc. Also the Slavic family that includes Forth (reverse Polish notation) and the Semitic family that includes APL (right-to-left-reading). I don't know how you'd characterize Ada -- Esperanto maybe?

    -Phil
  • mindrobotsmindrobots Posts: 6,506
    edited 2015-03-31 09:22
    Heater. wrote: »
    C is the "latin" of the programming language landscape.

    You mean it's DEAD??? :D
  • tonyp12tonyp12 Posts: 1,951
    edited 2015-03-31 10:00
    I'm warming up to C, it's like a pre-compiler for Asm and if you write the syntax correct it's pretty efficient and IAR have a setting for high that can be based on speed or code size.
    example, A function can be set for __inline and it will not actually call it but treat it as a macro if your settings is for speed.

    If you #include <stdbool.h> you get boolen support, but it does waste a whole byte for a 0 or 1.

    bitfield structure extension does handle bits as boolen just fine, add Union and you got yourself a nice GPIO register system.
    struct {
    char a:1;
    char b:1;
    char c:1;
    char d:1;
    char e:1;
    };
  • MicksterMickster Posts: 2,693
    edited 2015-03-31 10:03
    Heater. wrote: »
    Martin_H,

    Do what? BASIC has pointers now a days?

    Oh, only since about the mid '90s. In-line asm too

    https://www.powerbasic.com/support/help/pbcc/pointers_%28@%29.htm

    If time is money to you, I suggest you look at a modern BASIC. Then again if it's someone else's dime or your idea of programming is simply to stick together someone else's code that you might not even understand - but trust anyway(!!!) - go with C. It will at least help your resume
  • CuriousOneCuriousOne Posts: 931
    edited 2015-03-31 10:26
    And as far as I know, C does not has (at least in microcontrollers) BASIC statement MID$ equivalent, which was already integrated into BASIC interpreters in early 80s. (reference here: https://www.powerbasic.com/support/help/pbcc/mid_function.htm)

    Also, in BASIC I don't need to care about memory alocation and so on (in reasonable manner of course, all compilers have limitations of course), so no need for POINTERs.

    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,

    Can we get some examples from there if possible?
  • David BetzDavid Betz Posts: 14,516
    edited 2015-03-31 10:30
    CuriousOne wrote: »
    And as far as I know, C does not has (at least in microcontrollers) BASIC statement MID$ equivalent, which was already integrated into BASIC interpreters in early 80s. (reference here: https://www.powerbasic.com/support/help/pbcc/mid_function.htm)

    Also, in BASIC I don't need to care about memory alocation and so on (in reasonable manner of course, all compilers have limitations of course), so no need for POINTERs.

    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,

    Can we get some examples from there if possible?
    This is quite true and one of the reasons I still have a fondness for the old Basic interpreters I used to use on the PDP-12 when I was in college. However, you can get those same features in much more capable languages these days. Try Python, Ruby, JavaScript, etc. Also, you don't get those features in some MCU versions of Basic like pbasic that runs on the BASIC Stamp.
  • CuriousOneCuriousOne Posts: 931
    edited 2015-03-31 10:35
    Inline ASM code was available even in Sinclair ZX Spectrum basic, © 1982.
  • localrogerlocalroger Posts: 3,451
    edited 2015-03-31 10:51
    Dave Hein wrote: »
    Give a man Basic, and he can play Star Trek. Teach a man to program C, and he can write a Basic interpreter -- and then he can play Star Trek.

    I've written several BASIC interpreters and compilers -- in VB6. Those started as byte coded interpreters similar to 70's/80's era 8-bit BASICs for code compactness, then expanded to include inline ASM and inline compiled BASIC as my needs expanded. The different code streams have different capabilities; I have to use ASM to write interrupt driven code, as the byte code interpreter isn't thread safe and the unoptimized compiled BASIC doesn't have fine enough register control. I can write functions and pass and return arguments in compiled BASIC but not in byte code. Byte code supports all the usual string functions, compiled treats strings as byte arrays. The compiler does do proper numeric expression evaluation. Did all that for the 80186, then had to do it for flat memory '386 when the box I was hacking was updated.

    VB is a good language for that kind of system because of its string handling. String buffer overruns simply aren't ever a problem even when you haul a half megabyte file into RAM as a string. VB6 also has string array split and join functions which are very fast. There a few very annoying missing things, especially two's complement integer math and shift and boolean logic operators. I wrote a DLL in another more primitive language to provide those.
  • mindrobotsmindrobots Posts: 6,506
    edited 2015-03-31 10:58
    CuriousOne wrote: »
    And as far as I know, C does not has (at least in microcontrollers) BASIC statement MID$ equivalent, which was already integrated into BASIC interpreters in early 80s. (reference here: https://www.powerbasic.com/support/help/pbcc/mid_function.htm)

    It took me a Google and a click to find this C example of mid:
    #include "mbed.h"
     
    #ifndef min
    #define min(a,b) ( (a) < (b) ? (a) : (b) )
    #endif
     
    void mid(const char *src, size_t start, size_t length, char *dst, size_t dstlen)
    {       size_t len = min( dstlen - 1, length);
     
            strncpy(dst, src + start, len);
            // zero terminate because strncpy() didn't ? 
            if(len < length)
                    dst[dstlen-1] = 0;
    }
     
    int main()
    {
            const char *stringSrc = "Hello World";
            char stringDst[9];
     
            // copy "World" to destination 
            mid( stringSrc, 6, strlen(stringSrc) - 6, stringDst, sizeof(stringDst));
     
            // show what we did
            printf( "src ...%s...\r\ndst ...%s...\r\n", stringSrc, stringDst);
     
            return 0;
    }
    

    also demonstrating a point made earlier - the world is awash in good (and bad) c examples.
    Can we get some examples from there if possible?
    [/code]
    By this, I assume you haven't seen earlier BASIC code (before functions and subroutines)?

    Altair BASIC Manual from 1975.

    Scroll about 2/3 of the way down this page and you'll see a listing of an Eliza program written in "old school" BASIC. (Sorry if the link makes folks teary eyed!)
  • CuriousOneCuriousOne Posts: 931
    edited 2015-03-31 11:15
    So I'm asking, can somebody provide a live example - "this is way simpler to make in C, than in BASIC" ?

    The earliest BASIC I'm used was one available on PDP-11, not sure which BASIC it was.
  • TorTor Posts: 2,010
    edited 2015-03-31 11:22
    mindrobots - thanks for providing the example, I'm on a mobile device and can't do that myself. Yes, by CuriousOne's question I assume he hasn't actually seen BASIC in its original form. [Edit: Well, PDP-11 probably used a primitive BASIC - if so, then I simply don't understand why the question was asked.] That short Eliza program is not nice to look at. It was after writing much larger and more complex code that I realised that this was not the way forward. I instinctingvely felt that there must be a Better Way out there.. I wanted structure instead of flowcharts, and I never wanted to see goto's used for what should have been function calls (which worked just fine in assembly, as I was familiar with. Not so in BASIC). In short, I left BASIC behind and never looked back. I've only used it since to write Hello World tests when I'm testing something retro I've built. Then move away to something better.

    As for MID$ etc, remember that C the language doesn't have _any_ string handling. That is instead delegated to libraries, the language is designed to make it easy to add functions for almost everything you need. There is a standard library with e.g. strcpy(), but that's not part of the language definition per se. And you can and should add your own, as needed. I've written many string variants for myself. And lately also functions that mimic Perl's dynamic list operators, as they are so convenient and the syntax is familiar. C lets you do this.
  • mindrobotsmindrobots Posts: 6,506
    edited 2015-03-31 11:30
    CuriousOne wrote: »
    So I'm asking, can somebody provide a live example - "this is way simpler to make in C, than in BASIC" ?

    The earliest BASIC I'm used was one available on PDP-11, not sure which BASIC it was.

    Heater did: Linux is way simpler to make in C than it could be to make in BASIC. Software that runs TELCO switches was found to be way simpler to be made in Erlang than in C. The S2 GUI was way simpler to be made in Perl than it was in any other language.

    All for different reasons. You aren't going to get any concrete answers or rules from any one because everybody has different reasons they use different languages.

    The Wumpus game written in "old school" BASIC was easier to leave in BASIC and port to a PIC microcontroller running Micromite BASIC than to rewrite in C - 1) I don't really know/like C and 2) for this case, BASIC made more sense.

    I'm currently looking at Pymite - it's written in C and Python....expanding the project in BASIC would not be a good choice.

    A number of applications at work are written in Java because of the way Java fits into the enterprise and it is a more effective tools for the developers than writing the applications in C.
  • CuriousOneCuriousOne Posts: 931
    edited 2015-03-31 11:33
    From the viewpoint of normal human, it looks like:

    "Thank you for choosing our car, it doesn't have _any_ seats. That is instead delegated to aftermarket, the car is designed to allow you to add functions for almost everything you need. Standard type seats are available in aftermarket". Of course, you can and should add your own log or sofa, as needed.
  • TorTor Posts: 2,010
    edited 2015-03-31 11:45
    If I buy a car I buy it for driving, it's a utility. When I use a compiler, I do that because I want to program. If I want to just use something premade to do some computing, e.g. browse the net, I grab the tablet. I program because I want to program. So I want something flexible, something that doesn't have the decisions and constraints pre-made: One size fits all. C (and to a much larger degree something like Lisp) gives you the bare minimum, what you get out of that is much more flexibility to create, which is why I program in the first place. When that's said, C comes with a large arsenal of pre-made library functions that cover everything basic, and the world is full of libraries which do more. The point is that defining the language around libraries instead of a pre-made set of features makes for a much richer toolkit, and that's good in my book.
  • mindrobotsmindrobots Posts: 6,506
    edited 2015-03-31 11:52
    CuriousOne wrote: »
    From the viewpoint of normal human, it looks like:

    "Thank you for choosing our car, it doesn't have _any_ seats. That is instead delegated to aftermarket, the car is designed to allow you to add functions for almost everything you need. Standard type seats are available in aftermarket". Of course, you can and should add your own log or sofa, as needed.

    For which languages?

    C is a very simple language by definition. To it have been added libraries with some amazing power.

    Python is referred to as having "batteries included". It is a much more complex language (don't anybody get upset by that) that has an enormous amount of features included in its standard environment.

    BASIC implementations are so varied, it's hard to tell what is BASIC anymore.

    Many languages are esoteric in nature and their features seem illogical, impractical, unorthodox, etc. until you need them

    Some languages, people just look at, scratch their heads, shrug their shoulders and walk away. While other people look at the same language and think it will replace running water as one of the world's greatest inventions.

    What languages you end up using comes down to a personal choice or often a choice that is made for you.

    I'm still not sure what you wanted from your original post. Many people with many (many) years of programming experience have basically shown you why there really isn't a "better" it all depends on the situation, goals and circumstances. We have many veritable dinosaur programmers hanging around this place, many of them STILL learning new languages because their "better" just got "bested" for something they want to do next.

    What motivated your question? Did someone say, "you should use/learn C because it is better than BASIC"? If someone did, invite them to the forums to join the language wars and plead their case.....it should be interesting.
  • dgatelydgately Posts: 1,630
    edited 2015-03-31 11:58
    CuriousOne wrote: »
    And as far as I know, C does not has (at least in microcontrollers) BASIC statement MID$ equivalent, which was already integrated into BASIC interpreters in early 80s. (reference here: https://www.powerbasic.com/support/help/pbcc/mid_function.htm)

    This kind of makes a good point (for C) in that although C does not have an inherent MID$ equivalent, there are standard C libraries that would allow a developer to extend C with a MID$-type function, quite easily.

    As an example, string.h includes many useful functions:

    https://en.wikipedia.org/wiki/C_string_handling

    A simple MID$-type function in C (yes, you need to check lengths and not overlap the dest & source, as memory management is the C programmer's responsibility. But, it is also a feature that allows optimization):
      strncpy(dest, source + start, num); 
      dest[num] = '\0'; 
    

    dgately
  • CuriousOneCuriousOne Posts: 931
    edited 2015-03-31 12:00
    Well, I have a personal opinion of course, and I prefer BASIC. But it is also an established believing that "C is better than BASIC". Since my knowledge level does not allows me to see any practical advantages (I only see disadvantages) I've decided to ask, if someone can clarify above mentioned believing.
  • kwinnkwinn Posts: 8,697
    edited 2015-03-31 12:03
    CuriousOne wrote: »
    From the viewpoint of normal human, it looks like:

    "Thank you for choosing our car, it doesn't have _any_ seats. That is instead delegated to aftermarket, the car is designed to allow you to add functions for almost everything you need. Standard type seats are available in aftermarket". Of course, you can and should add your own log or sofa, as needed.

    Depends on what you consider a "normal human" A car carries people from place to place so it needs seats. It does not need radio, tv, internet etc. so those are optional items. Same is true for a computer language. If I use Fortran to do edge detection for cardiac imaging I need math and imaging capabilities, but not string manipulation, so it was a good language for that application even though it is lacking in other ways. C has the basic functions needed for a variety of applications and a library of additional functions for when they are needed. That you can add needed functions via the libraries to the core language is one of it's strong points. No need to waste memory on unneeded code.
  • CuriousOneCuriousOne Posts: 931
    edited 2015-03-31 12:06
    One of key differences of humans vs. other animals is the fact that we can use other ways of communications, not accessible to other animals. Like written text, imaging and so on. That was the point in the above message.
Sign In or Register to comment.