LinuxQuestions.org
Visit Jeremy's Blog.
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 02-15-2012, 01:01 AM   #1
gourav87
LQ Newbie
 
Registered: Feb 2012
Posts: 1

Rep: Reputation: Disabled
micro use in printf


#define WHITE BLACK

printf("WHITE");

is there any to print this output as-- BLACK.

this code i am using is in C.
Need urgent.
Thanks
 
Old 02-15-2012, 01:12 AM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
You will have to change the code some.

Code:
#define WHITE     "BLACK"

...

printf( WHITE );
To my knowledge, there's no way to have the cpp modify the contents inside a string literal. That is, after all, the whole point. The programmer wants the literal text between the double quotes.

Last edited by Dark_Helmet; 02-15-2012 at 01:14 AM.
 
Old 02-15-2012, 05:57 AM   #3
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
An alternative:
Code:
#include <stdio.h>
#include <string.h>

#define MACRO(str) (strcmp((str), "WHITE") ? (str) : "BLACK")

int main(void)
{
    printf( MACRO("WHITE") );
    printf("\n");
    return 0;
}
This kind of string substitution is used by e.g. the gettext() package, for internationalization. There, the macro is just _ , i.e. printf(_("WHITE"));

Note that the compiler does do some serious logic checking, so the above is not nearly as inefficient as one might think. At least gcc-4.6.1 on x86-64, even with -O0, compiles the above C to exactly the same code as
Code:
#include <stdio.h>

int main(void)
{
    printf("BLACK");
    putchar('\n');
    return 0;
}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
People who call it M$ or Micro$oft or Micro$loth lowpro2k3 General 48 12-24-2007 12:19 AM
printf macro ashlesha Programming 4 11-13-2006 04:14 AM
LXer: Imation 4GB Micro Hard Drive: Still Micro on the Outside, More Giga on the Inside LXer Syndicated Linux News 0 12-28-2005 06:16 PM
printf problem??? Mistro116@yahoo.com Programming 2 11-13-2005 08:45 PM
printf new_user10 Programming 3 09-09-2003 11:12 PM

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

All times are GMT -5. The time now is 09:29 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