473,807 Members | 2,847 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 1992

"CBFalconer " <cb********@yah oo.comwrote in message
news:48******** *******@yahoo.c om...
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
This is the results of my program using "indent div.c" which is the name
of the source code.

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

Believe me I didn't do this myself. Indent did it for me!

Bill
Jun 27 '08 #31
"Bill Cunningham" <no****@nspam.c omwrites:
"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.
I believe "rio" is pulling your leg. His code is gratuitously
obfuscated.

--
Keith Thompson (The_Other_Keit h) <ks***@mib.or g>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #32
Nick Keighley wrote:
>
.... snip ...
>
[mutter, one project had a standard of 4 column indent
and 8 space tabs. So you mixed tabs and spaces. eek!]
That is one more reason to use 3 space indentation. Mixing spaces
with tabs is very likely to show up early, and get corrected.

--
[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 #33
Ben Bacarisse <be********@bsb .me.ukwrites:
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.
Ever used ldd? At least on linux it runs the executable with argc==0
which is catched by the dynamic linker. So if your program is dynamicly
linked, you'll never get that case but running by mistaken ldd on a
statically linked executable can lead to that.

Yours,

--
Jean-Marc
Jun 27 '08 #34
Bill Cunningham wrote:
"CBFalconer " <cb********@yah oo.comwrote in message
>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

This is the results of my program using "indent div.c" which is
the name of the source code.
.... snip ...
>
Believe me I didn't do this myself. Indent did it for me!
You control it with indent.pro. Just make the line I provided
above available in indent.pro in the directory that contains the
source code you are indenting.

--
[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 #35

"Eligiusz Narutowicz" <el************ *@hotmail.comwr ote in message
news:fv******** **@registered.m otzarella.org.. .
Bart <bc@freeuk.comw rites:
>On May 8, 11:33 am, "cr88192" <cr88...@NOSPAM .hotmail.comwro te:
>>"Richard Heathfield" <r...@see.sig.i nvalidwrote in message

news:HM****** *************** *********@bt.co m...

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.

Anyone using notepad to write C should be fired. There are lots of
productivity tools which make programming more sure fire and certainly
easier - not to use at least some of them is silly.
I have, personally, never really had that much motivation...

notepad is simple, effective, and edits text files.

I can jump to line numbers, I can search for things, ...

about the only thing it really lacks IMO is a better search-and-replace
feature (so that I don't end up resorting to things like sed, ...). but,
only very rarely is this an issue...
when it comes to C, really, I am not sure what would be added that would
really help all that much...

also note that, often, I have anywhere from 10 to 50 notepad's open at once,
and this was one thing that was a major problem in Vista: it setting a
critically low limit on the number of open windows I could have open before
stuff went weird...

Jun 27 '08 #36

"Eligiusz Narutowicz" <el************ *@hotmail.comwr ote in message
news:fv******** **@registered.m otzarella.org.. .
"cr88192" <cr*****@NOSPAM .hotmail.comwri tes:
<snip>
>>
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
I will agree with this for the most part, as a general rule the Linux
codebase is fairly readable.

however, I do disagree with it on a few points, namely in that I tend to use
rather different naming conventions (including, on occasion, variants of
hungarian notation, including in function names...).

I do agree that globals should be avoided whenever possible, and especially
not be used between subsystems. however, even there, this is not absolute
(there are some rare cases where I believe this is acceptable).
I tend to encode in the names at least some info about where in the codebase
the code is located, and also because of my modularity practices (this helps
both in avoiding accidental clashes or dependencies, and also in locating
where some particular code is at in the project).

of course, newer languages (such as C++, C#, Java, ...) provide other
mechanisms for this, but it is a tradeoff (shorter easier names, of which
one is no longer certain where exactly in the codebase this function is
located, albeit "jump to" features and similar are common in modern IDE's).

it works...

Jun 27 '08 #37
On 8 May, 22:37, CBFalconer <cbfalco...@yah oo.comwrote:
Nick Keighleywrote:

... snip ...
[mutter, one project had a standard of 4 column indent
and 8 space tabs. So you mixed tabs and spaces. eek!]

That is one more reason to use 3 space indentation. *Mixing spaces
with tabs is very likely to show up early, and get corrected.
you misunderstand. It wasn't an error to mix tabs and spaces
it was compulsary. If you didn't indent with mixed tabs and
spaces you couldn't check your source in!

(actually the checkin system ran your program through
indent and indent was set to this standard)
--
Nick Keighley

Jun 27 '08 #38

"rio" <a@b.cwrote in message
news:48******** *************** @reader4.news.t in.it...
>
"Joe Wright" <jo********@com cast.netha scritto nel messaggio
news:xd******** *************** *******@comcast .com...
>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

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

ICK...

these are not exactly the way I would personally use the trinary and comma
operators...

of course, I do have something vaguely similar, but it is more of a
"compact" style:

#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);
if(x==0.0) { printf("Error: x==0\n"); return(-1); }
printf("%.2f\n" , y/x), 0); return(0); }

usually, this is done for code that:
I usually wont really be reading or editing much;
my compulsion for reducing vertical space exceeded my need for clarity.

some elements of this style may sometimes be used in ordinary code though.

>

Jun 27 '08 #39
rio
"Bill Cunningham" <no****@nspam.c omha scritto nel messaggio
news:LzJUj.2452 3$qW.3229@trndd c06...
>
"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 */
if "x" is 0.0, y/x should be here a seg-fault.
For don't allow this the program says there is an input error because x==0,
and the program return -1 the error code.
There are things in C, shortcuts that I haven't grasped yet like += and ?:
I do things the long way right now.
this is what i like
Bill


Jun 27 '08 #40

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

Similar topics

0
1260
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
7802
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
7112
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
5879
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
3183
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
7545
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
1807
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
1690
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
2110
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
9599
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
10626
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
10372
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
10374
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
10112
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...
1
7650
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...
1
4330
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
3854
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3011
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.