0

I have a problem statement, please help me answer this:

Define a macro that receives an array and the number of elements in the array as arguments. Write a program using this macro to print out the elements of an array.

1
  • 1
    you should try this yourself. its easy and you will learn Commented Mar 24, 2012 at 13:01

2 Answers 2

3

Here's a start:

#define PRINT(a, n) do {     \
    int i;                   \
    for (i = 0; ?; ?) {      \
        ?                    \
    }                        \
} while(0)
Sign up to request clarification or add additional context in comments.

1 Comment

Note the do { ... } while(0) structure around the macro, it is very educational.
1
#include<stdio.h>
#define PRINTARRAY(array, length) \
for(int i = 0; i < length; i++) \
    printf("%d\t", array[i]);

int main(void) {
    int array[5] = {4, 2, 3, 1, 0};
    PRINTARRAY(array, 5);
    return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.