The C Killer

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

Rysto
Posts: 1460
Joined: Wed Mar 21, 2007 4:07 am UTC

Re: The C Killer

Postby Rysto » Fri Aug 27, 2010 10:18 pm UTC

Xeio wrote:Var, why is it there? What does it do other than needlessly add a keyword? I can only assume something about making writing the compiler easier?

Quite possible. C is plagued by the fact that its grammar is inherently ambiguous. For example:

f(a);

might call a function called "f" or define a variable called a with type f. The var keyword would resolve that ambiguity.

cogman
Posts: 112
Joined: Sun May 18, 2008 2:17 pm UTC

Re: The C Killer

Postby cogman » Fri Aug 27, 2010 10:57 pm UTC

Rysto wrote:
Xeio wrote:Var, why is it there? What does it do other than needlessly add a keyword? I can only assume something about making writing the compiler easier?

Quite possible. C is plagued by the fact that its grammar is inherently ambiguous. For example:

f(a);

might call a function called "f" or define a variable called a with type f. The var keyword would resolve that ambiguity.

No, not really. f(a); will always call a function. The only time it will do something different is if you have something like int f(a); in which case it will always create a variable f with value a. The difference being the type identifier before the parenthesis.

That is the beauty of c. it is really quite succinct.

I really like the C syntax myself. I don't think it could really be improved upon. Though, I must say, C# has a pretty nice set of features. I wouldn't mind a few of them being given to C (IE reflection.)

User avatar
Sc4Freak
Posts: 673
Joined: Thu Jul 12, 2007 4:50 am UTC
Location: Redmond, Washington

Re: The C Killer

Postby Sc4Freak » Fri Aug 27, 2010 11:31 pm UTC

I don't know about C, but C++ certainly does have these problems. C might have these problems too, but I don't have enough experience to know offhand.

Code: Select all

struct Foo
{
    Foo(int x) { cout << "struct Foo" << endl; }
};

void Foo(int x) { cout << "void Foo" << endl; }

int main()
{
    Foo(1); // ?
}

And another subtle problem:

Code: Select all

int main()
{
    int x;
    int y();
    int z = int();
}

x is an uninitialized int. y is the name of a function which takes no arguments and returns int. z is an integer which has been default-initialized (which is 0 for integers).

Rysto
Posts: 1460
Joined: Wed Mar 21, 2007 4:07 am UTC

Re: The C Killer

Postby Rysto » Sat Aug 28, 2010 12:02 am UTC

cogman wrote:No, not really. f(a); will always call a function. The only time it will do something different is if you have something like int f(a); in which case it will always create a variable f with value a. The difference being the type identifier before the parenthesis.

You lose:

Code: Select all

typedef int f;
f(a);

User avatar
TheChewanater
Posts: 1279
Joined: Sat Aug 08, 2009 5:24 am UTC
Location: lol why am I still wearing a Santa suit?

Re: The C Killer

Postby TheChewanater » Sat Aug 28, 2010 12:46 am UTC

Code: Select all

#define f(foo) int foo = 10;

