Hot answers tagged

14 votes
Accepted

Solution to Codejam 2019 1A (Pylons) in C

These coding competition sites are great at teaching you tricks and algorithms, but they are terrible at teaching you to write readable and maintainable code. Let's see how we can improve the latter. ...
  • 40.6k
9 votes

Multiperson chat server using the select() API

log.h I don't think that calling code has any need for TS_BUF_LENGHT, so this could be moved to the implementation file. ...
  • 65.4k
8 votes

Rock, Paper, Scissors game with CPU

int games_number = 0, games_played = 0, user_wins = 0, computer_wins = 0, even_games = 0; //Counters Wow, that's a lot of counters! When choosing a data ...
  • 7,031
8 votes

Event functionality in C

Files The EventListener.h file should contain only declarations, as it needs to be safe to include in object files that are linked together. Put the definitions ...
  • 65.4k
7 votes

Multiperson chat server using the select() API

Answers to your questions Is my code POSIX compliant? That question doesn't make sense. Your code does not implement the POSIX specification, so there is no question about compliance. You might use ...
  • 40.6k
6 votes

C program for storing the time of the sunset and sunrise

General Observations Does my code meet C99 standards? Not exactly, it meets an earlier standard than C99, since C99 is generally backwards compatible with the earlier standards it compiles just fine ...
  • 20.8k
6 votes

Rock, Paper, Scissors game with CPU

Many points already covered. Check input functions return values ...
6 votes
Accepted

Write a function reverse(s) that reverses the character string s. Use it to write a program that reverses its input a line at a time

Your implementation is not what the exercise asked for. It asked for a function that reverses a string (implied: in-place), and separately, I/O to read and print lines using that function. What it did ...
  • 55.3k
6 votes

Follow-up: C program for storing the time of the sunset and sunrise

General Observations I was hoping you would do a follow up question. This is definitely an improvement, I can see more issues I should comment on, which was harder to do in the first version. I ...
  • 20.8k
6 votes
Accepted

Write a program entab that replaces strings of blanks by the minimum number of tabs and blanks to achieve the same spacing

Thank you for putting your work out there, it's brave of you! That is how we improve. The motivation for CHARMAX / LINEMAX is unclear, given that they are not part of the problem specification. Gold ...
  • 7,031
5 votes

Follow up: A chat server using the select() API

Nice effort. This review only addresses 2 of the 10 questions. I hope OP gets a review of the other 8 questions too. Does any part of my code exhibit undefined behaviour? Calls to ...
4 votes

Write a function reverse(s) that reverses the character string s. Use it to write a program that reverses its input a line at a time

Independent of whether the function reverseString as written is doing what it should, it has an off-by-one error. In the loop ...
  • 41
4 votes

Write a function reverse(s) that reverses the character string s. Use it to write a program that reverses its input a line at a time

The main program can be simplified by using a function you probably haven't read about yet called fgets. This function reads a line of text at a time. The variable <...
  • 20.8k
4 votes

Rock, Paper, Scissors game with CPU

When it comes to code structure, here are some tips that may be helpful: Don't use gotos when you can achieve the same behaviour with ...
4 votes
Accepted

Rock, Paper, Scissors game with CPU

move the srand to the top, no reason to call it multiple times Get rid of the goto and instead rely an outer ...
  • 589
4 votes
Accepted

Finding the sum of 2^x and all powers of 8 less than 2^x

c should not exist, and calculating n before your p%3 check is not a good idea. Further, don'...
  • 55.3k
4 votes
Accepted

Compute lp-norm of a vector in parallel with MPI

Your assignment is ambiguous. You can indeed create the vector on one process and broadcast it, but that is bad MPI practice for two reasons: it's a sequential bottleneck, and it means that process ...
4 votes

Simple photomosaic generator

static const char *cache_filename = "/home/wilson/.cache/photomosaics/avgs"; Recommend you define a helper function which returns the ...
  • 7,031
4 votes

Parse program arguments of the form key=value

I certainly prefer variant A. Despite the extra function call, it's much easier to use correctly. Consider for example that some maintainer (perhaps you) needs to change the name of one of the ...
  • 65.4k
4 votes
Accepted

Write a Program Detab That Replaces Tabs in the Input with the Proper Number of Blanks to Space to the Next Tab Stop

Overall: Aside from bug, a very good post. Nice layout & formatting Good object names Bug: Inconsistent size With char line[BUFSIZ]; and C definition of "...
4 votes

C program for storing the time of the sunset and sunrise

Overall Ambitious and good effort. Enable all compiler warnings. Use double instead of float. Make use of C standard library ...
4 votes

Follow-up: C program for storing the time of the sunset and sunrise

Are there major issues with my code? Reconsider astronomical calculations and local time functions: Keep them separate. For astronomy, use double with a ...
4 votes

Wipe a directory tree

The code is very dense, to the extent that it harms readability. I'm not a fan of variable names such as B, especially for globals. We could do with better layout ...
  • 65.4k
4 votes
Accepted

Event functionality in C

In addition to Toby's comments, here are two more issues that should be looked into. trigger needs to be volatile The trigger variable in your EventListener struct ...
  • 266
4 votes
Accepted

Follow up: A chat server using the select() API

Answers to the remaining 8 questions Could any of these syscalls block? That's an excellent question to ask for a program like this. There are lots of system calls in your code that can block, apart ...
  • 40.6k
3 votes

C program for storing the time of the sunset and sunrise

Let's look closely at calc_sun_time(). The first thing that strikes me is that it returns a float, but there's nothing to ...
  • 65.4k
3 votes

C program for storing the time of the sunset and sunrise

(This answers tries to address good practices, but doesn't consider the question of "standard c". This answer is also specific to POSIX) Standard constants Use ...
3 votes

Parse program arguments of the form key=value

As with many things in C, the answer is "use a macro." Hard-coding values is error-prone because there is the risk you will either (a) get a value wrong; (b) change one of the values but not ...
  • 12.2k
3 votes

Follow-up about computing lp norm with MPI

Allocate to the referenced object, not type Code obliges a reviewer to type check vec, which is defined someplace else. It is easier to code right, review and ...
3 votes

Follow-up about computing lp norm with MPI

This code looks good. In particular I like how you handled the rem elements. There is a yet more elegant scheme: define ...

Only top scored, non community-wiki answers of a minimum length are eligible