473,786 Members | 2,375 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 #1
43 1982
Bill Cunningham wrote:
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

Find the GNU 'indent' utility. It gives us..

#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;
}

...given your program as input. What do you think?

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Jun 27 '08 #2
Bill Cunningham wrote:
>
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?
Close. The main problems are too little indentation (I suggest 3
spaces) and the lack of spaces in the source. Also the variable
declaration should not occur after executable code. Compare the
following:

#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], NULL);
y = strtod(argv[2], NULL);
printf("%.2f\n" , y/x);
return 0;
} /* main */

I like to mark the closing brace of a function with the function
name.

There is a program around called indent (GNU version 2.2.9 here)
which does all this for you. It is configurable. I use the
following configuration for it (really just one long line in
indent.pro):

-kr -l66 -i3 -bad -di16 -lc66 -nce -ncs -cbi0 -bbo -pmt -psl -ts1
-cdw -ppi 3

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home .att.net>
Try the download section.
** Posted from http://www.teranews.com **
Jun 27 '08 #3

"Joe Wright" <jo********@com cast.netwrote in message
news:xd******** *************** *******@comcast .com...
..given your program as input. What do you think?
The first copy of the file I wrote has had its indentation changed by
the news server or client. It was indented when I posted it and the
indentation was removed. Maybe nntp does that. The progran was originally
indented like this.

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

Thatis what was copied onto the post and it was posted without the
indentation.

Bill
Jun 27 '08 #4
Bill Cunningham wrote:
"Joe Wright" <jo********@com cast.netwrote in message
news:xd******** *************** *******@comcast .com...
>..given your program as input. What do you think?

The first copy of the file I wrote has had its indentation changed by
the news server or client. It was indented when I posted it and the
indentation was removed. Maybe nntp does that. The progran was originally
indented like this.
You probably used tabs rather than spaces. Stick to (two) spaces and
your code will be left unmolested.

--
Ian Collins.
Jun 27 '08 #5
Keith Thompson wrote:
"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?

Better, but not yet good.

A single space per indentation level is IMHO insufficient.
It's still very hard to see what's indented. I like 4-column
indentation myself; I consider 2 to be the bare minimum. (Some
advocate 8-column indentation, which certainly encourages Any
decent text editor should do most of the work for you automatically
(":se ai" or ":set autoindent" in vi, for example).

There are several common styles for indentation and brace placement.
Your program exhibits none of them. That's not necessarily A Bad
Thing, but it's a bit jarring, and there's rarely any good reason
not to follow one of the existing styles.

Two of the most common styles are:

1. K&R style. An opening brace '{' goes at the end of a line,
preceded by a space (except possibly for the opening brace of a
function definition, for historical reasons). Enclosed lines are
indented by one level (e.g., by 4 columns). The closing brace is
aligned directly under the beginning of the line containing the
opening brace (this is where you go astray).

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.
That is the GNU style and is the default provided by indent. It is
fairly common, especially by open source enthusiasts, though I cannot
think of a more hideous style, and I've seen my share of truly awful C code.
Jun 27 '08 #6

"Ian Collins" <ia******@hotma il.comwrote in message
news:68******** *****@mid.indiv idual.net...
You probably used tabs rather than spaces. Stick to (two) spaces and
your code will be left unmolested.
Exactly. I always do. Maybe it's a bad habit.

Bill
Jun 27 '08 #7

"Bill Cunningham" <no****@nspam.c omwrote in message
news:%7sUj.3801 $Ve.393@trnddc0 8...
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?
well, this gets into this big hairy area known as "coding style" (most have
opinions, few can completely agree...).
now, I usually code in notepad, which has inflexible 8-space tabs, so
usually I use this.
if the tab space is adjustable, usually I like 4 space tabs.

2 or 3 spaces is IMO too little.
1 space is just horrid (may as well not indent at all...).
usually, I put opening and closing braces on their own lines, and closing
braces are indended the same as the opening braces.

int main(int argc, char *argv[])
{
FILE *fd;
if(argv<2)
{
printf("usage: %s <filename>\n" , argv[0]);
return(-1);
}

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

note that EXIT_SUCCESS and EXIT_FAILURE are considered "more correct" for
main return values, but 0 and -1 are more common/traditional.
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.

it is common for commas to be followed by a space ("f(x, y);" but not
"f(x,y);").
some people precede/follow parens and/or operators with spaces.

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 *', ...).

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

and so on...

Bill


Jun 27 '08 #8
cr88192 said:

<snip>
>
now, I usually code in notepad,
Oh dear. :-)
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?
2 or 3 spaces is IMO too little.
And IMO 3 is too many. Vive la difference!
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>
int main(int argc, char *argv[])
{
FILE *fd;
if(argv<2)
You meant argc.
{
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!
return(-1);
This has no portable meaning (and the parentheses are redundantly
superfluous).
}

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.
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.
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.
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.
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.
>
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.

--
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 #9
pereges wrote:
i use tabs for eg.

void func1(void)
{
...
My tab key is configured to give me four space characters
in my source code editor.
Tab characters don't post very well.

--
pete
Jun 27 '08 #10

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

Similar topics

0
1255
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
7788
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
7092
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
3181
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
7527
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
1804
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
1686
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
2106
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
9647
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
9492
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
10360
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
10163
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
10108
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
8988
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...
0
5397
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...
1
4064
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
2
3668
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.