I was reading a book on c++ (A Complete Guide to Programming in C++ by Ulla Kirch-Prinz and Peter Prinz; ISBN: 0-7637-1817-3) and it mentioned that isupper(), along with islower(), isalpha(), isdigit(), isalnum(), isspace(), and isprint(), are macros for character classification.
I find this kind of weird for two reasons:
In the same book, it talks about how, even though macros can have arguments, they are still essentially just placeholder names that are filled in with the definition. This seems weird, since I don't know what kind of replacement text would operate conditionally. That sounds more in line with either a function or a built-in operation (like sizeof
).
It also seems weird because I've heard some people call these functions, while I've also heard some people call them macros.
Any explanation would be dearly appreciated. I'm moderately new to this, so I'm still trying to figure this all out. Thank you.
Speed versus size The main benefit of using macros is faster execution time. During preprocessing, a macro is expanded (replaced by its definition) inline each time it is used. A function definition occurs only once regardless of how many times it is called.
It's a function, not a macro. The function definition of isupper () differs depending on things like locale and the current character set-that's why there's a function specifically for this purpose.
Entered character is uppercase character Application : isupper () function in C programming language is used to find out total number of uppercase present in a given senence.
Definition and Usage. The isupper() method returns True if all the characters are in upper case, otherwise False. Numbers, symbols and spaces are not checked, only alphabet characters.
isupper(), islower(), lower(), upper() in Python and their applications. isupper() In Python, isupper() is a built-in method used for string handling. The isupper() methods returns “True” if all characters in the string are uppercase, Otherwise, It returns “False”.
Standard C (in all its versions, as far as I know) allows any standard library function to also be defined as a macro. In the current C standard, that's buried in §7.1.4 (Use of Library Functions), paragraph 1:
Any function declared in a header may be additionally implemented as a function-like macro defined in the header… Any invocation of a library function that is implemented as a macro shall expand to code that evaluates each of its arguments exactly once, fully protected by parentheses where necessary, so it is generally safe to use arbitrary expressions as arguments.
The macros may be defined by a standard library implementation in order to provide efficiencies (although this is less important than it used to be, now that most compiler will inline functions), but the functions also need to be defined as functions with exactly the same semantics, which means that you can use the function without including the header (as long as you provide the correct function prototype) and that you can take the address of the function.
And, as indicated in the second sentence I quoted, you don't have to worry about the possible odd side effects of macros, such as multiple evaluation of their arguments -- unless the description of the function indicates otherwise. (One such function is getc
, which is semantically identical to fgetc
except that getc
is allowed to implemented as a macro which does evaluate its argument more than once.)
It was never the case that these functions were only implemented as macros (at least, not in conformant standard libraries). But there was a time when it was so common to also implement simple functions as macros that some people just counted on the macros existing. Or talked about them as though that was the only implementation.
None of this is the case in C++. C++ requires functions to be functions (and macros to be macros), and the functions are in namespaces. That didn't make C++ incompatible with C; it was merely necessary for the C++ headers which declare functions shared with C (that is, the ones whose names start with c
) to #undef
all the (potential) macros definitions after including the corresponding C header.
Apart from the history discussed in the comments,
isupper
is officially a function in C++.
See the documentation in that link. It shows the signature, namespace, and other details.
Edit: (response to the comment)
Historically, yes it might have been a macro while evolving from C. Because that was implementation defined.
If you wonder how that macro could have been defined, see this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With