user2706540
user2706540

Reputation: 189

Is there a const function in C?

I mean can we declare a function in C as const like

void fun(int iVar) const;

int g_var = 1;

void fun(int iVar)
{
  //do some thing
  g_var = iVar;
}

Upvotes: 6

Views: 11772

Answers (1)

unwind
unwind

Reputation: 399753

Not in standard C, since there are no classes or objects (as in "class instances, i.e. collections of data with associated functions"), there's nothing for the function to be const "against".

There could in theory be such a notation to mean "this function has no globally visible side-effects, it's a pure function of its input arguments" but there isn't.

The GNU GCC compiler supports, as an extension, declaring a function as either const or pure:

int square(int) __attribute__((pure));

The __attribute((pure)) part is GCC-specific.

Upvotes: 18

Related Questions