How to write macro in c in this case?

Viewed 53

Given the following function in c:

int reset_values_of_string(char* s, int to_new_val){
   int l = strlen(s);
   int was_changed = 0;
   for(int i=0; i<l; i++) {
      if(s[i] != to_new_val) was_changed=1;
      s[i] = to_new_val;
   }
   return was_changed;
}

Can I write this function as MACRO (#define) in C? that will do exactly the same and will return this value ?

In addition, what is bascially preferred? to implement it with macro or with function like that i write it above.

1 Answers

Can I write this function as MACRO (#define) in C? That will do exactly the same and will return this value

Yes, and it will be close enough to the function.

What is basically preferred?

A function would be preferred.

Modern compilers can choose to automatically inline functions if they deem it a better option. You may also encourage compilers to inline functions.

Related