user1232138
user1232138

Reputation: 5541

Printing out the elements of an array using a macro

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.

Upvotes: 0

Views: 4473

Answers (2)

Zhang Tao
Zhang Tao

Reputation: 23

#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;
}

Upvotes: 1

cnicutar
cnicutar

Reputation: 182664

Here's a start:

#define PRINT(a, n) do {     \
    int i;                   \
    for (i = 0; ?; ?) {      \
        ?                    \
    }                        \
} while(0)

Upvotes: 3

Related Questions