Re: Just curiosity about some constructs
- From: "Vladimir S. Oka" <novine@xxxxxxxxxxxxxxx>
- Date: 31 Jan 2006 08:01:29 -0800
Sensei wrote:
> I have still curiosity about the reason of some C constructs/keywords...
>
> The first is about static functions. What was the reason of restricting
> a function to be visible just in a specific source file? Wasn't it
> sufficient not to be given a prototype (for visibility)?
If you don't make the function `static`, at the linking stage it may be
"found" by the linker looking for a function of the same name in the
object files provided, even if you did not provide its prototype in
other source files. To ensure your function is never "found" in this
way (i.e. is accessible only to other functions within the same source
file) you declare it `static`.
> What about register and volatile variables? Was at that time a compiler
> not smart enough to optimize with in-register variables? And why would
> someone suggest the compiler not to optimze by making a variable
> volatile?
The `register` variables were introduced at the time when compilers
were not as good at optimising, you got that one correctly.
The `volatile`, however, does /not/ mean "do not optimise". It means
"this variable may change behind your back, through no action of your
program, so do not make any assumptions about its value". In most, but
not all, cases this does in effect prevent the compiler from optimising
bits of code that refer to a `volatile` variable.
> Last question! This is about the switch statement. The statement seems
> to me to be completely different from others. Let me explain with an
> example. A while(condition) will execute the statement after the
> while(), and if someone wants to have more instructions to be executed
> in the loop, then { } should be used. This is true also for if/else,
> do/loop, but not with the switch. A case does not require any { } to
> execute more than one instruction, moreover, a brake must be given to
> make a single case being executed, otherwise all the following
> non-brake case statements will be executed. Why wasn't the switch like
> the others with { } and automatic brake?
I'm sure someone will correct me if I'm wrong, but I think that the
philosophy of the `switch` statement has historical reasons, or some
rationale that today, and if you don't know "the story of C", does not
make much sense. However, I do not think that the way it works is in
any way "wrong" or "strange", just "not as expected, or usual, given
other programming languages in existence today". IMHO, in many cases,
the way it works is actually preferrable. The only downside I can think
of is some extra typing for `break` statements (but you save on /not/
typing the braces! ;-) ).
> It's probably useless, but not for curiosity...
I don't quite follow your thought here...
Cheers
Vladimir
.
- Follow-Ups:
- Re: Just curiosity about some constructs
- From: Sensei
- Re: Just curiosity about some constructs
- References:
- Just curiosity about some constructs
- From: Sensei
- Just curiosity about some constructs
- Prev by Date: Re: how to develop library ( .a and .so files) ! URGENT!!!
- Next by Date: Re: Find the Data Type of Variable
- Previous by thread: Re: Just curiosity about some constructs
- Next by thread: Re: Just curiosity about some constructs
- Index(es):
Relevant Pages
|