473,406 Members | 2,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Exercise 5-14

mdh
I have Tondo and Gimpel. There are a couple of aspects of their
answer that puzzles me.

The Question is to modify a sort program to handle a -r flag
( function is irrelevant), which needs to wrok together with a -n flag
( function again irrelevant..I believe).

Firstly, a very brief outline of the function.

......numerous function declarations;

#define NUMERIC 1

static char option = 0;
main (int argc, char *argv[])
{

.....snip....
while ( --argc 0 && (*++argv)[0] == '-')
...snip...

switch (c){

case 'n':

option |= NUMERIC;

....snip...
}
My first question relates to the use of the qualifyer "static";

My understanding ( or lack of) is that a static variable outside of a
function limits the scope of that variable to that file. Is this just
good programming by T&G or is there another good reason for this?

Secondly, is this use of bitwise operators commonly used to set and
get flags in a program in C?

Thanks in advance.

Jul 14 '08 #1
4 1492
On Jul 14, 8:56*am, mdh <m...@comcast.netwrote:
My understanding ( or lack of) is that a static variable outside of a
function limits the scope of that variable to that *file. Is this just
good programming by T&G or is there another good reason for this?
Statics has got internal linkage. They won't be visible outside this
file. If some other object code you link with has got an identifier
called option, collision/shadowing won't happen as option is static.
It is considered a good programming practice to declare variables and
function as static if you intend to use them just in the given file.
Also, static data is initialized and won't contain garbage value. It
does not make a difference as it has been explicitly declared to be 0.
It is again a good practice to explicitly initialize static and global
variables.
Secondly, is this use of bitwise operators commonly used to set and
get flags in a program in C?
It more or less depends on your style. But yes, it does seem very
common in POSIX API(probably even Win32 API, but I have not used much
of that).

Jul 14 '08 #2
mdh
On Jul 13, 9:42*pm, rahul <rahulsin...@gmail.comwrote:
On Jul 14, 8:56*am, mdh <m...@comcast.netwrote:
My understanding ( or lack of) is that a static variable outside of a
function limits the scope of that variable to that *file. Is this just
good programming by T&G or is there another good reason for this?

Statics has got internal linkage. They won't be visible outside this
file. If some other object code *you link with has got an identifier
called option, collision/shadowing won't happen as option is static.
It is considered a good programming practice to declare variables and
function as static if you intend to use them just in the given file.
Also, static data is initialized and won't contain garbage value. It
does not make a difference as it has been explicitly declared to be 0.
It is again a good practice to explicitly initialize static and global
variables.
Secondly, is this use of bitwise operators commonly used to set and
get flags in a program in C?

It more or less depends on your style. But yes, it does seem very
common in POSIX API(probably even Win32 API, but I have not used much
of that).
Thanks Rahul. Just wanted to make sure.
Jul 14 '08 #3
mdh wrote:
I have Tondo and Gimpel. There are a couple of aspects of their
answer that puzzles me.

The Question is to modify a sort program to handle a -r flag
( function is irrelevant), which needs to wrok together with a -n flag
( function again irrelevant..I believe).

Firstly, a very brief outline of the function.

.....numerous function declarations;

#define NUMERIC 1

static char option = 0;
main (int argc, char *argv[])
{

....snip....
while ( --argc 0 && (*++argv)[0] == '-')
...snip...

switch (c){

case 'n':

option |= NUMERIC;

...snip...
}
My first question relates to the use of the qualifyer "static";

My understanding ( or lack of) is that a static variable outside of a
function limits the scope of that variable to that file. Is this just
good programming by T&G or is there another good reason for this?
The static qualifier can be used on both functions and objects and with
both functions and external objects it limits their visibility to the
rest of that translation unit. It's commonly used for this purpose to
mitigate excessive namespace pollution.
Secondly, is this use of bitwise operators commonly used to set and
get flags in a program in C?
Very much so. This is usually more easier than using bitfields though a
beginner may find manipulating the latter more intuitive.

PS. Note that uninitialised static objects are automatically initialised
to zero, but it's good practise to explicitly initialise them anyway.

Jul 14 '08 #4
mdh
On Jul 13, 10:52*pm, santosh <santosh....@gmail.comwrote:
mdh wrote:
I have Tondo and Gimpel. There are a couple of *aspects of their
answer that puzzles me.
The Question *is to modify a sort program to handle a -r flag
( function is irrelevant), which needs to wrok together with a -n flag
( function again irrelevant..I believe).
Firstly, a very brief outline of the function.
.....numerous function declarations;
#define NUMERIC 1
static char option = 0;
main (int argc, char *argv[])
{
....snip....
while ( --argc 0 && (*++argv)[0] == '-')
*...snip...
switch (c){
case 'n':
option |= NUMERIC;
...snip...
}
My first question relates to the use of the qualifyer "static";
My understanding ( or lack of) is that a static variable outside of a
function limits the scope of that variable to that *file. Is this just
good programming by T&G or is there another good reason for this?

The static qualifier can be used on both functions and objects and with
both functions and external objects it limits their visibility to the
rest of that translation unit. It's commonly used for this purpose to
mitigate excessive namespace pollution.
Secondly, is this use of bitwise operators commonly used to set and
get flags in a program in C?

Very much so. This is usually more easier than using bitfields though a
beginner may find manipulating the latter more intuitive.

PS. Note that uninitialised static objects are automatically initialised
to zero, but it's good practise to explicitly initialise them anyway.
Thanks Santosh
Jul 14 '08 #5

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

Similar topics

5
by: Charles | last post by:
I am going through the exercises and Bruce Eckel's Thinking in C++ and I ran into an exercise that wasn't included in his solutions that I think I could use some assistance with. Exercise 3-26...
12
by: Merrill & Michele | last post by:
It's very difficult to do an exercise with elementary tools. It took me about fifteen minutes to get exercise 1-7: #include <stdio.h> int main(int orange, char **apple) { int c; c=-5;...
6
by: sathyashrayan | last post by:
Dear group, Following is a exercise from a book called "Oreilly's practical C programming". I just wanted to do a couple of C programming exercise. I do have K and R book, but let me try some...
8
by: Mike S | last post by:
Hi all, I noticed a very slight logic error in the solution to K&R Exercise 1-22 on the the CLC-Wiki, located at http://www.clc-wiki.net/wiki/KR2_Exercise_1-22 The exercise reads as...
16
by: Josh Zenker | last post by:
This is my attempt at exercise 1-10 in K&R2. The code looks sloppy to me. Is there a more elegant way to do this? #include <stdio.h> /* copies input to output, printing */ /* series of...
5
by: ebrimagillen | last post by:
Hello mates, Just needed a solution on the exercises below. Exercise 2 A classic problem in introductory programming is the grains of rice on a chess board problem. Your program should...
5
by: Richard Gromstein | last post by:
Hello, I have an exercise that I need to finish very soon and I really need help understanding what to do and how exactly to do it. I am working on reading the chapter right now and working on it...
26
by: arnuld | last post by:
this is the programme i created, for exercise 2, assignment 3 at http://www.eskimo.com/~scs/cclass/asgn.beg/PS2.html it runs fine. i wanted to know if it needs any improvement: ...
19
by: arnuld | last post by:
this programme runs without any error but it does not do what i want it to do: ------------- PROGRAMME -------------- /* K&R2, section 1.6 Arrays; Exercise 1-13. STATEMENT: Write a program...
5
by: arnuld | last post by:
this is a programme that counts the "lengths" of each word and then prints that many of stars(*) on the output . it is a modified form of K&R2 exercise 1-13. the programme runs without any...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.