473,386 Members | 1,668 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,386 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 1490
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
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...

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.