0

I just tried to read some source code in the C language. I encountered with some weird syntax in the <ctype.h> file.

Can somebody please explain the syntax or give me some reference as to what it means?

extern const unsigned short int **__ctype_b_loc (void)
     __THROW __attribute__ ((__const__));

I know it should be some definition for a global variable, but the syntax spread into 2 lines, and there is much extra stuff after the variable definition (after the __ctype_b_loc)

chqrlie
  • 131,814
  • 10
  • 121
  • 189
Ali Shabani
  • 428
  • 2
  • 10
  • @SteveSummit But GPT's explanation is correct (at least I think it is to the best of my ability - so if it's wrong then that's going to be a good wake-up call for me). – Dai May 05 '23 at 15:00
  • `__THROW` is likely a macro that expands to an `__attribute__` directive. And `__attribute__` is a GCC and Clang extension to add information for the compiler. – Some programmer dude May 05 '23 at 15:00
  • Also note that the function declaration is for an internal function that you should never need to call. There are plenty of documentation, tutorials and examples about the `__attribute___` compiler directive. It shouldn't have been hard to find. – Some programmer dude May 05 '23 at 15:02
  • Yeah - you're all right, (sorry!) – Dai May 05 '23 at 15:02
  • Regardless of the correctness of this particular response, ChatGPT is banned from SO. https://meta.stackoverflow.com/questions/421831/temporary-policy-chatgpt-is-banned?cb=1 – Tim Randall May 05 '23 at 15:02
  • @AliShabani Unfortunately, these days, system header files are not intended to be human readable. (Once upon a time, checking them was a great way to learn about a function, but today, not so much.) The weird extra syntax there is a couple of function *attributes*. If you don't know what a particular attribute means, in general you can safely ignore it. – Steve Summit May 05 '23 at 15:04
  • 1
    @Ali Shabani, If it was simply [`extern const unsigned short int **__ctype_b_loc (void);`](https://cdecl.org/?q=const+unsigned+short+int+**__ctype_b_loc+%28void%29%3B), would you want explanation? – chux - Reinstate Monica May 05 '23 at 15:23
  • Does this answer your question? [Why do I see THROW in a C library?](https://stackoverflow.com/questions/2486386/why-do-i-see-throw-in-a-c-library) – Harith May 05 '23 at 16:31

2 Answers2

2

This declaration is a function prototype with extra non portable attributes. A function prototype has a trailing ; instead of a function body surrounded by {}. Function prototypes declare argument and return types. The posted code defines the function __ctype_b_loc as taking no arguments and retuning a pointer to a pointer to unsigned short int objects that should not be modified:

extern const unsigned short int **__ctype_b_loc(void);

The __THROW identifier probably means the function can throw an exception when used in a C++ context.

The __attribute__((__const__)) means the function is pure, ie: the value returned only depends on the arguments, and thus will be constant for the duration of the program.

This function is used as part of the implementation of the <ctype.h> system, it probably returns a pointer to internal data, a set of arrays of flags and values used by the isxxx(c) and toxxx(c) macros defined in this header.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
1

This is function declaration, extern specifies that the definition of this function is provided in another source file or library, the function name is __ctype_b_loc, it takes no arguments (void) and returns a pointer to a pointer to an array of unsigned short int, the const means that the data it returns can't be modified so I guess this declaration is used internally by the C library implementation.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • Good answer. Maybe point out that `__THROW __attribute__ ((__const__));` is non-standard compiler extensions? – Allan Wind May 05 '23 at 15:05
  • I didn't know that ignoring the `{}` is possible for function declartion. and my question was about the function body (maybe) not the declartion syntax. but tnx anyway. – Ali Shabani May 05 '23 at 16:35