Why does "C" have a "primitive" structure?

Steve R.

Retired
Local time
Today, 05:17
Joined
Jul 5, 2006
Messages
5,427
As a non-programmer, I have often wondered why "C" retains a "primitive" structure since it is compiled. I would assume that a compiler would be designed by now to make all the {,/,+ obsolete?

"C" is not a new programming language, so I would have expected the compilers, by this time, to have evolved to remove these "formatting" landmarks as unneccessary. (Yes I recognize the "//" as begin comment. and the ++ must identify the that the preceeding name is a function. I may be answering my own question!)

Code:
switch(TestCondition)
{
    case 1:
       TestCondition++; //increment by one
       break;
    case 2:
       TestCondition++; //increment by one
       goto case 1; //move the control to the first case, having the effect of increasing by two
    case else:
       break; //Do nothing 
}

And I was thinking how nice it would be to have this and I realized it can be done with a bit extra typing:

Code:
Select Case TestCondition
      Case 1 
1        TestCondition=TestCondition+1
     Case 2
         TestCondition=TestCondition+1
         Goto 1
     Case Else
End Select

Note the '1' used as a line number to indicate the jump point for the subsequent case numbers. Still using that ugly goto, but at least we can blame C#/C++ for that. :p
 
Some people simply prefer less verbose languages.

For example compare ending a function in

c#

Code:
}

with vb.net

Code:
End Function

With .net everything is compiled down to intermediate language so it becomes a matter of preference more than anything else when deciding which .net language to use. I mainly use vb.net because I started in VBA. However I can tell you that the switch to c# isn't that bad once you get the hang of it.

By the way ++ is the incrementation of the variable TestCondition by 1.
 
Indeed.

The whole point was to make the coding terse (MWAHAHAHAHA!) so theoretically, a C programmer can type out more lines faster.

There is nothing stopping you from writing a compiler that will require the following format:

Code:
Switch(TestCondition)
Begin
    Case 1
        Begin
            TestCondition++
         End
    Case 2
        Begin
            TestCondition++
            Goto Case 1
         End
End

In fact, this would be a rough representative of Ada coding, which The_Doc_Man uses at his work. I do not know the specific syntaxes, but you get the idea. The principle behind the differences in formatting is this:

{} is just same as Begin/End; we're just using a different convention.

The reason why I laughed because having written in C#, I can tell you that I type slower because I have to reach for exotic keys more often (e.g. {}, [], ||, \\ and so forth) where I find it faster to just type out "Begin/End" and still accomplish basically same thing. But there are some good things such as this:
Code:
TestCondition++;
Return True;

This is much faster than VB/VBA:
Code:
TestConditon=TestCondition+1
MyFunction = True

So it's not really a 'primitive' structure but in fact a convention for syntaxes.

PS: I should also note that the convention isn't strictly for syntaxes only. As you may notice, all statements in C must end with ; whereas VBA uses a line feed/carriage return to denote a single statement. So you could do this:
Code:
TestCondition++; Return;

But not this:
Code:
TestCondition=TestCondition + 1 End Function

At end of the day, all programming languages are same in the sense that they get resolved into machine code, so once you understand the theory behind the programming languages, it's just a matter of learning the correct syntaxes for a given language.
 
Last edited:
The reason why I laughed because having written in C#, I can tell you that I type slower because I have to reach for exotic keys more often (e.g. {}, [], ||, \\ and so forth) where I find it faster to just type out "Begin/End" and still accomplish basically same thing.

This is a very interesting point.

There is a school of thought where the more abstract the syntax is, the more advanced it is. But nowadays we are seeing a shift from this. Programmers are having to shift closer to the user as usability becomes more and more of a priority. (Web based checkout systems are a great example).

In this respect languages like vb.net/vba are better because the coder is using syntax that actually means something in real life rather than an abstract character like '{'.

Design models like Object Orientation are specifically geared to enhancing usability. Both readability and structure of code that relate to real world models are an integral part of this.
 
Indeed.

If you take look at some newer programming languages, we can see this at work.

Take python. It did something entirely different. It used whitespaces for begin/end.

Compare those two similar procedures, in C and Python respectively:

foo function in C with K&R indent style:
Code:
void foo(int x) {
    if (x == 0) {
        bar();
        baz();
    } else {
        qux(x);
        foo(x - 1);
    }
}

foo function in Python:
Code:
def foo(x):
    if x == 0:
        bar()
        baz()
    else:
        qux(x)
        foo(x - 1)

