473,748 Members | 7,118 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Just curiosity about some constructs

Hi again!

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)?

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?

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?

It's probably useless, but not for curiosity...

--
Sensei <se******@mac.c om>

Part of the inhumanity of the computer is that, once it is competently
programmed and working smoothly, it is completely honest. (Isaac Asimov)

Jan 31 '06 #1
11 1605


Sensei wrote On 01/31/06 10:01,:
Hi again!

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)?
No; you are confusing "scope" and "linkage." Without
`static', a function identifier has "external linkage," which
means that the name can refer to only one thing in the entire
program, no matter how many translation units are used. If
you have `void foo(void) {...}' in one file and in another
you write `double foo(char *ptr) {...}', the identifier has
external linkage in both and the two uses clash. With `static',
the identifier has "internal linkage," meaning that it does
not clash with any uses of `foo' (external or internal) in
other translation units.

A typical use is for a translation unit ("module") to
define some number of functions (and/or variables) with
external linkage, so code in other translation units can
refer to them. Meanwhile, "helper" functions (and/or
variables) are declared `static' so other modules cannot
refer to them and so their names won't clash with identifiers
used in those other modules. Think of `static' in this
sense as meaning "private."
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?
Compilers today are much better at optimizing than those
of the past. This is partly because of advances in the state
of the art, but mostly because the machines that run the
compilers are much larger and faster than those of the past.
The computations a modern compiler makes while performing its
optimizations would have taken far too much time and memory
to make them feasible on machines with 64KB of memory and a
250KHz CPU. `register' was a helpful hint to resource-strapped
compilers; nowadays it is largely useless.

`volatile' is a different matter, and is still relevant.
It tells the compiler that it is not safe to optimize the
accesses to a variable, usually because the variable can
change for reasons outside the compiler's knowledge or because
the act of making the access has some side-effect the compiler
cannot know about. One common example is the use of memory
locations that connect to I/O control registers, hardware
clocks, and so on. You might write a delay loop like

while (*hardware_time r < target_time)
;

.... and you'd be, er, "disappoint ed" if the compiler turned
this into the equivalent of

if (*hardware_time r < target_time)
while (1)
;

.... by optimizing away the "redundant" fetches of the value.
Similarly, you might control an I/O device by writing a
series of command codes to a special location:

*control_word = SEEK_TO_CYLINDE R + cyl_num;
*control_word = START_WRITING;

.... and you'd be disappointed again if the compiler decided
to discard the first assignment on the grounds that the
value would be immediately overwritten by the second.
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?


The case labels in a `switch' are exactly that: labels
for single points in the code, not identifiers of regions
of code. That makes it easy to send several values to the
same piece of code:

switch (character) {
case ' ':
case '\t':
case '\n':
case '\f':
case '\r':
process_white_s pace();
break;

case '.':
case ',':
case ':':
process_punctua tion();
break;

...
}

Many languages have a construct of this general flavor, not
all with quite the same rules. Why dmr chose this particular
scheme is a question only he can answer. His choice has been
both widely criticized and widely applauded.

--
Er*********@sun .com

Jan 31 '06 #2
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

Jan 31 '06 #3
On 2006-01-31 17:01:29 +0100, "Vladimir S. Oka" <no****@btopenw orld.com> said:

It's probably useless, but not for curiosity...


I don't quite follow your thought here...


Well, questions like this one can be seen as useless. I asked this just
for curiosity, not for any ``higher'' intention... :)

--
Sensei <se******@mac.c om>

Part of the inhumanity of the computer is that, once it is competently
programmed and working smoothly, it is completely honest. (Isaac Asimov)

Jan 31 '06 #4
>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)?
No. The names would still conflict at link time.
And early C had no prototypes, per se, although there were
declarations which declared the return type (but not argument types).
What about register and volatile variables? Was at that time a compiler
not smart enough to optimize with in-register variables?
Yes, and sometimes people wanted to give the compiler hints
that the default optimization wasn't doing the best it could.
And why would
someone suggest the compiler not to optimze by making a variable
volatile?
Because the variable can be changed by *SOMETHING ELSE*. Either
the variable isn't memory but a hardware register, or it's modified
by a signal handler, another thread, etc.

while (siop->statusregist er & TX_BUFFER_EMPTY )
/* wait */ ;

The classic problem comes when the compiler loads the value once,
then goes into an infinite loop rather than checking when the flag
comes on repeatedly.

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?


The switch *DOES* use { }, but it's very, very, very rarely appropriate
to use switch without { }.

