1

I came across a question where #define was used to replace int in the program as follows

#define type int
int main()
{
 type *a,b;
}

Is this valid? Although it gave me error while I tried to print size of variable b saying b is undeclared. I want to know the specific reason behind this. Please let me know.

Some users told me that I have hidden some part of my code. I had not given the printf statement in my above code snippet. Below is the code with printf and the one which gives error

#define type int;
int main()
{
 type* a, b;
 printf("%d",sizeof(b));
}
phuclv
  • 27,258
  • 11
  • 104
  • 360
  • 6
    The code snippet you show is perfectly valid. It declares `a` as a pointer to `int` and `b` as an `int`. The preprocessor simple replaces all instances of the symbol `type` with the expansion which is `int`. If you get errors, it with some other code that you don't show us. – Some programmer dude Aug 25 '15 at 10:27
  • 3
    Where are you printing `sizeof(b)`? – Gopi Aug 25 '15 at 10:29
  • 2
    Use `%zu` conversion specifier to print arguments of type `size_t`. – Lundin Aug 25 '15 at 10:48
  • 1
    Macros are text replacements. The semicolon after ´int` will be replaced, too, so that your declaration reads `int;* a, b`, which is a syntax error. Remove the semicolon from your macro definition: `#define type int`, like in yout first example. – M Oehm Aug 25 '15 at 10:48
  • 3
    Check the end of your macro definition. That semicolon shouldn't be there. – Some programmer dude Aug 25 '15 at 10:48
  • `sizeof` returns `size_t`, which must be printed using `%zu` [How to print size_t variable portably?](http://stackoverflow.com/q/2524611/995714), [How do I print the size of int in C?](http://stackoverflow.com/q/5943840/995714) – phuclv Aug 25 '15 at 11:36
  • The parenthesis around `b` are not needed for `sizeof` (just like for `return`); it is an operator, not a function. Parenthesis are only needed if you are referencing a type, e.g., `sizeof (int)`. Also, `℅zu` is only supported with C99+; to print `size_t` portably with C89/90, use `%lu` and cast the variable to `(unsigned long)`. – RastaJedi Aug 25 '16 at 19:57

3 Answers3

6

Yes, it can be used, but keep in mind, there is significant difference between macro and type defintion. The preprocessor replaces "mechanically" each occurence of token, while typedef defines true type synonym, thus the latter is considered as recommended way.

Here is an simple example to illustrate what it means in practice:

#define TYPE_INT_PTR int*
typedef int *int_ptr;

int main(void)
{
    TYPE_INT_PTR p1 = 0, p2 = 0;
    int_ptr p3, p4;

    p3 = p1;
    p4 = p2; // error: assignment makes pointer from integer without a cast 

    return 0;
}

You may expect that type of p2 is int*, but it is not. The preprocessed line is:

int* p1 = 0, p2 = 0;

On the other hand, typedef defines both p3 and p4 as pointers to int.

Grzegorz Szpetkowski
  • 35,042
  • 4
  • 82
  • 127
1

For simple types like your example it'll work, although it won't work as expected when declaring multiple variables one the same statement as Grzegorz Szpetkowski said. For more complex cases it might not work at all, as the texts in macros are only blindly replaced.

For example to declare a function pointer with typedef:

typedef int(*int_func)(int);
func some_function_pointer;

You can't use a simple macro like above as the syntax for function pointers is different, and you'll also need different macros for declaring a variable or casting one.

#define int_func_declaration(f) int(*f)(int)
#define int_func_cast(f) ((int(*)(int))f)

int_func_declaration(some_function_pointer) = int_func_cast(some_pointer_value);
phuclv
  • 27,258
  • 11
  • 104
  • 360
0

The mistakes in your program are:

  1. There should not be any semi column in the #define preprocessor command.
  2. There is no meaning for the * after type in the line type* a,b;. If you like of declaring a pointer then you should declare like this type *a, b;
  3. You forgot to include stdio.h header file in the beginning of the code

Click here for the corrected code.

phuclv
  • 27,258
  • 11
  • 104
  • 360
  • code should be posted inside the answer/question itself, not in an external link. And your code is also wrong. It uses `%d` to printf `sizeof` which invokes undefined behavior. [`%zu` must be used instead](https://stackoverflow.com/q/940087/995714) – phuclv Oct 12 '20 at 07:19