Symbols Overloading in C
Have you ever observed that in C Programming, Many symbols are overloaded with more than one meaning? They convey different meanings when used in different contexts.
The symbols like static
, extern
, void
, *
, &
change their meaning depending on where they appear. Here, we are going to discuss a few of these symbols in detail.
Static Keyword
The static
keyword when used along with a variable inside a function retains its value across multiple calls. The variable gets the memory at compile time and remains in memory throughout the execution of the program.
The same static
keyword when used at a function level restricts the variable's visibility to the file only.
Extern Keyword
The extern
keyword when applied to a function definition makes it globally visible to every object file it is linked with. Though, By default a function is extern unless specified with static
keyword explicitly.
However, when the same extern
keyword is applied to a variable, it convinces the compiler that the variable is defined elsewhere.
Void Keyword
The void
keyword when used as a function return type indicates that the function does not return any value. Similarly, when used in a function's parameter list indicates that the function takes no arguments. However, In conjunction with operator, void*
can be declared as a generic pointer and can point to any type of data in memory.
& and * Symbols
The &
and *
symbols have multiple meanings when used in different contexts. The ampersand symbol (&
) can be used as an address-of
operator or bitwise AND
operator.
Similarly, The asterisk symbol (*
) can be used as a dereference operator or multiplication operator. It can also be used to declare a pointer.
Conclusion
Today, we learned that how some symbols in C, like static
, extern
, void
, *
, &
are overloaded and can have different meanings depending on how you use them. These can often be confusing, so it's important to learn what each meaning is and when to use it to write a good C program.
Discussion