1

I have defined macro in a function.I have used this macro in main function which works fine.

So does macro have global scope?

#include <stdio.h>
void f()
{
#define SQUARE no*no
}

void main()
{
    int no;
    printf("Enter no");
    scanf("%d",&no);
    printf("Square of no = %d",SQUARE);
}
8
  • 2
    In the wording of the C standard, macros don't have scope at all. Commented Sep 23, 2014 at 18:03
  • 2
    It's a basic fact about macros that they are just text replacement. And besides, it's obvious from trying it, so why waste everyone's time asking this? Commented Sep 23, 2014 at 19:27
  • 1
    If you #define a macro in a function, #undef it at the end. Not officially a scoped macro, but close enough for all practical purposes. Commented Sep 24, 2014 at 0:59
  • 1
    @WumpusQ.Wumbley Unless the macro was already defined ... so, no, not scoped. Commented Sep 24, 2014 at 2:07
  • 1
    To deny that C has global variables (and therefore global scope) is obscurantist pedanticism. en.wikipedia.org/wiki/Global_variable#C_and_C.2B.2B ... and it's clear what the OP means by the term here, which isn't "global" in that sense. Macros have a lexical scope that extends from the point where they are defined to the end of the compilation unit or to where they are #undefed. Commented Sep 24, 2014 at 2:09

3 Answers 3

3

It may seem like they have global scope file scope(which is what you mean when you say global scope) but macros don't have scope since they are replaced during preprocessing before semantic analysis. If we go to the draft C99 standard it says the following in 6.2.1 Scopes of identifiers:

[...]Macro names and macro parameters are not considered further here, because prior to the semantic phase of program translation any occurrences of macro names in the source file are replaced by the preprocessing token sequences that constitute their macro definitions.

Sign up to request clarification or add additional context in comments.

1 Comment

Note quite sure why someone would downvote this for.
0

What is stopping you from trying it yourself?

The answer is yes. It has global scope.

2 Comments

I tried.It works fine
Then there was no reason to ask the question.
0

In C language macros are processed at early phases of translation. At that stage the language-level notion of scope is not yet applicable. Macros are processed without regard to any scope.

Note that C language does not have such scope as "global scope". C language only defines file scope, function scope, block scope and function prototype scope. That's it. No "global" scope.

The section of language specification that deals with scopes (6.2.1) explicitly states

Macro names and macro parameters are not considered further here, because prior to the semantic phase of program translation any occurrences of macro names in the source file are replaced by the preprocessing token sequences that constitute their macro definitions.

which means that the notion of scope is not applied to macros.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.