Re: Simplicity has a future



Harald van Dijk wrote:
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’
c.c:20: error: expected ‘,’ or ‘;’ before ‘)’ token
c.c:64: error: implicit declaration of function ‘toupper’
c.c:69: error: implicit declaration of function ‘tolower’

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

My C compiler gave several screens of errors and warning before giving up.

--
Ian Collins.
.



Relevant Pages

  • Re: Simplicity has a future
    ... const char *protected = a; ... (sizeof SPACE //* we need a space\ ... in here somewhere */ sizeof (int ... It's perfectly legal C90. ...
    (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)
  • Re: Why is C still being used instead of C++
    ... > I compile them into a.obj and b.obj. ... int fprintf(FILE * restrict stream, const char * restrict format, ...); ...
    (alt.comp.lang.learn.c-cpp)
  • cannot pass objects of non-POD type
    ... void Write(const char* msg, const char* msg2, ...) ... When i compile this code i get following compilation warning. ... sam.cpp: In function `int main': ...
    (comp.lang.cpp)