What Is The Reason For Underscore In C Variable Name Definition?

    1. Home
    2. Questions
    3. Tags
    4. Users
    5. Companies
    6. Labs
    7. Jobs
    8. Discussions
    9. Collectives
    10. Communities for your favorite technologies. Explore all Collectives

  1. Teams

    Ask questions, find answers and collaborate at work with Stack Overflow for Teams.

    Try Teams for free Explore Teams
  2. Teams
  3. Ask questions, find answers and collaborate at work with Stack Overflow for Teams. Explore Teams

Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Get early access and see previews of new features.

Learn more about Labs What is the reason for underscore in C variable name definition? Ask Question Asked 13 years ago Modified 6 years, 11 months ago Viewed 5k times 7

I am trying to understand when a developer needs to define a C variable with preceding '_'. What is the reason for it?

For example:

uint32_t __xyz_ = 0; Share Improve this question Follow edited Aug 2, 2014 at 1:32 Jonathan Leffler's user avatar Jonathan Leffler 751k145 gold badges944 silver badges1.3k bronze badges asked Nov 11, 2011 at 11:30 user1041588's user avatar user1041588user1041588 891 silver badge3 bronze badges 3
  • 6 You'll have to ask the author – David Heffernan Commented Nov 11, 2011 at 11:31
  • Welcome to StackOverflow! The first thing to realise is that this website is not a forum. It's a question/answer site. Come with a good question, and be prepared to accept an answer if you find one satisfactory. – Kerrek SB Commented Nov 11, 2011 at 11:32
  • Possible duplicate of Why does C have keywords starting with underscore – klutt Commented Jun 17, 2019 at 18:17
Add a comment |

3 Answers 3

Sorted by: Reset to default Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first) 12

Maybe this helps, from C99, 7.1.3 ("Reserved Identifiers"):

  • All identifiers that begin with an underscore and either an uppercase letter or another underscore are always reserved for any use.

  • All identifiers that begin with an underscore are always reserved for use as identifiers with file scope in both the ordinary and tag name spaces.

Moral: For ordinary user code, it's probably best not to start identifiers with an underscore.

(On a related note, I think you should also stay clear from naming types with a trailing _t, which is reserved for standard types.)

Share Improve this answer Follow answered Nov 11, 2011 at 11:34 Kerrek SB's user avatar Kerrek SBKerrek SB 476k93 gold badges895 silver badges1.1k bronze badges 5
  • The _t reservation is from POSIX isn't it? – Flexo - Save the data dump Commented Nov 11, 2011 at 11:42
  • @awoodland: I'm not 100% sure. I'd appreciate if someone could say where this rule comes from. That said, *_t types are so ubiquitous in the C standard that it's probably good to stick to that policy. – Kerrek SB Commented Nov 11, 2011 at 11:47
  • 1 they key bit is the "any header" _t in the second table of: pubs.opengroup.org/onlinepubs/9699919799/functions/… – Flexo - Save the data dump Commented Nov 11, 2011 at 11:53
  • 4 And corollary: a developer needs to define reserved names only when implementing the C language (and libraries). So if you're not contributing to a compiler or stdlib, that's not you. – Steve Jessop Commented Nov 11, 2011 at 11:59
  • What does the second bullet point mean? Can we use those? – user129393192 Commented Jun 9, 2023 at 6:27
Add a comment | 10

It is a trick used in the header files of C implementations for global symbols, in order to prevent eventual conflicts with other symbols defined by the user.

Since C lacks a namespace feature, this is a rudimentary approach to avoid name collisions with the user.

Declaring such symbols in your own header and source files is not encouraged because it can introduce naming conflicts between your code and the C implementation. Even if that doesn't produce a conflict on your current implementation, you are still prone to strange conflicts across different/future implementations, since they are free to use other symbols prefixed with underscores.

