How do people start programming C or C++?

After reading A Brief Introduction to Forth, I don’t see any reason not to call Forth a stack-based language. Care to elaborate?

From WhatIs:

How much Perl have you seen in your life? Perl is a prime example of a C-like language: It has all the same control structures, syntax rules, and code grouping mechanisms.

There are many, many C-like languages. Here are a few I can name off the top of my head:
[ul]
[li]Ada[/li][li]Ruby[/li][li]Python[/li][li]Perl[/li][li]Java[/li][li]Pascal[/li][li]Almost any modern interpreted language.[/li][/ul]C has become a modern template, like FORTRAN before it (FORTRAN begat COBOL, Basic, CORC, CUPL, and a host of other really bad teaching languages). It’s guaranteed that any new language will have to incorporate some C-like features to be acceptable to the majority of programmers.

Ok, name me some non-C-like languages in modern use by more than a few people worldwide.

Damn, you folks are great. Lotsa splendid information here - thanks $1e6.

Several folks have recommended Borland C++ Builder. But the Borland website seems to say I couldn’t use it on my NT computer at work - anybody know otherwise?

Everybody that recommends K&R (2nd edition) does so most eloquently. Not to worry - I’m reading it! As Derleth says, “Helpfully, K&R lists an abbreviated version of the ANSI documentation…” and in fact I didn’t notice this until last night (it’s actually an appendix). Wow - this is perfect - rather than muddying the water with a teaching example without stating the point, this splendid section just says what the language does - it’s the perfect introduction - where has it been all my life??? I’m gonna Xerox it onto the wall! But Derleth, really - “Everybody but Microsoft is using UNIX” is kinda like saying “There are no tyrants in Iraq except Saddam Hussain”, isn’t it? Oh, well, no harm in hoping, I guess. Onward: I’ll also check into the Oualline book.

Sandwriter, thanks for suggesting Bloodshed Dev-C++ 4.0. I’ll check it out.

Ultrafilter, the program in Forth to compute 3*4+2 and print the result would look like this:
: demo.for.ultrafilter 3 4 * 2 + . ;
You said “After reading A Brief Introduction to Forth, I don’t see any reason not to call Forth a stack-based language.” I quite agree. Actually, two stacks, one for computation and one for returns from words (subroutines), are at the heart of Forth. And Forth is actually assembly language for some Forth processors that implement the two stacks in hardware. I don’t mean a virtual machine, either - I mean real melted sand.

Urban Ranger, you said “Basic, Fortran and Ada certainly do not look anything like C…”
I found a neat collection of hello world programs at Hello, World Page!, to wit:

ADA:
with Text_Io; use Text_Io;
procdure hello is
begin
put (“Hello world!”);
end hello;

BASIC:
10 print"Hello World!"
20 goto 10

