|
Does C implement the concept of a namespace?
modified 17-Apr-19 5:39am.
|
|
|
|
|
|
|
C does not provide support for namespace like C++. Without Namespace, we cannot declare two variables of the same name.
We can count Namespaces as a
limitation of C programming Langauge.
|
|
|
|
|
You mean you can not have the same name in the same scope.
It is routine to carry the same name such as i, j, count etc but they get restricted to
within a local scope as a local variable in a function or unit.
Not like there should be many but global variables obviously can't have the same name.
In vino veritas
|
|
|
|
|
It is not a limitation. When C was designed there was not thought to be a need for such a feature. But you could still have variables and functions that were limited to specific source modules. And local variables inside functions were hidden from others automatically.
|
|
|
|
|
Just to be clear namespaces should not be considered a limitation or a feature that is used to protect the scope of variables.
Rather it should be considered as a way to protect other entities like classes and methods.
If you have a problem with namespace collision in scopes with variables then you need to refactor your code so variables are not exposed at all.
|
|
|
|
|
Why C is not popular among developers?
|
|
|
|
|
|
And what is popular among developers?
|
|
|
|
|
|
You, politically incorrect! 
|
|
|
|
|
|
Richard MacCutchan wrote: Wine, women and song.
<looks around> Where?
Freedom is the freedom to say that two plus two make four. If that is granted, all else follows.
-- 6079 Smith W.
|
|
|
|
|
Richard MacCutchan wrote:

|
|
|
|
|
I do like 'C' because my name is Carlo. Richard, for instance, likes more 'R'.
|
|
|
|
|
And i really do like 'M'...
|
|
|
|
|
Of course. 
|
|
|
|
|
C is highly portable and simple language. But, because of some limitation of C, it is loosing fame.
The main reason behind is, it doesn't support object-oriented programming features. Means-
Inheritance
Encapsulation
Polymorphism etc.
are not suported by C programming language, that's why C++ is developed.
Even C doesn't perform run time type checking.
modified 16-Apr-19 21:38pm.
|
|
|
|
|
Abhays01 wrote: The main reason behind is, it doesn't support object-oriented programming features
…
Inheritance
Encapsulation Inheritance (structs inheriting from other structs), and encapsulation (struct name in the H file, and struct implementation in the C file) are certainly possible with C.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
David Crow wrote: struct name in the H file, and struct implementation in the C file That's not encapsulation, in any sense.
|
|
|
|
|
In object oriented programming languages, encapsulation is used to refer to one of two related but distinct notions, and sometimes to the combination thereof:
- A language mechanism for restricting direct access to some of the object's components.
- A language construct that facilitates the bundling of data with the methods (or other functions) operating on that data.
Opaque structs in C fit at least one of those definitions for abstraction. You could probably make the case that since you'd need to bundle the opaque struct with some subroutines to manipulate it that you're basically writing methods - the only difference is the class keyword and the lack of an implicit *this* pointer.
|
|
|
|
|
But you cannot do any form of abstraction or encapsulation in C. There is no mechanism for hiding members of a struct.
|
|
|
|
|
Richard MacCutchan wrote: There is no mechanism for hiding members of a struct. Are you saying that given the H and LIB files I mentioned earlier, that you'd be able to directly access the members of the CarPrivate struct?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
Sorry, I misread that. No you could not refer to the members if they are not defined in the header. But in most cases of writing pure C code this is not an issue. Both the caller and the provider need the definition of the struct in order to pass data between them.
|
|
|
|