You can do the same thing within { } (anywhere, as in after a while)
with labels and goto (although case labels are a little different
from regular labels, they are still labels).

Gordon L. Burditt
Jan 31 '06 #5

Sensei wrote:
On 2006-01-31 17:01:29 +0100, "Vladimir S. Oka" <no****@btopenw orld.com> said:

It's probably useless, but not for curiosity...


I don't quite follow your thought here...


Well, questions like this one can be seen as useless. I asked this just
for curiosity, not for any ``higher'' intention... :)


Asking out of curiosity is not useless, as it can lead to increased
knowledge. Knowledge is rarely useless. Carry on wondering...

Cheers

Vladimir

Jan 31 '06 #6
Eric Sosman wrote:
Sensei wrote On 01/31/06 10:01,:
.... snip ...
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?


The case labels in a `switch' are exactly that: labels
for single points in the code, not identifiers of regions
of code. That makes it easy to send several values to the
same piece of code:

switch (character) {
case ' ':
case '\t':
case '\n':
case '\f':
case '\r':
process_white_s pace();
break;

case '.':
case ',':
case ':':
process_punctua tion();
break;

...
}

Many languages have a construct of this general flavor, not
all with quite the same rules. Why dmr chose this particular
scheme is a question only he can answer. His choice has been
both widely criticized and widely applauded.


Since the case labels are exactly that I format switch statements
to show such. i.e.:

switch (character) {
case ' ':
case '\t':
case '\n':
case '\f':
case '\r': process_white_s pace();
break;

case '.':
case ',':
case ':': process_punctua tion();
break;
...
default: ...
} /* switch */

by pulling the labels out to the left margin. I will then indent
other nested switch labels. Similarly I pull goto labels out to
the left. The indentation then shows flow of control properly.

--
"The power of the Executive to cast a man into prison without
formulating any charge known to the law, and particularly to
deny him the judgement of his peers, is in the highest degree
odious and is the foundation of all totalitarian government
whether Nazi or Communist." -- W. Churchill, Nov 21, 1943
Feb 1 '06 #7

"Sensei" <se******@mac.c om> wrote in message
news:43******** *************** @reader2.news.t in.it...
Hi again!

I have still curiosity about the reason of some C constructs/keywords...
Last question! This is about the switch statement. The statement seems
to me to be completely different from others. A case does not require any { } to
execute more than one instruction, moreover, a brake[sic, break] must be given to make a single case being executed, otherwise all the following
non-brake[sic] case statements will be executed. Why wasn't the switch like the others with { } and automatic brake[sic]?


The ultimate example of this can be found in "C: A Reference Manual" by
Samuel Harbison and Guy Steele, Tartan Laboratories, 3rd. edition, on page
231:

switch (x)
default:
if (prime(x))
case 2: case 3: case 5: case 7:
process_prime(x );
else
case 4: case 6: case 8: case 9: case 10:
process_composi te(x);

The only other situation where I've seen an unstructured switch was to
select between two while's at the end of a do-while loop. Otherwise,
structured programming of switch is the way to go (using {}). I think the
reason behind unstructured elements in C, like 'goto' and 'switch', was to
allow porting of Fortran, which is unstructured, to C.

Rod Pemberton
Feb 1 '06 #8
"Rod Pemberton" <do*******@bitb ucket.cmm> writes:
[...]
The ultimate example of this can be found in "C: A Reference Manual" by
Samuel Harbison and Guy Steele, Tartan Laboratories, 3rd. edition, on page
231:

switch (x)
default:
if (prime(x))
case 2: case 3: case 5: case 7:
process_prime(x );
else
case 4: case 6: case 8: case 9: case 10:
process_composi te(x);

The only other situation where I've seen an unstructured switch was to
select between two while's at the end of a do-while loop. Otherwise,
structured programming of switch is the way to go (using {}). I think the
reason behind unstructured elements in C, like 'goto' and 'switch', was to


See also Duff's Device, <http://www.c-faq.com/misc/duff.html>.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 1 '06 #9

"Keith Thompson" <ks***@mib.or g> wrote in message
news:ln******** ****@nuthaus.mi b.org...
"Rod Pemberton" <do*******@bitb ucket.cmm> writes:
[...]
The ultimate example of this can be found in "C: A Reference Manual" by
Samuel Harbison and Guy Steele, Tartan Laboratories, 3rd. edition, on page 231:

switch (x)
default:
if (prime(x))
case 2: case 3: case 5: case 7:
process_prime(x );
else
case 4: case 6: case 8: case 9: case 10:
process_composi te(x);

The only other situation where I've seen an unstructured switch was to
select between two while's at the end of a do-while loop. Otherwise,
structured programming of switch is the way to go (using {}). I think the reason behind unstructured elements in C, like 'goto' and 'switch', was
to
See also Duff's Device, <http://www.c-faq.com/misc/duff.html>.


Cool! I don't recall seeing that one, although I'm sure I read Stroustrup's
book, just not as many times as Harbison's and Steele's, I guess. But on
the web, Tom Duff, regarding his "Duff's Device" said: "(Actually, I have
another revolting way to use switches to implement interrupt driven state
machines but it's too horrid to go into.)" So, we are still missing the
_ULTIMATE_ unstructured switch. :( The worst (or best?) part was I
understood Duff's Device before reading the description. :(
Rod Pemberton

Feb 1 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1357
by: Web Science | last post by:
Site and Features: http://www.eigensearch.com Search engine, eigenMethod, eigenvector, mathematical, manifolds, science, technical, search tools, eigenmath, Jacobian, quantum, mechanics, manifolds, science, physics, chemistry, law, legal, government, home, office, business, domain lookup, medical, travel, food, university students, searching, searchers, surfing, advanced search, search tools Chemistry, mathematics, physical sciences,...
1
1831
by: Web Science | last post by:
Site and Features: http://www.eigensearch.com Search engine, eigenMethod, eigenvector, mathematical, manifolds, science, technical, search tools, eigenmath, Jacobian, quantum, mechanics, manifolds, science, physics, chemistry, law, legal, government, home, office, business, domain lookup, medical, travel, food, university students, searching, searchers, surfing, advanced search, search tools Chemistry, mathematics, physical sciences,...
22
1739
by: chris | last post by:
I have to retreive data from a old DOS program written in 1995 but I don't recognize the language -- Here's a sample of the code used in the .lib files -- Can anyone tell me what language it is ? Thanks in advance. Here's the code sample : if cmpy=0 cmpy+ selsuc{} usestk{ii,is,"inf\rech"} sucrec=ide.sucrec
193
9612
by: Michael B. | last post by:
I was just thinking about this, specifically wondering if there's any features that the C specification currently lacks, and which may be included in some future standardization. Of course, I speak only of features in the spirit of C; something like object-orientation, though a nice feature, does not belong in C. Something like being able to #define a #define would be very handy, though, e.g: #define DECLARE_FOO(bar) #define...
5
2416
by: masood.iqbal | last post by:
My simplistic mind tells me that having local variables within looping constructs is a bad idea. The reason is that these variables are created during the beginning of an iteration and deleted at the end of the iteration. Kindly note that I am not talking about dynamically allocating memory within a loop, which is a perfectly valid operation (albeit with a chance of resulting in a memory leak, unless we are careful). The most common...
0
983
by: Web Science | last post by:
Site and Features: http://www.eigensearch.com Search engine, eigenMethod, eigenvector, mathematical, manifolds, science, technical, search tools, eigenmath, Jacobian, quantum, mechanics, manifolds, science, physics, chemistry, law, legal, government, home, office, business, domain lookup, medical, travel, food, university students, searching, searchers, surfing, advanced search, search tools Chemistry, mathematics, physical sciences,...
0
914
by: Web Science | last post by:
Site and Features: http://www.eigensearch.com Search engine, eigenMethod, eigenvector, mathematical, manifolds, science, technical, search tools, eigenmath, Jacobian, quantum, mechanics, manifolds, science, physics, chemistry, law, legal, government, home, office, business, domain lookup, medical, travel, food, university students, searching, searchers, surfing, advanced search, search tools Chemistry, mathematics, physical sciences,...
21
2376
by: shotokan99 | last post by:
guys, i read in some forums that php is just for hobbiest, and is not suited for some serius and heavy application. for robust and hi performance application asp.net or jsp is the way to go. how true is this? however if talking about robustnes and bulk of data, friendster is written on php and doing well. correct me if im wrong with this. tnx
26
1856
by: Frank Samuelson | last post by:
I love Python, and it is one of my 2 favorite languages. I would suggest that Python steal some aspects of the S language. ------------------------------------------------------- 1. Currently in Python def foo(x,y): ... assigns the name foo to a function object. Is this pythonic? Why not use the = operator like most other assignments?
0
8984
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9530
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9363
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9312
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6793
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4593
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2206
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.