Simplicity has a future

R

Richard Heathfield

Ian Collins said:

(Withdrawn. That was a misreading of my own code.)
Hang on a minute, you're posting code that won't compile as C99 and
asking the audience to convert it to C++? Given your original rules, it
can't be morphed to C99, let alone C++.

The claim I was addressing, which was originally made by Tomas, is that "I
can copy-paste your C code and compile it as C++". Are you claiming that
C90 code is not C code unless it is also C99 code?
I'm sure I could turn my C99 compiler back to C90, but why bother?

Which C99 compiler are you using? If the answer is "gcc", why bother to
claim that you have a C99 compiler? (gcc is /not/ a conforming C99
compiler.)
Maybe if you can explain the usefulness to mankind of the code, I might
give it a go....

Like much code posted here, its usefulness to mankind is that it illustrates
a point about the language. In this case, the point is that the claim "I
can copy-paste your C code and compile it as C++" is demonstrably false.
 
R

Richard Heathfield

Ian Collins said:

Don't bother - it isn't an error.
One good reason for passing C though a C++ compiler:

"/tmp/x.c", line 16: Error: Cannot use const char[82] to initialize
char[81].

Oh yes you can. Not in C++, true - but this is comp.lang.c, not
comp.lang.c++, and the code I posted is C, not C++.

Read the code more carefully. I think you jumped to a false conclusion.
 
G

Guest

Richard said:
Harald van D?k said:


It is.


It is certainly silly to assume that everyone has started using C99 even
though almost nobody has a conforming C99 compiler.

I didn't assume that, nor did I claim to.
Go on then.

Your original program:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define a(b) #b
#define SPACE ' '

int main(void)
{
const char *asm = a(asm);
const char *protected = a(protected);
const char *using = a(using);
const char *b[28] = {0};
char title__of__the__program[81] =
"C to C++ Translation\nONLY extra casts and renaming"
" of variables are allowed.\n\n\n\n\n";
auto i = strlen(protected) -
(sizeof SPACE //* we need a space\
in here somewhere */ sizeof (int
) + strlen(asm) + strlen(using));

if(strcmp(asm, "asm") != 0 || strcmp(protected, "protected") != 0 ||
strcmp(using, "using") != 0)
{
puts("Error!");
return EXIT_FAILURE;
}

b[0] = asm + 1;
b[1] = b[0] - 1;
b[2] = protected + 8;
b[4] = b[2] - 8;
b[5] = b[4] + 1;
b[6] = b[5] + 1;
b[7] = using + 4;
b[8] = b[5];
b[9] = b[0] - 1;
b[10] = b[0] + 1;
b[11] = b[10];
b[12] = b[2] - 4;
b[13] = b[12] - 3;
b[14] = b[0];
b[16] = b[2];
b[17] = b[16] - 6;
b[19] = b[7] - 1;
b[20] = b[2] - 6;
b[21] = b[20] + 1;
b[23] = b[7] - 4;
b[24] = b[23] + 1;
b[25] = b[2] - 4;
b[27] = b[25] + 1;

printf("%.*s\n",
(int)sizeof title__of__the__program,
title__of__the__program);

while(i < sizeof b / sizeof b[0])
{
if(b != NULL)
{
if(i == sizeof(char) / sizeof SPACE ||
i == sizeof b / sizeof b[0] - 1)
{
putchar(toupper((unsigned char)
b[sizeof(char)/sizeof SPACE]));
}
else
{
putchar(tolower((unsigned char)
b[sizeof(char)/sizeof SPACE]));
}
}
else
{
putchar(SPACE);
}
i += strlen(protected) - (strlen(asm) + strlen(using));
}

putchar('.' * sizeof SPACE / sizeof(int));
putchar('\n');

return 0;

}

The changes I made (line numbers refer to the original code, not the
modified code):

Add #include <ctype.h> before line 4.

Change ' ' to ((int) ' ') on line 6.

Add before line 8:
#define asm renamed_asm
#define protected renamed_protected
#define using renamed_using

