r/cprogramming •

Solve "The C Programming Language" exercises in the browser with auto-graded tests

[deleted] — view deleted post

0 Upvotes

23 comments sorted by

1

u/thondat 3d ago

your site's 'house solutions' need some serious QA. e.g. 7-1:

  while ((c = getchar()) != EOF)
    if (strcmp(name, "lower") == 0)
      putchar(tolower(c));
    else if (strcmp(name, "upper") == 0)
      putchar(toupper(c));
    else
      putchar(c);

the two strcmps are executed for each input char. this is unnecessary and abysmal. once before the main loop is enough, as name never changes. this is the only exercise i tried, but it instantly makes me think there must be other horrors lurking.

1

u/johntrytle 4d ago

Damn this would've been super useful when I was using that book

1

u/Automatic-Tackle5018 4d ago

Wow, this is pretty cool! I'm a bit beyond working through K&R exercises, but I appreciate the effort this took, and it is a great resource for new learners.

0

u/sciencekm 3d ago

I did this one:

Exercise 1-1: Hello, world

Run the "hello, world" program on your system. Experiment with leaving out parts of the program, to see what error messages you get.

Print hello, world followed by a newline.

and submitted:

#include <stdio.h>
int main() {
  return puts("hello, world");
}

It was declared "failed." Seriously?

1

u/didntplaymysummercar 3d ago edited 3d ago

It literally says for this program "Exited with code 13; main must return 0. Your output was correct; only the exit failed the test."

puts returns non-negative on success, glibc returns number of characters written.

You tried to be cute 1337 hax0rz with your return puts (in a very old and portable language with like 5+ standard library implementations) and then go "Seriously?" at a site that even gave you direct error reason and told you they use GCC so you can take a guess what libc it uses.

1

u/sciencekm 3d ago

There is no requirement for the the exit code. Can you please point me to it?

1

u/zhivago 2d ago

I don't understand your question.

Does the return value of puts have sensible semantics for the exit code of your program?

1

u/sciencekm 2d ago

puts returns negative value on error. The program returns this is the exit code.

Strangely, the "solution" is apparently this:

int main() {
  printf("hello, world\n");
  return 0;
}

That always returns zero even if printf failed (like if you are in an ssh terminal that quit).

1

u/zhivago 2d ago

Have you considered return printf(...) < 0; ?

1

u/sciencekm 2d ago

I think for this particular case, printf() is not the most appropriate. The output is really not formatted and the output requires a \n that is free in puts(). printf() would be scanning the string looking for format specifiers only to find none. puts() won't have to do any of that.

That's just my preference. I'm sure others will find their own reasons to use printf() in this particular case.

1

u/zhivago 2d ago

l think that you missed the point.

Never mind.

2

u/[deleted] 3d ago

[removed] — view removed comment

1

u/sciencekm 3d ago

You can't point to the requirement and so you resort to name calling?

2

u/[deleted] 3d ago

[removed] — view removed comment

1

u/cprogramming-ModTeam 2d ago

r/cprogramming does not allow harassment

1

u/cprogramming-ModTeam 2d ago

r/cprogramming does not allow harassment

1

u/didntplaymysummercar 3d ago

At first I thought you really didn't understand what happened, but based on your behavior I'm now 99% sure you knew it'd check the return code (as many/most/all online judges do) and you wrote this convoluted return puts to then come here and put OP down and act all smart and arrogant as if you found a real problem in their website. Absolutely gross behavior.

0

u/[deleted] 3d ago

[deleted] — view deleted comment

0

u/didntplaymysummercar 3d ago

You're wrong, puts on glibc returns count of characters written so he gets "Exited with code 13; main must return 0. Your output was correct; only the exit failed the test."

1

u/sciencekm 3d ago

There is no requirement for the exit code.

2

u/didntplaymysummercar 3d ago

There is also no requirement for you to be a smarmy redditor yet here we are. It literally told you: "Exited with code 13; main must return 0" and it's a completely normal feature of online judges and normal practice in real life programming to verify process exit codes.

0

u/sciencekm 3d ago

When something is told you after the fact, that is not how requirements work.

2

u/didntplaymysummercar 3d ago

If you can't understand implicit requirements exists and reasons for them - that's a you problem.