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 1984
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.
if (argc!3) {
fprintf(stderr, "usage error\n");
return -1;
}
This is one of many possible styles. The main thing is to use
intentation to identify each block, and to use a consistent style.
The two commonest are probable
if (x)
{
bodyhere();
}
and
if (x) {
bodyhere();
}
The one you used above is less common in my experience.
On 8 May, 01:51, "Bill Cunningham" <nos...@nspam.c omwrote:
* * 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.
that's the idea! :-)
#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 it's better! In fact it's good enough to prevent
me whining about indentation. Personnally I prefer the
braces to line up, but the above is ok. I definitly
prefer whitespace around binary operators (eg. =)
and I'd use more blank lines. But on usenet saving vertical
space can be a good thing.
#include <stdio.h>
#include <stdlib.h>
int main (int argc, char **argv)
{
double x;
double 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;
}
the main thing is to group things together that
logically belong together, leave enough whitespace
(horizontal and vertical) to aid readability and
above all, be consistent.
Oh, and never use tabs!
[mutter, one project had a standard of 4 column indent
and 8 space tabs. So you mixed tabs and spaces. eek!]
--
Nick Keighley
"The Real Programmer wants a "you asked for it, you got it"
text editor--complicated, cryptic, powerful, unforgiving,
dangerous. TECO, to be precise."
i use tabs for eg.
void func1(void)
{
...
Richard Heathfield ha scritto:
cr88192 said:
<snip>
>now, I usually code in notepad,
Oh dear. :-)
Oh dear dear :-)
>which has inflexible 8-space tabs, so usually I use this. if the tab space is adjustable, usually I like 4 space tabs.
And IMO 3 is too many. Vive la difference!
You forgot <stdio.h>
You meant argc.
A type , may be...
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!
argc must be 1 (the program name ia always passed as argument), by
standard.
But, yes, it is best to check it...
> return(-1);
This has no portable meaning (and the parentheses are redundantly
superfluous).
I'm not agree, it' more readable.
>> fd=fopen(argv[1], "rb"); ... return(0);
Again, the parentheses are superfluously redundant.
Question of style: for me it is better to place parentesis around the
returned value.
What about an horrible return like this one ????
return a*32 / b + c - d * e / oh_my_good ;
0 is fine - it means success.
Ok, it's a standard value
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.
:-)
brix
brix99luftballo ns wrote:
argc must be 1 (the program name ia always passed as argument), by
standard.
No. argc may be zero.
N869
5.1.2.2.1 Program startup
[#2] If they are declared, the parameters to the main
function shall obey the following constraints:
-- 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. If
the host environment is not capable of supplying
strings with letters in both uppercase and lowercase,
the implementation shall ensure that the strings are
received in lowercase.
-- If the value of argc is greater than zero, the string
pointed to by argv[0] represents the program name;
argv[0][0] shall be the null character if the program
name is not available from the host environment. If
the value of argc is greater than one, the strings
pointed to by argv[1] through argv[argc-1] represent
the program parameters.
--
pete
brix99luftballo ns said:
>
Richard Heathfield ha scritto:
<snip>
>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!
argc must be 1 (the program name ia always passed as argument), by
standard.
Wrong.
But, yes, it is best to check it...
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.
>> 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?
>> fd=fopen(argv[1], "rb"); ... return(0); Again, the parentheses are superfluously redundant.
Question of style: for me it is better to place parentesis around the
returned value.
What about an horrible return like this one ????
return a*32 / b + c - d * e / oh_my_good ;
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)?
<snip>
--
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
"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) ;
}
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.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.
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.
cr88192 wrote:
"Richard Heathfield" <rj*@see.sig.in validwrote in message
news:HM******** *************** *******@bt.com. ..
>cr88192 said:
<snip>
>> { 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...
<snip>
Any Unix if the exec* call used to invoke the program provides a
parameter list with argv[0]=NULL. In Unix programs other than your shell
are allowed to exec your program, so you cannot guarantee you will get a
program name.
>>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.
<snip>
Not on VMS. IIRC on VMS odd return values indicate success and even
return values indicate failure with the C compiler using some magic to
make returning 0 actually return an odd number to the environment.
There *are* good reasons for using non-C-standard return values from
main, but in my experience they only apply when you have return return
values indicating something more than simple success/failure.
--
Flash Gordon
Flash Gordon said:
cr88192 wrote:
<snip>
>I don't personally know of any OS where argc is not at least 1...
Always a shaky argument.
>
<snip>
Any Unix if the exec* call used to invoke the program provides a
parameter list with argv[0]=NULL.
Right. Also, I believe, older Mac systems.
<snip>
--
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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
|
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...
|
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
|
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'
| |
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.
|
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 .
|
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...
|
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...
|
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,...
|
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...
| |
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, and deployment—without 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...
|
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...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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...
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |