473,799 Members | 3,017 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

indentation

I have had several complaints by some people who wish to help me and I
wish to get the problem straight. I wrote this small utility myself and
added some indentation and I wonder if it is acceptable. It does make source
easier to read.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv) {
if (argc!=3) {
fprintf(stderr, "usage error\n");
return -1;
}
double x,y;
x=strtod(argv[1],NULL);
y=strtod(argv[2],NULL);
printf("%.2f\n" ,y/x);
return 0;
}

Is this a good example of a properly indended program?

Bill
Jun 27 '08
43 1983

"Richard Heathfield" <rj*@see.sig.in validwrote in message
news:HM******** *************** *******@bt.com. ..
cr88192 said:

<snip>
>>
now, I usually code in notepad,

Oh dear. :-)
all in all it is a good and simple editor...

>which has inflexible 8-space tabs, so
usually I use this.
if the tab space is adjustable, usually I like 4 space tabs.

Tab/space wars are so 1990s, though, aren't they?
yeah...
anymore, it is mostly forgotten, but I still usually use 4 or 8...

>2 or 3 spaces is IMO too little.

And IMO 3 is too many. Vive la difference!
yeah, it is a matter of taste I guess. 2 or 3 spaces IMO is painful to read
or skim...
>1 space is just horrid (may as well not indent at all...).

Agreed.
>usually, I put opening and closing braces on their own lines, and closing
braces are indended the same as the opening braces.

Agreed again.

You forgot <stdio.h>
yeah, I inferred this, since my point was more about demonstrating style
than actually working code.
>int main(int argc, char *argv[])
{
FILE *fd;
if(argv<2)

You meant argc.
yes, typing does not always work perfectly...

> {
printf("usage: %s <filename>\n" , argv[0]);

If argc is 0, the behaviour is undefined. If it is >= 1, argv[0] must
represent the program name in some way, but need not be a string
representing the invocation name for the program. It could even be a pid!
this is a common practice though, and also I don't personally know of any OS
where argc is not at least 1...

> return(-1);

This has no portable meaning (and the parentheses are redundantly
superfluous).
return(-1);
is the same as:
return -1;

only, the parens are a matter of style and tradition...
> }

fd=fopen(argv[1], "rb");
...
return(0);

Again, the parentheses are superfluously redundant.
>}

note that EXIT_SUCCESS and EXIT_FAILURE are considered "more correct" for
main return values,

0 is fine - it means success.
yeah.
>but 0 and -1 are more common/traditional.

A -1 return value has no de jure meaning in C (which is, at least, in
keeping with the better kinds of tradition - if we knew why we did them,
they wouldn't be traditions!).

To indicate failure portably, use EXIT_FAILURE.
it is more correct, but -1 is a very common value for indicating error as
well.
I think usually any value other than 0 counts as an error.

>IMO, both forms:
if(...)
{

and:
if(...) {

are fairly common and acceptable, but most people put the brace on its
own line for functions, and rarely for structs or unions.

The word "most" is arguable. K&R's style is perniciously persistent even
now. And a significant number of Allman adherents /do/ put a struct brace
on its own line.
I comment based on what I have most often seen, but these conventions are by
no means universal.

>it is common for commas to be followed by a space ("f(x, y);" but not
"f(x,y);").

True, and wise.
>some people precede/follow parens and/or operators with spaces.

True, and a matter of taste, I think. My own taste is for parentheses not
to "command" any whitespace, but for binary operators to be separated from
their operands by a space.
yeah, my case I don't usually use a space either (since to me the paren is
'strong', and putting a space there makes it seem 'weak'...).

my case, whether or not I use spaces around operators is a matter of
situation, where usually they are used for breaking up expressions into
recognizable parts, and sometimes for aligning groups of regularized
expressions.

sometimes terms for polynomial expressions can be broken up like this as
well.

4*a*c - 2*b

>if certain single-letter variable names are used (especially,
i,j,k,s,t,.. .) it is almost obligatory that they be certain types (i,j,k
are int, s,t are 'char *', ...).

No, not really. Common, yes. Obligatory? Hardly.
sufficiently common patterns are almost obligatory.
if one is going to use different types, they are better off avoiding these
names, for sake of reducing possible confusion.

>>
return is often/usually written as if it were a function call (common in
C, rare in C++).

return /isn't/ a function call, and it seems to me from perusing this
group
and from what I've seen of good C code (in well-regarded literature, in
workplaces, and on the Web) that few if any experienced C programmers
treat it like one.
well, as noted, it is a lot more common in C IME, but it is almost never
done in C++.

however, I have personally seen a lot more code with the parens than without
the parens...

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999

Jun 27 '08 #21
"Bill Cunningham" <no****@nspam.c omwrites:
I have had several complaints by some people who wish to help me and I
wish to get the problem straight. I wrote this small utility myself and
added some indentation and I wonder if it is acceptable. It does make source
easier to read.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv) {
if (argc!=3) {
fprintf(stderr, "usage error\n");
return -1;
}
double x,y;
x=strtod(argv[1],NULL);
y=strtod(argv[2],NULL);
printf("%.2f\n" ,y/x);
return 0;
}

Is this a good example of a properly indended program?

Bill
You are a very good troll I think! :-)
Jun 27 '08 #22
"cr88192" <cr*****@NOSPAM .hotmail.comwri tes:
"Richard Heathfield" <rj*@see.sig.in validwrote in message
news:HM******** *************** *******@bt.com. ..
>cr88192 said:

<snip>
>>>
now, I usually code in notepad,

Oh dear. :-)

all in all it is a good and simple editor...

>>which has inflexible 8-space tabs, so
usually I use this.
if the tab space is adjustable, usually I like 4 space tabs.

Tab/space wars are so 1990s, though, aren't they?

yeah...
anymore, it is mostly forgotten, but I still usually use 4 or 8...

>>2 or 3 spaces is IMO too little.

And IMO 3 is too many. Vive la difference!

yeah, it is a matter of taste I guess. 2 or 3 spaces IMO is painful to read
or skim...
>>1 space is just horrid (may as well not indent at all...).

Agreed.
>>usually, I put opening and closing braces on their own lines, and closing
braces are indended the same as the opening braces.

Agreed again.

You forgot <stdio.h>

yeah, I inferred this, since my point was more about demonstrating style
than actually working code.
>>int main(int argc, char *argv[])
{
FILE *fd;
if(argv<2)

You meant argc.

yes, typing does not always work perfectly...

>> {
printf("usage: %s <filename>\n" , argv[0]);

If argc is 0, the behaviour is undefined. If it is >= 1, argv[0] must
represent the program name in some way, but need not be a string
representing the invocation name for the program. It could even be a pid!

this is a common practice though, and also I don't personally know of any OS
where argc is not at least 1...

>> return(-1);

This has no portable meaning (and the parentheses are redundantly
superfluous) .

return(-1);
is the same as:
return -1;

only, the parens are a matter of style and tradition...
>> }

fd=fopen(argv[1], "rb");
...
return(0);

Again, the parentheses are superfluously redundant.
>>}

note that EXIT_SUCCESS and EXIT_FAILURE are considered "more correct" for
main return values,

0 is fine - it means success.

yeah.
>>but 0 and -1 are more common/traditional.

A -1 return value has no de jure meaning in C (which is, at least, in
keeping with the better kinds of tradition - if we knew why we did them,
they wouldn't be traditions!).

To indicate failure portably, use EXIT_FAILURE.

it is more correct, but -1 is a very common value for indicating error as
well.
I think usually any value other than 0 counts as an error.

>>IMO, both forms:
if(...)
{

and:
if(...) {

are fairly common and acceptable, but most people put the brace on its
own line for functions, and rarely for structs or unions.

The word "most" is arguable. K&R's style is perniciously persistent even
now. And a significant number of Allman adherents /do/ put a struct brace
on its own line.

I comment based on what I have most often seen, but these conventions are by
no means universal.

>>it is common for commas to be followed by a space ("f(x, y);" but not
"f(x,y);").

True, and wise.
>>some people precede/follow parens and/or operators with spaces.

True, and a matter of taste, I think. My own taste is for parentheses not
to "command" any whitespace, but for binary operators to be separated from
their operands by a space.

yeah, my case I don't usually use a space either (since to me the paren is
'strong', and putting a space there makes it seem 'weak'...).

my case, whether or not I use spaces around operators is a matter of
situation, where usually they are used for breaking up expressions into
recognizable parts, and sometimes for aligning groups of regularized
expressions.

sometimes terms for polynomial expressions can be broken up like this as
well.

4*a*c - 2*b

>>if certain single-letter variable names are used (especially,
i,j,k,s,t,... ) it is almost obligatory that they be certain types (i,j,k
are int, s,t are 'char *', ...).

No, not really. Common, yes. Obligatory? Hardly.

sufficiently common patterns are almost obligatory.
if one is going to use different types, they are better off avoiding these
names, for sake of reducing possible confusion.

>>>
return is often/usually written as if it were a function call (common in
C, rare in C++).

return /isn't/ a function call, and it seems to me from perusing this
group
and from what I've seen of good C code (in well-regarded literature, in
workplaces, and on the Web) that few if any experienced C programmers
treat it like one.

well, as noted, it is a lot more common in C IME, but it is almost never
done in C++.

however, I have personally seen a lot more code with the parens than without
the parens...

I agree with people who write real apps accessed by many peoples. And
these are:

It is clear and it works.

http://www.linuxjournal.com/article/5780
Jun 27 '08 #23
On May 8, 11:33*am, "cr88192" <cr88...@NOSPAM .hotmail.comwro te:
"Richard Heathfield" <r...@see.sig.i nvalidwrote in message

news:HM******** *************** *******@bt.com. ..
cr88192 said:
<snip>
now, I usually code in notepad,
Oh dear. :-)

all in all it is a good and simple editor...
(So you can write some very complex software but not an editor better
than notepad?..)

If you like notepad, you might like to try qed.exe, if you can still
find it. (I think an updated version here http://www.movsd.com/qed.htm).
It has some useful indent commands. And I believe still smaller than
notepad.

--
Bartc
Jun 27 '08 #24
Right, so let's do that. See 2.1.2.2 of C89 or 5.1.2.2.1 of C99:

* The value of argc shall be nonnegative.

* argv[argc] shall be a null pointer.

* If the value of argc is greater than zero, the array members
argv[0] through argv[argc-1] inclusive shall contain pointers to
strings, which are given implementation-defined values by the host
environment prior to program startup. The intent is to supply to the
program information determined prior to program startup from elsewhere
in the hosted environment. [...]

It is clear from the above that argc may be 0.
You are rigth... till now i've never found a system that pass a 0 as
argc, but it should exist,
maybe on a very small controllers environment...
>
>>> return(-1);
This has no portable meaning (and the parentheses are redundantly
superfluous ).

I'm not agree, it' more readable.

Please explain why parentheses make the return code more readable. Does
this apply to every expression, or just to the expression that
(optionally) follows a return statement? If so, why is return special?
It's just a question of style. I prefer to place parentheses. Return is
not special, so it
is trated in the same way of all other cases where parentheses should be
used also
when they are superfluous.
What makes you think that adding parentheses helps? If the reader can't
comprehend a*32 / b + c - d * e / oh_my_good, why are they more likely to
comprehend (a*32 / b + c - d * e / oh_my_good)?
Yea, but of couse for me is much better to use parentheses:
retun( (((z*100)/y)+((w-r)*k)) / u );

That it is a non-ambiguous form, readable also from people with little
knowledge
of the language and not familiar with the specific operator's precedence.

b.

Jun 27 '08 #25
brix99luftballo ns said:

<snip>

till now i've never found a system that pass a 0 as
argc, but it should exist,
maybe on a very small controllers environment...
Macs and Unices aren't that small.
>Please explain why parentheses make the return code more readable. Does
this apply to every expression, or just to the expression that
(optionally) follows a return statement? If so, why is return special?
It's just a question of style. I prefer to place parentheses. Return is
not special,
Agreed. So why adorn its expression with parentheses? = takes an expression
as its right operand. Do you write a = (b)? Or how about +, which takes
two expressions as its operands. Do you write: a = (b) + (c) ?

If so, why? And if not, why treat return differently?
so it is trated in the same way of all other cases where parentheses
should be used also when they are superfluous.
If they are superfluous, why use them at all? I can understand why you
might add them in places where the precedence isn't obvious, e.g. in
something like (a << b) + c - but surrounding an expression with
parentheses does nothing to clarify precedence within the expression
itself.
>What makes you think that adding parentheses helps? If the reader can't
comprehend a*32 / b + c - d * e / oh_my_good, why are they more likely
to comprehend (a*32 / b + c - d * e / oh_my_good)?
Yea, but of couse for me is much better to use parentheses:
Why?
retun( (((z*100)/y)+((w-r)*k)) / u );

That it is a non-ambiguous form,
So is the original.
readable also from people with little
knowledge
of the language and not familiar with the specific operator's precedence.
How, precisely, does changing return 0; to return(0); clarify precedence?

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #26
Keith Thompson <ks***@mib.orgw rote:
2. Opening and closing braces appear on lines by themselves.
Enclosed lines are indented by one level.
Rarer, but still valid, styles are:
3. Like 2, but the braces are aligned with the enclosed lines.
4. Like 2, but the braces are half-indented (e.g., by 2 columns).
I think this is the GNU-recommended style.
4.
int main(int argc, char **argv)
{
if (argc != 3)
{
fprintf(stderr, "usage error\n");
return -1;
}
double x, y;
x = strtod(argv[1], NULL);
y = strtod(argv[2], NULL);
printf("%.2f\n" , y / x);
return 0;
}
That's not exactly GNU style, which is like this:

int
main (int argc, char **argv)
{
if (argc != 3)
{
fprintf(stderr, "usage error\n");
return -1;
}
double x, y;
x = strtod (argv[1], NULL);
y = strtod (argv[2], NULL);
printf ("%.2f\n", y / x);
return 0;
}

Andrew.
Jun 27 '08 #27
Richard Heathfield <rj*@see.sig.in validwrites:
brix99luftballo ns said:

<snip>

till now i've never found a system that pass a 0 as
>argc, but it should exist,
maybe on a very small controllers environment...

Macs and Unices aren't that small.
Most systems can be made to pass a zero, but Unices (I prefer *nix)
don't do so as a matter of course.

<snip>
>so it is trated in the same way of all other cases where parentheses
should be used also when they are superfluous.

If they are superfluous, why use them at all? I can understand why you
might add them in places where the precedence isn't obvious, e.g. in
something like (a << b) + c - but surrounding an expression with
parentheses does nothing to clarify precedence within the expression
itself.
You are being your usual argumentative self! Can't you see a reason
why return (E); could be seen as a reasonable generalisation? if,
while, switch etc all have syntactic ()s that at least *look*
superfluous (yes, I know they are not so). From that point of view a
return without them look out of place.

The language designers certainly thought so. I, for one, had to
*un-learn* return (E); since it was always thus in B and it was also
required in very early C. To be portable, you wrote return (E); just in
case your code got near an ancient compiler.

Of course those days are long gone and I now prefer a minimal use of
parentheses.

--
Ben.
Jun 27 '08 #28
Ben Bacarisse said:

<snip>
Most systems can be made to pass a zero, but Unices (I prefer *nix)
don't do so as a matter of course.
Granted.

<snip>
You are being your usual argumentative self!
Well, I'm being my usual logical self. :-)
Can't you see a reason
why return (E); could be seen as a reasonable generalisation?
Do you really want to *generalise* the parenthesisatio n of expressions?

<snip>
Of course those days are long gone and I now prefer a minimal use of
parentheses.
Precisely.

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #29

"rio" <a@b.cwrote in message
news:48******** *************** @reader4.news.t in.it...
i like below

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{double x, y;

if (argc!=3) { fprintf(stderr, "usage error\n"); return -1;}

x=strtod(argv[1], 0);
y=strtod(argv[2], 0);

return (x!=0.0) ?
(printf("%.2f\n ", y/x), 0): (printf("Error: x==0\n"), -1) ;
}
This is the part that goes over my head with my knowledge.

return (x!=0.0) ? /*what's the ? and 0.0 for */

There are things in C, shortcuts that I haven't grasped yet like += and ?: I
do things the long way right now.

Bill
Jun 27 '08 #30

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

Similar topics

0
1256
by: Magnus Lie Hetland | last post by:
Not many members on the Atox mailing list yet, so I'll venture a request here... In Atox 0.2, I've added support for indentation tokens (somewhat like the Python indentation scheme, but a bit more relaxed). If anyone finds that sort of thing interesting, please have a look and give me your opinion -- does it seem like a useful way of dealing with this sort of thing? (It seems that lookahead is more sorely needed with indentation, to
147
7791
by: Sateesh | last post by:
Hi, I am a beginner in Python, and am wondering what is it about the indentation in Python, without which python scripts do not work properly. Why can't the indentation not so strict so as to give better freedom to the user? Is there any plausible reason behind this? Cheers! Sateesh
177
7101
by: C# Learner | last post by:
Why is C syntax so uneasy on the eye? In its day, was it _really_ designed by snobby programmers to scare away potential "n00bs"? If so, and after 50+ years of programming research, why are programming languages still being designed with C's syntax? These questions drive me insane. Every waking minute...
7
5878
by: diffuser78 | last post by:
I am a newbie to Python. I am mainly using Eric as the IDE for coding. Also, using VIM and gedit sometimes. I had this wierd problem of indentation. My code was 100% right but it wont run because indentation was not right. I checked time and again but still no success. I rewrote the code over again in VI and it ran. Can you please explain whats the trick behind the correct indentation. Thanks
9
3182
by: John Salerno | last post by:
How do you make a single string span multiple lines, but also allow yourself to indent the second (third, etc.) lines so that it lines up where you want it, without causing the newlines and tabs or spaces to be added to the string as well? Example (pretend this is all on one line): self.DTD = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"\n"http://www.w3.org/TR/html4/strict.dtd">\n\n'
135
7535
by: Xah Lee | last post by:
Tabs versus Spaces in Source Code Xah Lee, 2006-05-13 In coding a computer program, there's often the choices of tabs or spaces for code indentation. There is a large amount of confusion about which is better. It has become what's known as “religious war” — a heated fight over trivia. In this essay, i like to explain what is the situation behind it, and which is proper.
4
1805
by: bearophileHUGS | last post by:
This is the best praise of semantic indentation I have read so far, by Chris Okasaki: http://okasaki.blogspot.com/2008/02/in-praise-of-mandatory-indentation-for.html A quotation: I have appreciated that article, and I have personally seen how fast students learn Python basics compared to other languages, but I think that it's way more than just indentation that makes the Python language so quick to learn .
1
1688
by: Eric S. Johansson | last post by:
in trying to make programming in Python more accessible to disabled programmers (specifically mobility impaired speech recognition users), and hitting a bit of a wall. The wall (for today) is indentation. I need a method of getting the "right indentation" without having to speak a bunch of unnecessary commands. For example, depth specified by the previous line. But, frequently you need to go to a more arbitrary indentation for example...
19
2108
by: Eric S. Johansson | last post by:
Almar Klein wrote: there's nothing like self interest to drive one's initiative. :-) 14 years with speech recognition and counting. I'm so looking to my 15th anniversary of being injured next year.... another initiative is exporting the speech recognition environment to the Linux context. In a nutshell, he dictated to application on Windows, it tunnels over the network to a Linux machine, and will allow you to cut and paste to and...
0
9685
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, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9538
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10473
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...
1
10219
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,...
0
10025
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9068
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 projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7563
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
5584
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3755
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.