Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

152
Views
Does C have a [[nodiscard]] mechanism to warn when values are ignored?

In C++ we can decorate our return types with "[[nodiscard]]" which triggers a compiler warning if the results are unused.

This is particularly useful to enforce error codes

auto dont_forget_to_check = do_something_important();
assert(dont_forget_to_check);

Does something like this exist for C?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

There's no standard way to do this, but gcc does support the warn_unused_result attribute for this.

__attribute__ ((warn_unused_result))
int foo()
{
    return 5;
}

int main()
{
    foo();
    return 0;
}

Compiler output:

[dbush@db-centos7 ~]$ gcc -g -Wall -Wextra -o x1 x1.c
x1.c: In function ‘main’:
x1.c:11:8: warning: ignoring return value of ‘foo’, declared with attribute warn_unused_result [-Wunused-result]
     foo();
        ^
over 4 years ago · Santiago Trujillo Report

0

Does something like this exist for C?

Not yet, but likely in C2x.

C++11 style attribute syntax and the nodiscard, maybe_unused, deprecated, and fallthrough attributes.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!