int main ()
{
  
(a);
ImageImage
http://internetometer.com/give/4279
No one can agree how to count how many types of people there are. You could ask two people and get 10 different answers.

cogman
Posts: 112
Joined: Sun May 18, 2008 2:17 pm UTC

Re: The C Killer

Postby cogman » Sat Aug 28, 2010 2:39 am UTC

Ok, lets address these "issues" one at a time.

Sc4Freak wrote:I don't know about C, but C++ certainly does have these problems. C might have these problems too, but I don't have enough experience to know offhand.

Code: Select all

struct Foo
{
    Foo(int x) { cout << "struct Foo" << endl; }
};

void Foo(int x) { cout << "void Foo" << endl; }

int main()
{
    Foo(1); // ?
}

The main call HAS to go to the function Foo. It doesn't have a variable name following Foo. Even if it was Foo something(8); It seems pretty clear that that goes to a struct while the other will always go to a function. Not really ambiguous.


And another subtle problem:

Code: Select all

int main()
{
    int x;
    int y();
    int z = int();
}

x is an uninitialized int. y is the name of a function which takes no arguments and returns int. z is an integer which has been default-initialized (which is 0 for integers).

Not seeing the gotcha. Seems pretty clear as long as you follow the Variable-type variable-name formula, it is pretty easy to tell when something is a function vs a variable/struct.

Rysto wrote:
cogman wrote:No, not really. f(a); will always call a function. The only time it will do something different is if you have something like int f(a); in which case it will always create a variable f with value a. The difference being the type identifier before the parenthesis.

You lose:

Code: Select all

typedef int f;
f(a);

No, I don't. You just typedefed an f to be the same as an int. That is not valid c code (unless there is a function f). f variablename(a); I really don't see why this is so difficult.

TheChewanater wrote:

Code: Select all

#define f(foo) int foo = 10;

int main ()
{
  f (a);
}
 

Ok, THIS is valid code and a valid concern. It exemplifies why macros can be evil. That being said, if you had a function like int f(a) { }; in your code, an error would be thrown due to the macro over writing it (assuming the macro is before the code).

The ambiguity is that of inexperience with c. Only in the odd case of a macro can things get harry, all the other examples above aren't really examples of ambiguity, just examples of functions having the same names as structures.

User avatar
Xeio
Friends, Faidites, Countrymen
Posts: 5101
Joined: Wed Jul 25, 2007 11:12 am UTC
Location: C:\Users\Xeio\
Contact:

Re: The C Killer

Postby Xeio » Sat Aug 28, 2010 2:51 am UTC

bieber wrote:
Xeio wrote:Oh well, at least it isn't whitespace significant. I really wish it had C#'s get/set syntax too...
Actually, it seems that it is to an extent, because of the way the compiler auto-inserts semicolons (there may be a way to turn that off, I'm just getting this from skimming the Go page). If you put the opening brace of a control structure on the next line then the compiler automatically inserts a semi-colon before the brace, which of course screws everything up :/
Wait... really? So they just broke the style many programmers use because the syntax sucks (The style we use where I work is braces-on-their-own-line)?

That's both hilarious and terrible, I would have thought google could have done better at creating a programming language...

jareds
Posts: 436
Joined: Wed Jan 03, 2007 3:56 pm UTC

Re: The C Killer

Postby jareds » Sat Aug 28, 2010 4:47 am UTC

cogman wrote:Ok, lets address these "issues" one at a time.

Sc4Freak wrote:I don't know about C, but C++ certainly does have these problems. C might have these problems too, but I don't have enough experience to know offhand.

Code: Select all

struct Foo
{
    Foo(int x) { cout << "struct Foo" << endl; }
};

void Foo(int x) { cout << "void Foo" << endl; }

int main()
{
    Foo(1); // ?
}

The main call HAS to go to the function Foo. It doesn't have a variable name following Foo. Even if it was Foo something(8); It seems pretty clear that that goes to a struct while the other will always go to a function. Not really ambiguous.
If the declaration of the function Foo didn't exist, "Foo(1);" would be a perfectly legal statement constructing an instance of Foo and then discarding it. The resolution of this has nothing to do with what you said. Rather, because struct-names are in a different namespace than everything else for C-compatibility reasons, function Foo shadows struct Foo, so there's no way to refer to struct Foo without using "struct Foo", so there's no way to use the constructor.
cogman wrote:
And another subtle problem:

Code: Select all

int main()
{
    int x;
    int y();
    int z = int();
}

x is an uninitialized int. y is the name of a function which takes no arguments and returns int. z is an integer which has been default-initialized (which is 0 for integers).

Not seeing the gotcha. Seems pretty clear as long as you follow the Variable-type variable-name formula, it is pretty easy to tell when something is a function vs a variable/struct.
That wasn't the best example of the function/variable ambiguity in C++, but it's absurd to say it's easy. Are "a" and "b" function declarations or not? (x and y were declared earlier)

Code: Select all

struct T {
T(int);
};
void foo() {
T a(x);
T b(y);
}

cogman wrote:
Rysto wrote:
cogman wrote:No, not really. f(a); will always call a function. The only time it will do something different is if you have something like int f(a); in which case it will always create a variable f with value a. The difference being the type identifier before the parenthesis.

You lose:

Code: Select all

typedef int f;
f(a);

No, I don't. You just typedefed an f to be the same as an int. That is not valid c code (unless there is a function f). f variablename(a); I really don't see why this is so difficult.
That is perfectly valid code. Parentheses are legal in declarators in C, which is how you can declare, for example, function pointers without using typedef. "int (a);" declares a variable "a" of type "int", as does "f(a)" in Rysto's example. Specifically,
ISO/IEC 9899:1999 wrote:

Code: Select all

6.7.5 Declarators
Syntax
declarator:
    pointer_opt director-declarator
direct-declarator:
    identifier
    ( declarator )
....
That is, you can put any declarator in parentheses. The standard doesn't say anything along the lines of "you can only use parentheses in a declarator when you absolutely have to". The ambiguity of C's grammar is well-known.
cogman wrote:No, not really. f(a); will always call a function. The only time it will do something different is if you have something like int f(a); in which case it will always create a variable f with value a. The difference being the type identifier before the parenthesis.
First of all, you're thinking of C++. "int f(a);" does not create a variable f with value a in C. You make this mistake in your other post.
Second, the fact that it matters whether identifiers are type identifiers or not is the whole point. Sure, "int" always is (technically, it's a type specifier, but we'll ignore that), but "f" might or might not be. In an unambiguous grammar, it wouldn't matter.

bieber
Posts: 223
Joined: Thu Mar 13, 2008 12:13 am UTC

Re: The C Killer

Postby bieber » Sat Aug 28, 2010 5:16 pm UTC

Xeio wrote:
bieber wrote:
Xeio wrote:Oh well, at least it isn't whitespace significant. I really wish it had C#'s get/set syntax too...
Actually, it seems that it is to an extent, because of the way the compiler auto-inserts semicolons (there may be a way to turn that off, I'm just getting this from skimming the Go page). If you put the opening brace of a control structure on the next line then the compiler automatically inserts a semi-colon before the brace, which of course screws everything up :/
Wait... really? So they just broke the style many programmers use because the syntax sucks (The style we use where I work is braces-on-their-own-line)?

That's both hilarious and terrible, I would have thought google could have done better at creating a programming language...


That's what it looks like. I had to re-read that paragraph like three times before I really believed it said what it does.

One caveat. You should never put the opening brace of a control structure (if, for, switch, or select) on the next line. If you do, a semicolon will be inserted before the brace, which could cause unwanted effects. Write them like this


Feel free to look over the rest of their formatting guide. Apparently the standard distribution actually includes a program to format your code the way they like it for you...

User avatar
'; DROP DATABASE;--
Posts: 3284
Joined: Thu Nov 22, 2007 9:38 am UTC
Location: Midwest Alberta, where it's STILL snowy
Contact:

Re: The C Killer

Postby '; DROP DATABASE;-- » Sun Aug 29, 2010 3:45 pm UTC

Carnildo wrote:
'; DROP DATABASE;-- wrote:I would love to see some sort of assembly language that isn't platform-dependent.

That was the original point of C.
Original, yes, but it's so far from that now. Still a much higher level.
poxic wrote:You suck. And simultaneously rock. I think you've invented a new state of being.

zenhack
Posts: 1
Joined: Wed Sep 01, 2010 9:21 pm UTC

Re: The C Killer

Postby zenhack » Wed Sep 01, 2010 9:42 pm UTC

Regarding the var keyword in go, it is a little weird, but it very rarely actually comes up; go has type inference. the 'var s string = ""' mentioned could easily just have been 's := ""'. The only place you really need it is if you're declaring global variables. even then you can group them:

Code: Select all

var (
    a = "a string"
    b = 1234
    c = 1.32
)


The semicolon thing is a little weird. the rule is actually a lexical one; the lexer will insert a semicolon at the end of a line if it would be valid to have one there. For example, This does not break:

Code: Select all

func fn (a, b, c, d, e, f,
    g, h, i, j, k int) {
   ...
}


because putting a semicolon at the end of the first line would not result in a valid statement.

User avatar
TheChewanater
Posts: 1279
Joined: Sat Aug 08, 2009 5:24 am UTC
Location: lol why am I still wearing a Santa suit?

Re: The C Killer

Postby TheChewanater » Thu Sep 02, 2010 3:00 am UTC

If I ever use go again, I think I might do this. Just out of defiance of the strict coding style enforcement.

Code: Select all

func foo (bar, baz
){
  meep (bar);
  mop (baz);
}
 
ImageImage
http://internetometer.com/give/4279
No one can agree how to count how many types of people there are. You could ask two people and get 10 different answers.

User avatar
Xanthir
My HERO!!!
Posts: 5426
Joined: Tue Feb 20, 2007 12:49 am UTC
Location: The Googleplex
Contact:

Re: The C Killer

Postby Xanthir » Thu Sep 02, 2010 3:58 am UTC

Xeio wrote:
bieber wrote:
Xeio wrote:Oh well, at least it isn't whitespace significant. I really wish it had C#'s get/set syntax too...
Actually, it seems that it is to an extent, because of the way the compiler auto-inserts semicolons (there may be a way to turn that off, I'm just getting this from skimming the Go page). If you put the opening brace of a control structure on the next line then the compiler automatically inserts a semi-colon before the brace, which of course screws everything up :/
Wait... really? So they just broke the style many programmers use because the syntax sucks (The style we use where I work is braces-on-their-own-line)?

That's both hilarious and terrible, I would have thought google could have done better at creating a programming language...

All of our (Google's) internal style guides mandate putting the opening brace on the same line as the statement. It's easy to see how we could think "Eh, screw everyone who does it wrong.".

bieber wrote:Apparently the standard distribution actually includes a program to format your code the way they like it for you...

What's wrong with a pretty-printer? Having multiple styles in a language is a bad thing. Having a definitive style to point to as the "official" style is a great thing, because it means that everyone formats their code the same way.
(defun fibs (n &optional (a 1) (b 1)) (take n (unfold '+ a b)))

0xBADFEED
Posts: 687
Joined: Mon May 05, 2008 2:14 am UTC

Re: The C Killer

Postby 0xBADFEED » Thu Sep 02, 2010 2:21 pm UTC

Xanthir wrote:What's wrong with a pretty-printer? Having multiple styles in a language is a bad thing. Having a definitive style to point to as the "official" style is a great thing, because it means that everyone formats their code the same way.

Definitely.
And it's something you have to specify at the language's outset. If you release a language without a definitive style-guide it's too late. Just look at Python. The standard style has been established for quite a while now but it's completely common to have libraries that violate it.

C++ is probably the worst. The project I'm working on right now uses a dozen or so 3rd-party libraries, all with different styles. No matter how consistent you make your own code it always looks sloppy due to the Frankenstein effect of so many different styles. This is something that Java really nailed (as have most languages since). Even with all the things I dislike about Java there's something satisfying about being able to plug-in a bunch of libraries and know they're all going to fit in stylistically with the rest of the code-base. Codifying some elements of style as syntax seems completely reasonable to me.

User avatar
TheChewanater
Posts: 1279
Joined: Sat Aug 08, 2009 5:24 am UTC
Location: lol why am I still wearing a Santa suit?

Re: The C Killer

Postby TheChewanater » Thu Sep 02, 2010 6:45 pm UTC

Every C++ library I use uses the same style. Although I have seen some obscure libraries use Java-style or MS-style, I very rarely encounter those in C++.

Code: Select all

MyClass
myobject
my_method ()


Namespaces, however, vary greatly.

Of course, it doesn't enforce this. But that's good, so people can use their own styles if they absolutely must.
ImageImage
http://internetometer.com/give/4279
No one can agree how to count how many types of people there are. You could ask two people and get 10 different answers.

squareroot
Posts: 548
Joined: Tue Jan 12, 2010 1:04 am UTC
Contact:

Re: The C Killer

Postby squareroot » Fri Sep 03, 2010 12:19 am UTC

0xBADFEED wrote:This is something that Java really nailed (as have most languages since). Even with all the things I dislike about Java there's something satisfying about being able to plug-in a bunch of libraries and know they're all going to fit in stylistically with the rest of the code-base. Codifying some elements of style as syntax seems completely reasonable to me.


...there's a standard Java style guide?
Considering that I often find myself using 3 or 4 different formatting techniques - mostly based on the complexity of the block - in one class, I'm sure anything I've written would violate it beyond recognition. :-P
<signature content="" style="tag:html;" overused meta />
Good fucking job Will Yu, you found me - __ -

0xBADFEED
Posts: 687
Joined: Mon May 05, 2008 2:14 am UTC

Re: The C Killer

Postby 0xBADFEED » Fri Sep 03, 2010 1:02 am UTC

squareroot wrote:...there's a standard Java style guide?

http://www.oracle.com/technetwork/java/codeconvtoc-136057.html

Carnildo
Posts: 2023
Joined: Fri Jul 18, 2008 8:43 am UTC

Re: The C Killer

Postby Carnildo » Fri Sep 03, 2010 8:03 am UTC

0xBADFEED wrote:C++ is probably the worst. The project I'm working on right now uses a dozen or so 3rd-party libraries, all with different styles. No matter how consistent you make your own code it always looks sloppy due to the Frankenstein effect of so many different styles.

Only different styles per library? Luck you -- I'm dealing with a codebase that uses multiple styles per file -- and sometimes multiple styles in a single function.

whydowedothis
Posts: 2
Joined: Thu Sep 09, 2010 1:52 am UTC

Re: The C Killer

Postby whydowedothis » Thu Sep 09, 2010 2:02 am UTC

I don't think C will die any time soon. Mostly because it's such a popular language and it's really low level, so a lot of companies that make little gadgets (like where I work) will use them. Actually, my cousin is majoring in comp sci right now, and their school start off their kids with C.

User avatar
thoughtfully
Posts: 2253
Joined: Thu Nov 01, 2007 12:25 am UTC
Location: Minneapolis, MN
Contact:

Re: The C Killer

Postby thoughtfully » Thu Sep 09, 2010 1:21 pm UTC

Here's another one I had forgotten about:
http://en.wikipedia.org/wiki/Cyclone_%2 ... anguage%29
Image
Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away.
-- Antoine de Saint-Exupery

User avatar
sourmìlk
If I can't complain, can I at least express my fear?
Posts: 6393
Joined: Mon Dec 22, 2008 10:53 pm UTC
Location: permanently in the wrong
Contact:

Re: The C Killer

Postby sourmìlk » Sat Sep 18, 2010 1:42 am UTC

As a n00b programmer who just got his first programming job (in C#), my opinion means nothing, but here it is anyways:

alright, as for c's syntax being ambiguous: yes, it is, but part of the programmer's job is to make their code sensible. That's why a decent coder wouldn't have

Code: Select all

float foo = 0.0f;

class Foo
{
    int foo(foo)
    {
         return foo;
    }
};

int foo(Foo foo)
{
    for(int newFoo = foo.foo(foo); newFoo < foo; foo += newFoo)
    {
        foo*foo = foo.foo(newFoo);
    }

    return foo.foo(foo);
}


I had too much fun writing that :/

god damn, I just can't help myself.

also yes I know there are a million reasons that won't compile, I'm just trying to make a point here.
Terry Pratchett wrote:The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it.

ishkabible
Posts: 5
Joined: Fri Sep 17, 2010 11:34 pm UTC

Re: The C Killer

Postby ishkabible » Sat Sep 18, 2010 2:35 am UTC

A hacked-up Lua, with strong typing, integer operations, etc might make a good replacement for low-level C stuff.


i like this idea, i have thought about making a translator from a lua like language to c++. i would add new and delete, make tables vectors and maps (so instead of using tables you would use on or the other), give it strong typing and class declarations (i would copy the LuaBind syntax for this as a lot of people are already filmier with it). of course pointers would also have to be added, basically just control flow would look the same, other things would just be minor differences, you even give functions the same syntax by using the type of the return statement to conclude it's return type. the semi colon thing could help out beginners quite a bit (but also cripple them as well :( ) . you could make the main the top level function so you would not need a main function like you do in c++ might look something like

Code: Select all

function f(int x, int y)
     return x+y
end

f(10,10)



might look something like

Code: Select all

int f(int x,int y) {
     return x+y;
}

int main() {
     f(10,10);
}


metamorphosis
Posts: 20
Joined: Sun Sep 26, 2010 8:56 am UTC

Re: The C Killer

Postby metamorphosis » Sun Sep 26, 2010 9:10 am UTC

Python, if it was machine-code-compiled.


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 9 guests