Note how Python doesn't use {} or even a End If; it's denoted by whitespaces, meaning this format, which is legal in both C and VBA would be illegal in Python:
Code:
If A Then
B
Else
C
End If

I don't think abstract syntax means advanced programming; it is important to consider that C was originally written to be as flexible as possible, which is why it was so powerful. But the flexibility also made it very easy to shoot yourself in the foot. On the other hand, you would have much difficulty passing a pointer to a certain object to a function that wasn't expecting so and so object in VB, but this is quite doable in C.
 
Banana: Thanks. Your explanation has been very helpful. My experience in programming has been somewhat limited and I don't have much opportunity to discuss esoteric stuff with programmers.
 
Looks like I have to pass along some ancient history.

Back in the 1960s, Bell Laboratories was already a prime research center for computer work. (Still is, by the way.) The PDP-11 computer was being tested as the basis for some long-haul electronic switching using a dedicated, UNIX-like operating system. For a language, they didn't have C (much less C++ or C#) so they used assembler. If you haven't played with assembler, you know what a stickler for details it really is.

Anyway, some smart guys started playing with assembler macros to make a "pseudo compiler" that used PDP-11 syntax and instruction sets. Ready for this?

MOV Z, (R1)+
MOV -(R2), A

These are legit instructions in PDP-11 assembler, the auto-increment and auto-decrement addressing modes, where the pointer is the named register. So that funky post-increment and pre-decrement pointer addressing comes from the PDP-11 hardware. (Works on a VAX, too.) The syntax of the assembler became at least partly the syntax of the pseudo-compiler. The assembler was their "A" version of everything. The macro version was called "B" and was pretty good for starters. But it had limits. So they decided to take the next step, a true (if simple-minded) compiler that was tied to the hardware. They had used "B" already so called this one "C" - and it stuck.

The primitive structure is because they had to write their own compiler and they took short-cuts. After all, who wants to build your own compiler? That is also why you have to be so careful in original C. Those shortcuts included a blithe, blissful ignorance of data type mixing. Which is why later, a useful tool was developed called LINT - used to syntax-check C code for abominations of data-type mixing.

After a while, it became possible to apply better technology to make a C compiler less primitive - but they couldn't take it TOO far. Too much code had been written for the AT&T switching network. So what got built was better and more sophisticated, but it had to allow for backwards compatibility. C++, C#, and several other variants (including Java, if you look at it closely enough) now follow that original primitive syntax. But it all started with a PDP-11 assembler and a big bunch of macros.
 
In the 1970s C was a huge leap from BASIC and more compact than RPG or APL. It is also like a shorthand version of COBOL which is VERY wordy.
All these languages are way better then having to learn assembly languages though :eek:
 
Last edited:
Wow, I thought BASIC came after C, but apparently it's five years older than C (1964 vs. 1969.

You learn something new everyday.
 
BASIC worked great on my Timex Sinclair ZX81... 16K RAM and 3.25mhz clock speed... still have it and the Tandy tape drive to go with it!
 
The only reason I dislike C is that it retains too many remnants of its loosely structured origins. I prefer languages that more robustly attend to type compatibility issues. They help you in the long run even if they are a pain in the toches in the short run.

Modern C variants have addressed this, but often you need to invoke a compiler option to assure you have the "strict type checking" enabled.

There is also the issue of readability. If compactness were the only goal, we would all be writing in APL. {shiver}
 
Doc Man, you never cease to amaze :D You just answered a question I'd never thought to ask but has been tickling the back of my brain for years :)
 
Yeah, can't say I am really fond of languages that derives from C's style. When there's so many different punctuation marks, it can get easy to lose the track, and I think it tends to create too much whitespace (though K&R indenting can help a bit but it's still too much).

This is why I generally find myself leaning toward Basic, Python and Ada (wouldn't mind learning it... the trouble is finding a need for it...).



And just for fun.... allow me to introduce everyone to a bit more estoric language... This is a function for hello, world!... you know how it goes.

Code:
++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.

Yes, it will actually print out 'Hello, world!'. Learn more about it at your friendly local wiki article on [Censored]
 
Holy guacamole! OK, that ... BF language is a near pure example of a finite automaton style of programming. Tends to be a bit too abstruse for my taste-buds. I've got enough trouble with simple assembler. But I suppose it COULD be worse. After all, I could have to program an IBM 650 drum system. (Pre-assembler days, and no memory beyond a couple of registers. Can you say "Nightmare?" I knew you could...)
 

Users who are viewing this thread

Back
Top Bottom