Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


Search this Thread
Old 03-22-2004, 08:37 PM   #1
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

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

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

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

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

Rep: Reputation: 42
This works for me:

#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:

#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".


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

All times are GMT -5. The time now is 03:22 AM.

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