Change title__of__the__program to title on lines 14, 54 and 55.

Change on lines 14 and 15:
"C to C++ Translation\nONLY extra casts and renaming"
" of variables are allowed.\n\n\n\n\n"
to
{ 'C', ' ', 't', ...
.... '.', '\n', '\n', '\n', '\n', '\n' }

Change auto to auto int on line 17.

Change // to / / on line 18.

Please tell me if any part of this, according to you, changes the
essential character, or makes it invalid C90, or if this isn't enough
to make it valid C++.
 
I

Ian Collins

Richard said:
Ian Collins said:




The claim I was addressing, which was originally made by Tomas, is that "I
can copy-paste your C code and compile it as C++". Are you claiming that
C90 code is not C code unless it is also C99 code?
No, but I still find it amusing that the example can't be compiled as
(current) standard C. There must be some humour in that somewhere.
Which C99 compiler are you using? If the answer is "gcc", why bother to
claim that you have a C99 compiler? (gcc is /not/ a conforming C99
compiler.)
Never use it. I use Sun Studio, which is approaching C99 conformance.
The Solaris headers have also migrated to C99.
Like much code posted here, its usefulness to mankind is that it illustrates
a point about the language. In this case, the point is that the claim "I
can copy-paste your C code and compile it as C++" is demonstrably false.
Neither of the compliers I have will compile it, gcc or Studio. Studio
barfs over asm.
 
R

Richard Heathfield

Harald van D?k said:

Please tell me if any part of this, according to you, changes the
essential character, or makes it invalid C90, or if this isn't enough
to make it valid C++.

I'm impressed. You did a lot better than the last person to try. But you had
to hack the code around some - including one very tedious change to get
around the string literal problem.

You will, of course, recognise that the code was a highly compressed, and
thus artificial, illustration of portability problems between C and C++.
Unfortunately, in normal production code the problems are rather more
thinly spread out, and harder to spot. It is certainly true that some
people will simply mv foo.c foo.cpp, and then trust in God and their
compiler. I cannot help but see the risks in such an approach.
 
R

Richard Heathfield

Ian Collins said:
Never use it. I use Sun Studio, which is approaching C99 conformance.

Okay, fine - just as long as you realise that most people /don't/ have a C99
compiler.
The Solaris headers have also migrated to C99.
Neither of the compliers I have will compile it, gcc or Studio. Studio
barfs over asm.

gcc compiles it just fine here, using:

-W -Wall -ansi -pedantic -Wformat-nonliteral -Wcast-align -Wpointer-arith
-Wbad-function-cast -Wmissing-prototypes -Wstrict-prototypes
-Wmissing-declarations -Winline -Wundef -Wnested-externs -Wcast-qual
-Wshadow -Wconversion -Wwrite-strings -Wno-conversion -ffloat-store -O2

If Studio barfs over asm, it's not a conforming C implementation, C90 /or/
C99. asm is a perfectly normal C identifier in user namespace.
 
I

Ian Collins

Richard said:
Ian Collins said:



Don't bother - it isn't an error.

One good reason for passing C though a C++ compiler:

"/tmp/x.c", line 16: Error: Cannot use const char[82] to initialize
char[81].


Oh yes you can. Not in C++, true - but this is comp.lang.c, not
comp.lang.c++, and the code I posted is C, not C++.

Read the code more carefully. I think you jumped to a false conclusion.
OK, it may be legal C, but it ain't pretty.

#include <stdio.h>

int main(void)
{
char title__of__the__program[81] =
"C to C++ Translation\nONLY extra casts and renaming"
" of variables are allowed.\n\n\n\n\n";

printf( "%s\n", title__of__the__program );

return 0;
}

I'd rather have the compiler barf.
 
R

Richard Heathfield

Ian Collins said:
Richard said:
Read the code more carefully. I think you jumped to a false conclusion.
OK, it may be legal C, but it ain't pretty.

#include <stdio.h>