Share Improve this answer Follow edited Nov 11, 2011 at 12:29 answered Nov 11, 2011 at 11:31 Blagovest Buyukliev's user avatar Blagovest BuyuklievBlagovest Buyukliev 43.4k14 gold badges95 silver badges134 bronze badges 4
  • 1 It's a reserved name, reserved for the implementation. Sounds like a good way of seeing a strange problem in the future. – Flexo - Save the data dump Commented Nov 11, 2011 at 11:43
  • @awoodland: I fail to see a justification for the downvote. I'm not encouraging anyone to adopt this naming convention in their own header files. – Blagovest Buyukliev Commented Nov 11, 2011 at 11:51
  • I disagreed with the "in order to prevent conflicts" and "a rudimentary approach to avoid name collisions" - it doesn't prevent conflicts at all, in fact it introduces a whole new class of possible conflicts with a perfectly legal implementation. I would prefer to see a clarification along the lines of "a flawed attempt at preventing conflicts" to make that explicit. Currently it reads like a weak recommendation. – Flexo - Save the data dump Commented Nov 11, 2011 at 11:58
  • Upvoted now although some of the single underscore identifiers can also be reserved depending on where they're used. (you had a double underscore in the example at the end) – Flexo - Save the data dump Commented Nov 11, 2011 at 12:27
Add a comment | 1

whether its C or not, the leading underscore provides the programmer a status indication so he does not have to go look it up. In PHP, or any object oriented language where we deal with tens of thousands of properties and methods written by 1000's of authors, seeing an underscore prefix removes the need to go dig through the class andlook up whether its declared private, or protected or public. thats an immense time saver. the practice started before C, i am sure...

Share Improve this answer Follow answered Dec 21, 2017 at 6:08 James Danforth's user avatar James DanforthJames Danforth 8178 silver badges7 bronze badges Add a comment |

Your Answer

Reminder: Answers generated by artificial intelligence tools are not allowed on Stack Overflow. Learn more

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid …

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

Draft saved Draft discarded

Sign up or log in

Sign up using Google Sign up using Email and Password Submit

Post as a guest

Name Email

Required, but never shown

Post Your Answer Discard

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.

  • The Overflow Blog
  • We'll Be In Touch - A New Podcast From Stack Overflow!
  • The app that fights for your data privacy rights
  • Featured on Meta
  • More network sites to see advertising test
  • We’re (finally!) going to the cloud!
  • Call for testers for an early access release of a Stack Overflow extension...

Linked

21 use _ and __ in C programs 2 What does "_" mean in C programming language? 1 C variable name beginning with _ allowed, and (0x8) meaning? 13 Why does C have keywords starting with underscore 6 what is the use of (Underscore) in C? 1 SIMD for alpha blending - how to operate on every Nth byte? 0 Data Type In C To Represent Any Pointer To Function 60 What does double underscore ( __const) mean in C? 45 Why do C compilers prepend underscores to external names? 24 Meaning of double underscore in the beginning 4 Can an underscore be used anywhere in an identifier in C? 5 What does underscore as an argument in C mean? 1 In C, why certain function name prefix of "_ _" (double underscore) 6 what is the use of (Underscore) in C? 3 Possible reasons for semicolon before underscore in variable name in C 4 Underscore before a C function parameter 1 What are the rules about using an underscore in a C identifier?

Hot Network Questions

  • Lowest processable signal level after FFT with given noise level
  • Suggestion for catching a flight with short layover in Amsterdam
  • Do switches try to keep track of Ethernet group membership?
  • Understanding Linux 'top' command: Memory vs Swap display format confusion
  • Accused of violating NDA on thesis
  • Bleeding radiators
  • Did Superdana manufacture a 66 AC outlet power strip/surge protector?
  • Darlington-driven PNP vs. MOSFET
  • Is eating onion and garlic sin?
  • Scandinavian film, 1980s, possibly called Royal Toilet?
  • Is there a theorem in metaphysics that basically says, "Biting the bullet will be inevitable in any metaphysical theory?"
  • Commencement and repeal dates in UK Acts of Parliament - meaning of "From and after"
  • What is it called when you have a hobby where you're good enough at to impress others but you yourself know you're only beginning?
  • How to detect if the KERNAL load/save routine is currently active on a CBM system?
  • Can a storage device completely erase itself while performing the erase?
  • What is the best language to speak with locals in Singapore?
  • safe divide command for pgfmathparse to enable equality checks involving division by zero
  • Power series of the reciprocal of f defined as a power series
  • Do rediscoveries justify publication?
  • How were lead sheets made in the Graeco-Roman world?
  • Quantum gravity and perturbative parameters
  • How can I tell if commercial packaging is suitable for Sous Vide cooking?
  • How to DSolve[{-0.8*y'[x]*y''[x]/(1 + (y'[x])^2)^1.4 == 0.77781490, y[0] == 0, y[1] == 1}, y[x], x]
  • Where in the Gospels does Jesus explain what 'The Truth' is?
more hot questions Question feed Subscribe to RSS Question feed

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

lang-c

Từ khóa » C 0x8