C:
#include <stdio.h>
main()
{
for(;:wink:
{
printf ("Hello World!
");
}
}
FORTRAN:
PROGRAM HELLO
DO 10, I=1,10
PRINT *,‘Hello World’
10 CONTINUE
STOP
END

FORTH:
: hello
begin
true
while
." Hello World "
repeat
;

Which ones do you think look the most and least C-like?

Well, I do know that C does not have smilies, unless you’ve gone gonzo with an Emacs mode.

:smiley:

I was wrong about Ada being C-like. It is block-structured, which is what most C code ends up looking like, but the structure is more a part of the language than it is in C. It also has other resemblences to C, but not enough to make it C-like. It is actually Pascal-descended, which means it was written to be deliberately non-permissive in an attempt to fight bugs. C, on the other hand, is free-form in its syntax and permissive in its rules: Types can be overridden with casts, pointers can be bent, folded, spindled, and mutilated, and a programmer can rewrite portions of the language with judicious (or gonzo :D) use of the preprocessor.

I was wrong on Ada, but knowing C syntax has stood me in good stead as I’ve learned other languages.

A way to write the FORTRAN program so it has an infinite loop is:



     PROGRAM HELLO2
100  WRITE *, "HELLO, WORLD!"
     GO TO 100
     STOP
     END


Gotos are Considered Harmful. Computed gotos are Considered Sadistic.

:smiley:

Wow, where do I begin.

First of all, Pascal was invented long before C. It, its descendent Modula, and Ada are all Algol derivatives. Algol influenced far more programming languages than C, which is also one of its descendents.

As for interpreted languages, apparently you are unaware of Lisp descendents such as Scheme that are widely used.

COBOL was not influenced to any sig. extent by Fortran.

Etc., etc.

OK, I admitted I was wrong about Pascal and, by extension, Ada.

If you put it that way, modern imperative languages all have very similar control structures, syntax rules, and code grouping mechanisms.

Urban Ranger: Exactly my point, and all of those structures are similar to C’s. So learning C would help you learn those the way learning Latin gives you a foothold into Italian, French, and Spanish.

The real difference in imperative languages comes in to play once you start dealing with the object-oriented languages. There are a lot more issues there, and lots of other things you have to provide syntax for.

Well, they’ve been a part of VB longer than that–remember VBX controls? But you needed a C compiler to make those. I confess that I’m ignorant about recent versions of VB… can you make your own full-featured ActiveX controls with it?

I’ve had no problems running Delphi on NT 4, and C++Builder on Windows 2000 and XP (NT 5.0 and 5.1). You shouldn’t have any trouble.

That doesn’t mean the languages themselves are similar. Knowing how an if/else statement works will help you write decision-making code in nearly any language (perhaps not Scheme or Forth), but knowing C won’t necessarily help you learn Basic or Python.

I disagree that Python is a C-like language. One of the most fundamental differences between C (or Java, C#, Snack…) and other languages such as Basic (or Fortran, ABC, REXX…) is that C ignores whitespace and line breaks. On the other hand, Python code uses indentation to group statements and line breaks to terminate them. Artistically arranged code like you see in the Obfuscated C Contest simply isn’t possible in those languages.

Is it just me, or is that an odd use of “for”? What does that do, an infinite loop? (I should know this, but my C / C++ is very rusty.)

Yes, that does loop forever.

[hijack]

Python FAQ on obfuscated code
Here’s another obfuscated Python page
[/hijack]

Derleth,

You said “Gotos are Considered Harmful.” I always hear this. But the machine language implements all sorts of control structures with gotos, doesn’t it? Perhaps what should be considered harmful is how people can get lost in them, or something.

And I think C does have lots of smilies. That is, I am guessing that the definition of a smilie has something to do with three semicolons in a row. C often says “for parenthesis semicolon semicolon semicolon closedparanthesis”. If I type it for(;;;), which looks right in my “Your Reply” window, I think it gets displayed like a smilie. Maybe if I put spaces in there, for( ; ; ; ), it won’t. But I don’t get to find out till I post it. So, whether C uses smilies depends on your - excuse me, the SDMB’s - definition of a smilie.

Sure, but look at these IOCCC winners… :slight_smile:
dhyang
thadgavin
bas1
dloweneil
heathbar

Yes.

The difference is, the compiler’s goto’s have been thoroughly tested, and probably proven correct. Yours, on the other hand, probably haven’t. goto is best left to the experts.

“Gotos Considered Harmful” was my misremembering of the title of the paper that began the absolutely moronic Structured Programming Wars. It was actually called “Goto Statment Considered Harmful,” and was written by Edsger W. Dijkstra.

`Structured Programming’ is the idea that all code must conform to certain precepts to be correct and readable, which is all well and good until you consider how many ways there are to code something, and how ideas relevant in one context (writing large applications) are counterproductive or nonsensical in another (coding device drivers).

The main good to come out of the Structured Programming Wars was the idea that code should be structured, that having jump statments (or gotos, or branch statements, etc.) everwhere is not a good idea, but that any good idea can be taken too far.

http://wombat.doc.ic.ac.uk/foldoc/foldoc.cgi?considered+harmful

But that’s my point: It’s all syntax, aka implementation details, not general concepts. Look at it this way: Learning Forth isn’t going to do you a lot of good if you go right on into Ruby. Why? You’ll have to relearn everything. You’ll have to switch from the stack-model to the procedural-model, and that involves a lot more thought than gleaning the syntax from a language overview and some example code. You could, however, go from Ruby to C with less trouble, because both are procedural langauges. The spelling changes, but the grammar remains the same.

Napier: Please don’t read so much into my jokes. ::grin::

I don’t think there is ever a situation in C where you have three semicolons in a row, but you sometimes have two, like this:



for (;;) {
   printf("This will print forever.
");
}


Why will that print forever? It’s all to do with the syntax of the for statement.[ul]
[li]The first part of the for statment, the part before the first semicolon, initializes the index variable. IOW, it sets up the beginning of the loop. If you wanted to count from zero to 99 using i as an index variable, this would say i = 0'.[/li][li]The second part, between the two semicolons, decides if the loop is done yet. It does this by seeing if the index variable satisfies the end condition. To carry out my example, this part would say i <= 99’, which means if i is less than or equal to 99, we're not done yet'.[/li][li]The third and final part, after the second semicolon, advances the loop. It is a statement that does something to the index variable. A good statement for our purposes would be i++’, which increments i.[/li][/ul]If the compiler notices one of those statments is missing, it doesn’t care. It just ignores it and compiles the loop anyway. So if all three parts are missing, the compiler creates an infinite loop. Things like these are called idioms, and like idioms in natural languages, they can trip up newbies just as much as they help seasoned veterans. (BTW, Perl has a huge stock of idioms. The language’s motto is TMTOWTDI (There’s More Than One Way To Do It), something that either repulses you or enamors you.)

If you want to create code without smilies, like I did, check the litte `Disable Smilies in This Post’ button below the text box.

Object-oriented programming languages differ from each other more than regular procedural languages. The way you use objects in C++ is about 50% different from how you use them in Delphi (where all object variables are pointers), and totally different from Objective C or Smalltalk (where you pass messages at run time instead of binding method calls at compile time).

Another difference between C++ and most other languages is the role of user-defined types. In C++, the compiler works enough magic that you can extend the language to fit your needs by writing new classes, though the purpose of your code will be unclear to anyone who isn’t familiar with your new types. Other languages (e.g. Delphi, Java) make a much greater distinction between language syntax and user function calls, which means coding is more tedious, but your code is more readable.