LinuxQuestions.org
Help answer threads with 0 replies.
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-22-2004, 09:37 PM   #1
Squall
Member
 
Registered: Jan 2004
Location: The land of the free and the home of the brave
Distribution: Slack 10
Posts: 239

Rep: Reputation: 30
Can someone debug this 4 line program? (C)


I can't find the error:
~/test5/test5.c:/* this program will involve practicing macros */




#include <stdio.h>
#include "test5.h"
main ()
{
printf ("The answer is %d\n", MULTIPLY);
}


~/test5/test5.h:/* work on macro-y stuff */


#define ADD(x, y) (x) + (y)
#define MULTIPLY(ADD, z) (ADD) * (z)
#define x 1
#define y 2
#define z 3
/* MULTIPLY SHOULD EQUAL 9 */



Thanks, I'm stumped.
 
Old 03-22-2004, 09:57 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,005
Blog Entries: 11

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
I may have gotten the entire concept of
preprocessor directives wrong (after all
I've done more C++ than C), but shouldn't
the
printf ("The answer is %d\n", MULTIPLY);
have parameters too?


Cheers,
Tink
 
Old 03-22-2004, 10:13 PM   #3
Mohsen
Member
 
Registered: Feb 2003
Location: Iran
Distribution: Solaris 10
Posts: 201

Rep: Reputation: 30
Yes, without parameters what should your macro multiply by what?
Code:
int foo1 = 10, foo2 = 20;
printf ("The answer is %d\n", MULTIPLY (foo1, foo2);
 
Old 03-22-2004, 11:48 PM   #4
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 42
This works for me:

Code:
#include <stdio.h>

#define MULTIPLY (ADD * z)
#define ADD (x + y)
#define x 1
#define y 2
#define z 3

int main()
{
	printf("%d\n", MULTIPLY);
	return 0;
}
Since MULTIPLY and ADD are supposed to be constant, they shouldn't be #defined as taking inputs. The following is probably an introduction to the sorts of things you want to do with macros taking parameters:

Code:
#include <stdio.h>

#define MULTIPLY(a, b) ((a) * (b))
#define ADD(a, b) ((a) + (b))
#define WRAPPER(a, b, c) (MULTIPLY(ADD(a, b), c))
#define WRAPPER2 WRAPPER(x, y, z)
#define x 1
#define y 2
#define z 3

int main()
{
	printf("%d\n", WRAPPER2);
	return 0;
}
It also prints "9".
 
  


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
how to debug GTK+ program asahlot Programming 4 10-19-2005 05:23 AM
How do i debug my c++ program please? twirl Programming 1 09-12-2005 03:34 AM
how can I debug the interactinve program with GDB bigapple Programming 1 08-03-2005 10:55 PM
How do I debug a C program mfeoli Programming 3 03-12-2004 02:58 PM
c++ newbie - help debug simple program please mymojo Programming 4 11-27-2003 05:17 AM


All times are GMT -5. The time now is 07:56 PM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration