News for hackers. Stuff that matters.
Re:Ask HN: What one change in C would make it a better langu... by stncls on Thursday July 27, 2023 @07:39AM (#36892021)
A very surface change (almost syntactic sugar) that would be neat for me is some form of generics. Specifically, to allow me to implement an algorithm once for different data types. Right now I do it with preprocessor macros and includes; it works but feels wrong.

Going deeper, but not as far as changing the language (not hoping for memory safety here), I would like better research and documentation for undefined behavior (UB).

I'm not completely opposed to UB. I understand it is an unfortunate performance/safety trade-off that's not fully avoidable in a historical language like C. However, there are many types of UB, and for a few of them, the performance benefits feel theoretical (not supported by empirical evidence). I would love some before/after benchmarks with the types of UB that can be disabled in the compiler, and some open-minded discussion afterwards.

Re:Ask HN: What one change in C would make it a better langu... by jjgreen on Thursday July 27, 2023 @07:29AM (#36891952)
"case" variable scope
Re:Ask HN: What one change in C would make it a better langu... by mytailorisrich on Thursday July 27, 2023 @07:33AM (#36891981)
case x:

  {

     int local_var;

  }

  break;
Re:Ask HN: What one change in C would make it a better langu... by jjgreen on Thursday July 27, 2023 @08:40AM (#36892550)
... that's what I do while I wait for the committee to respond to my increasingly threatening emails ...
Re:Ask HN: What one change in C would make it a better langu... by tsukikage on Thursday July 27, 2023 @12:01PM (#36895608)
this stuff is just broken full stop

https://gcc.godbolt.org/z/vhbxob41x

Re:Ask HN: What one change in C would make it a better langu... by AnimalMuppet on Thursday July 27, 2023 @08:22AM (#36892372)
That might have issues with fallthrough.

  case 'a':
    int x = 7;
    // no break;
  case 'b':
    // did we get here through 'b', or through 'a'?
    // is x in scope, or not?  Is it initialized, or not?
Re:Ask HN: What one change in C would make it a better langu... by sigsev_251 on Thursday July 27, 2023 @07:47AM (#36892079)
Defer (which also means lambdas). I really hope that the proposal gets in the next standard.
Re:Ask HN: What one change in C would make it a better langu... by tsukikage on Thursday July 27, 2023 @07:51AM (#36892109)
Namespaces.
Re:Ask HN: What one change in C would make it a better langu... by DLA on Thursday July 27, 2023 @08:03AM (#36892211)
Bounds checks on arrays/strings. This would prevent the largest class of errors (buffer overflows) seen in C code.
Re:Ask HN: What one change in C would make it a better langu... by Shawnj2 on Thursday July 27, 2023 @11:36AM (#36895161)
There’s a lot of cases where they wouldn’t work because passing a pointer to an element is the same as passing an array reference.
Re:Ask HN: What one change in C would make it a better langu... by pestatije on Thursday July 27, 2023 @08:35AM (#36892499)
deprecate headers
Re:Ask HN: What one change in C would make it a better langu... by logicalmonster on Thursday July 27, 2023 @08:57AM (#36892716)
I'm not sure if that horse could ever be put back in the barn, but I think my mindset for using C would probably feel a lot different if somehow it could be made a strict subset of C++.
Re:Ask HN: What one change in C would make it a better langu... by AlectronikLabs on Thursday July 27, 2023 @11:27AM (#36895033)
Modules. No more duplicating code into header files, just import a module like in modern languages. I love C but really hate headers.

Then, second for namespaces.

Re:Ask HN: What one change in C would make it a better langu... by didgetmaster on Thursday July 27, 2023 @11:46AM (#36895358)
The default behavior for a case within a switch statement should be to break. If you want to fall through to the next case, it should have been an explicit command (e.g. fallthrough).

switch(x) { case 1: DoSomething(); case 2: DoSomethingElse(); case 3: SomePrep(); fallthrough; case 4: DoMore(); default: DoNothing; }

Only one statement needed to have case 3 fall through to case 4. No breaks needed at the end of any other cases.

Re:Ask HN: What one change in C would make it a better langu... by mikewarot on Thursday July 27, 2023 @01:10PM (#36896743)
It's not just one, but a short list

  get rid of Macros
  adopt Pascal style pointer references...   @ is an address,   p^ is what p points to
  adopt some form of counted strings, with a null on the end for compatibility
  adopt Pascal style cast statements, with each path being exclusive (default break)
Re:Ask HN: What one change in C would make it a better langu... by bjourne on Thursday July 27, 2023 @08:25PM (#36901848)
If the brain-dead implicit type promotion rules were removed. E.g

    unsigned int a = 5;
    signed int b = -10;
    bool c = a + b > 0;
c is true.
Re:Ask HN: What one change in C would make it a better langu... by akasakahakada on Friday July 28, 2023 @03:39AM (#36904028)
I ;

don't;

know;