Re: Simplicity has a future



Harald van D?k said:

Richard Heathfield wrote:
Tomás said:

If I copy-paste your C code, and then go through it for:

(a) Use of C++ keywords

(b) Inconsistencies between C and C++


And then compile it, I guarantee that the C++ version won't be slower
or require more memory.

But the C++ version may well be a very different program as a result of
your changes, so the comparison will no longer be valid.

But if you want to give it a go, please feel free. In fact, if you like,
I won't ask you to make the following C program a faster, tighter C++
program. I will merely ask you to change it sufficiently to make it legal
C++ whilst leaving its essential character untouched, so that comparison
between the C and C++ object file translations will be meaningful. The
program must, of course, remain valid C. To leave its essential character
unchanged, I must ask that you observe the following rules:

1) the program must remain a valid C program.
2) you may refactor recursive main() - so I
didn't bother to use one.
3) you may rename identifiers, including function
names.
4) you may add casts and modify whitespace.
5) you can't change the program in any other way.
6) the output of the program must remain unchanged, of course.


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

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

return 0;

}

c.c: In function ?main?:
c.c:17: error: type defaults to ?int? in declaration of ?i?

That's not an error. It's perfectly legal C90.

c.c:20: error: expected ?,? or ?;? before ?)? token

I forgot a line-splice \ when splitting the line for Usenet. Sorry about
that.

c.c:64: error: implicit declaration of function ?toupper?
c.c:69: error: implicit declaration of function ?tolower?

Those aren't errors (in C90) either. Both of those functions return int, and
neither is variadic.

That's compiling as C, not as C++.

I'll take the hit on the splice, which is trivially fixable. The others are
only errors in C99, which doesn't count.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
.



Relevant Pages

  • Re: Simplicity has a future
    ... And then compile it, I guarantee that the C++ version won't be slower or ... const char *protected = a; ... (sizeof SPACE //* we need a space\ ... in here somewhere */ sizeof (int ...
    (comp.lang.c)
  • Re: Simplicity has a future
    ... I guarantee that the C++ version won't be slower or ... const char *protected = a; ... (sizeof SPACE //* we need a space\ ... in here somewhere */ sizeof (int ...
    (comp.lang.c)
  • Re: Simplicity has a future
    ... And then compile it, I guarantee that the C++ version won't be slower or ... const char *protected = a; ... (sizeof SPACE //* we need a space\ ... in here somewhere */ sizeof (int ...
    (comp.lang.c)
  • [PATCH 72/79] sysdev: Pass the attribute to the low level sysdev show/store function
    ... return ppe_show_temp(sysdev, buf, 32); ... int val, rc; ... const char *buf, size_t count) ... container_of(dev, struct memory_block, sysdev); ...
    (Linux-Kernel)
  • Re: A code from M. Jerzy Buczynski
    ... #ifdef HAVE_ARPA_INET_H ... extern int h_errno; ... {const char **argvec; ... /* Socket opened saved. ...
    (comp.bugs.misc)