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 08-22-2011, 09:51 AM   #1
govindasahu
LQ Newbie
 
Registered: Aug 2011
Posts: 1

Rep: Reputation: Disabled
micro in c


#define test(a) a*a
void main()
{
int i=3;
printf("%d",test(i+4));
}

ans is : 19
how?????????
 
Old 08-22-2011, 10:07 AM   #2
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,223

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
You can't work it out yourself?

i = 3
test(a) = a * a
test(i + 4) = i + 4 * i + 4 // note the lack of brackets!
test(i + 4) = 3 + 4 * 3 + 4
test(i + 4) = 19

What you wanted to write was this:

Code:
#define test(a) (a*a)

Last edited by dugan; 08-22-2011 at 10:10 AM.
 
Old 08-22-2011, 10:08 AM   #3
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Look at the output after preprocessing (with gcc, you can use the -E option):

printf("%d\n", i + 4 * i + 4);

Is there any reason you need to use a macro rather than a function?

Edit: Damn you, dugan! .
 
Old 08-22-2011, 10:10 AM   #4
Proud
Senior Member
 
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116
http://en.wikipedia.org/wiki/C_preprocessor#Precedence
 
Old 08-22-2011, 12:02 PM   #5
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by dugan View Post
What you wanted to write was this:

Code:
#define test(a) (a*a)
I think you meant:
Code:
#define test(a) (a)*(a)
 
Old 08-22-2011, 04:16 PM   #6
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
I think you both meant
Code:
#define test(a) ((a)*(a))
so that e.g. ~test(expression) works the way you'd expect it to. (Otherwise it'll invert just one multiplicand, and not the result, I think.)

Last edited by Nominal Animal; 08-22-2011 at 04:18 PM.
 
Old 08-22-2011, 05:00 PM   #7
Proud
Senior Member
 
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116
+1 the use a function suggestion, we've messed up the macro 3 times already?

Last edited by Proud; 08-22-2011 at 05:01 PM.
 
1 members found this post helpful.
  


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
People who call it M$ or Micro$oft or Micro$loth lowpro2k3 General 48 12-24-2007 12:19 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
micro$oft again.... anacOndazz Linux - Security 5 11-05-2004 08:57 PM
Micro$oft Jenn_TG General 7 05-21-2004 06:43 AM
Trying to get away from Micro$oft tommycat Linux - Networking 10 07-19-2003 01:17 AM

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

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