int main(void)
{
char title__of__the__program[81] =
"C to C++ Translation\nONLY extra casts and renaming"
" of variables are allowed.\n\n\n\n\n";

printf( "%s\n", title__of__the__program );

But my code very definitely did not do that. If you want to introduce
arbitrary errors into your copy of the code, that's up to you, but the
error was not present in /my/ code.
 
I

Ian Collins

Richard said:
Ian Collins said:

Richard said:
Read the code more carefully. I think you jumped to a false conclusion.

OK, it may be legal C, but it ain't pretty.

#include <stdio.h>

int main(void)
{
char title__of__the__program[81] =
"C to C++ Translation\nONLY extra casts and renaming"
" of variables are allowed.\n\n\n\n\n";

printf( "%s\n", title__of__the__program );


But my code very definitely did not do that. If you want to introduce
arbitrary errors into your copy of the code, that's up to you, but the
error was not present in /my/ code.
It might not be in your code, but the very fact that the code is legal C
makes me cringe and carry on checking my C code with my C++ compiler.
 
R

Richard Heathfield

Ian Collins said:
Richard said:
Ian Collins said:
#include <stdio.h>

int main(void)
{
char title__of__the__program[81] =
"C to C++ Translation\nONLY extra casts and renaming"
" of variables are allowed.\n\n\n\n\n";

printf( "%s\n", title__of__the__program );


But my code very definitely did not do that. If you want to introduce
arbitrary errors into your copy of the code, that's up to you, but the
error was not present in /my/ code.
It might not be in your code, but the very fact that the code is legal C
makes me cringe

It shouldn't do. The char arr[3] = "123"; way of initialising an array of
characters, without null-terminating it, is a well-known C idiom.
and carry on checking my C code with my C++ compiler.

That approach is likely to bite you one day, if it hasn't silently done so
already.
 
M

Marc Boyer

Le 30-05-2006 said:
A recent article on CNN described the problem that phone makers
are facing.

(http://www.cnn.com/2006/TECH/biztech/05/29/cellphone.usability.ap/index.html)

Most people are rejecting the new "supercharged"
cell phones because they just do not want to learn the
incredible complex menus and stuff that you need to use them.

The new phones are just too complicated to use.

The same thing is happening with programming languages too.

With a big difference: most 'programming languages' users
are professionnals, and a (good) professional is ready to
learn if it can increase its productivity.

Well, that is the same stupid war for text editors:
NotePad, Gedit, Vi[m], [X]emacs...
Its takes time to learn how to use Vi/emacs but
once you knows it, you write code more efficiently than
with NotePad.
But, if you want to read the all documentation
of vi before writting your first text, yes, chose NotePad.
Extremey complicated languages like C++ are loosing ground to more
simpler languages.

Sure ?
People get tired of trying to learn a
monster language where learning the language gets in the way
of the basic needs of the user. You use a language to do something,
and when you have to learn too much stuff or the learning curve
is too steep, a simpler language wins.

OK, try this simple test: write a code that read a text line,
without any assumption on its maximal size. Is the C solution
really simplier that the C++ one ?

Marc Boyer
 
R

Richard Heathfield

Marc Boyer said:
OK, try this simple test: write a code that read a text line,
without any assumption on its maximal size. Is the C solution
really simplier that the C++ one ?

No, not really. My C solution is very simple and very short: just 23 lines,
divided as follows:

Function declarator lines: 1 \
Blank lines: 1 |
Declaration lines: 5 \ sub-total: 16 lines, in what you might
Opening brace lines: 4 / call "furniture" code - stuff that you
Closing brace lines: 4 | don't really need to worry about.
Return statement: 1 /

That leaves seven more lines. Two of those lines comprise calls to an
existing function (of my own devising, which was not written with your task
in mind, but which was very handy anyway). Another is a loop control
statement (no surprise there). The other four lines are a couple of ifs and
a couple of simple assignments.

Anyone C programmer having trouble understanding the code would probably
have trouble understanding "Winnie the Pooh".

My C++ solution, however, is even simpler!

extern "C" {
#include "my_c_solution's_header.h"
};

and linking in <insert library name here>.
 
V

Vladimir Oka

Richard said:
Anyone C programmer having trouble understanding the code would probably
have trouble understanding "Winnie the Pooh".

You underestimate the depths of Winnie the Pooh!
 
M

Marc Boyer

Le 31-05-2006 said:
No, not really. My C solution is very simple and very short: just 23 lines,
divided as follows:

Function declarator lines: 1 \
Blank lines: 1 |
Declaration lines: 5 \ sub-total: 16 lines, in what you might
Opening brace lines: 4 / call "furniture" code - stuff that you
Closing brace lines: 4 | don't really need to worry about.
Return statement: 1 /

That leaves seven more lines. Two of those lines comprise calls to an
existing function (of my own devising, which was not written with your task
in mind, but which was very handy anyway).

Which one ?
Another is a loop control
statement (no surprise there). The other four lines are a couple of ifs and
a couple of simple assignments.

Anyone C programmer having trouble understanding the code would probably
have trouble understanding "Winnie the Pooh".

What is your definition of "C programmer" ?
When did you met C beginner last time ?
Or wich version of "Winnie the Pooh" did you have looked ?
My C++ solution, however, is even simpler!

extern "C" {
#include "my_c_solution's_header.h"
};

and linking in <insert library name here>.

So C++ is simpler than C: that is what I wanted to illustrate ;-)

Marc Boyer
 
R

Richard Heathfield

Marc Boyer said:
Le 31-05-2006 said:
[Any] C programmer having trouble understanding the code would probably
have trouble understanding "Winnie the Pooh".

What is your definition of "C programmer" ?

Someone who understands C.
When did you met C beginner last time ?

Have you read this newsgroup recently?
So C++ is simpler than C: that is what I wanted to illustrate ;-)

If you say so. But C is simpler than C++, *next* time, because *next* time I
need to use such a function, I can do this:

#include "my_c_solution's_header.h"

and link in the appropriate library, without bothering with all that extern
"C" nonsense. We call it "code re-use", and I recommend it to you.
 
R

Richard Bos

Ben Pfaff said:
Yes, and at the same time it screws up all the comments that use
"new" as an English word.

But C++ programmers aren't stupid enough to need comments.

Richard
 
M

Marc Boyer

Le 31-05-2006 said:
Le 31-05-2006 said:
[Any] C programmer having trouble understanding the code would probably
have trouble understanding "Winnie the Pooh".

What is your definition of "C programmer" ?

Someone who understands C.

All the C99 standard ?
Have you read this newsgroup recently?

I read very quicky... There are everyday too much
messages for me.
If you say so. But C is simpler than C++, *next* time, because *next* time I
need to use such a function, I can do this:

#include "my_c_solution's_header.h"

and link in the appropriate library, without bothering with all that extern
"C" nonsense. We call it "code re-use", and I recommend it to you.

Did you see my ";-)" ? This was humor, joke, not an argument.
I am not going to argument what first was an example (reading one line)
and second a joke ("extern C" makes C++ better).

I do not arge that C++ is better than C, neither the opposite.
But I desagree with "simpler is better". Turing-like langage would
have been the better solution with such argument. There is a
'good compromise' to find between the complexity of the tool and
the complexity of the problems.

Marc Boyer
 
R

Richard Heathfield

Marc Boyer said:
Le 31-05-2006 said:
Le 31-05-2006, Richard Heathfield <[email protected]> a écrit :
[Any] C programmer having trouble understanding the code would probably
have trouble understanding "Winnie the Pooh".

What is your definition of "C programmer" ?

Someone who understands C.

All the C99 standard ?

C90 is still C. Anyone who understands C90 sufficiently well is not likely
to struggle with C99.
I read very quicky...

....and type too quicky too.
I do not arge that C++ is better than C, neither the opposite.

Good, because the answer to the question depends on what one means by
"better".
But I desagree with "simpler is better".

Good, because it depends on what you mean by "simpler".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,786
Messages
2,569,626
Members
45,328
Latest member
66Teonna9

Latest Threads

Top