LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-23-2011, 11:33 AM   #1
tinyTux
Member
 
Registered: Mar 2011
Location: Extended Memory
Distribution: Gentoo
Posts: 64

Rep: Reputation: 9
C: Underscore before string?


I have been reading through C source code to try to improve myself. In the coreutils source code, there are often function calls like so:

Code:
fprintf (stderr, _("Try `%s --help' for more information.\n"), program_name);
Why is that string in the middle surrounded by parentheses and preceded by an underscore?
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 03-23-2011, 11:36 AM   #2
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
The underscore is just an identifier. Either the program itself or a library uses it, and defines it as a function or a macro. _() Might return the text inside of fprint to translate to a different language, add formatting, etc. Hope that helps.

Josh
 
1 members found this post helpful.
Old 03-23-2011, 02:06 PM   #3
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Sprinkled throughout the coreutils code are macro definitions of the form
Code:
#define _(fred) gettext (fred)
There are 28 of those, at last count.

They don't use fred, of course, and it doesn't matter what they use; the macro _ is defined as a call to gettext() with the argument to the macro. gettext() returns a string. For more details on gettext(), see the man page.
 
3 members found this post helpful.
Old 03-23-2011, 02:09 PM   #4
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
If you read the sources carefully, you will very likely find something like
Code:
#ifdef  HAVE_GETTEXT
#define _(s) gettext(s)
#else
#define _(s) s
#endif
Like corp769 said, _() is usually a macro used to make string localization easier. The above way also allows the same code to be compiled without localization support without changing any code. For details, see eg. man 3 gettext.
 
3 members found this post helpful.
Old 03-24-2011, 04:15 AM   #5
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
don't be too alarmed,
if you read through the source code of standard libraries or
kernel code, you often find that your brain explodes.

this is normal
:-)
 
Old 03-24-2011, 05:17 AM   #6
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Quote:
Originally Posted by bigearsbilly View Post
if you read through the source code of standard libraries or
kernel code, you often find that your brain explodes.
It's not brain science.
 
Old 03-24-2011, 11:21 AM   #7
tinyTux
Member
 
Registered: Mar 2011
Location: Extended Memory
Distribution: Gentoo
Posts: 64

Original Poster
Rep: Reputation: 9
Thanks. I'll admit, I haven't read all the source code yet but am working on it. I must have missed that macro definition. I assumed it was some kind of special C construct I hadn't heard about yet.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] copy string a to string b and change string b with toupper() and count the chars beep3r Programming 3 10-22-2010 07:22 PM
Concatenate two variables with underscore - question laki47 Linux - Newbie 3 06-04-2010 05:02 AM
sed, replacing underscore with whitespace fjkum Programming 3 10-31-2007 12:09 AM
C: fprintf with underscore jhwilliams Programming 8 08-25-2007 12:08 AM
deleting an underscore Post Modern Programming 2 11-27-2005 05:35 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:40 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration