main()

B

Bill Cunningham

I've read that the only parameters that main takes other than a void is this
main(int argc,char *argv[])
I may be correct on this.
I want to take from the command line prompt 2 doubles, pass the values
stored in the array to another function which does some calculations and
returns the address of doubles to the main function.

rel.c /* name of source */

#include "main.h"
main.h reads as thus...
#include <stdio.h>
#include <math.h>

double relstr(double *p1,double *p2);
/* first line of rel.c */
main(int argc,char *argv[])
{if (argc!=2) {puts("command error"); exit(1);}
sscanf(argc,"%s",argv[1]);
double *p1=argv[0];
double *p2=argv[1];

I'm not quite sure what to return here and I've tried to compile this code
in many forms. I always get an error with parameter 1 of sscanf(). Should
the be a cast to a type or pointer to or from a type somewhere?

Bill
 
A

Allin Cottrell

Bill said:
I've read that the only parameters that main takes other than a void is this
main(int argc,char *argv[])
I may be correct on this.
I want to take from the command line prompt 2 doubles, pass the values
stored in the array to another function which does some calculations and
returns the address of doubles to the main function.

rel.c /* name of source */

#include "main.h"
main.h reads as thus...
#include <stdio.h>
#include <math.h>

double relstr(double *p1,double *p2);
/* first line of rel.c */
main(int argc,char *argv[])
that's int main
{if (argc!=2) {puts("command error"); exit(1);}
you want argc = 3
sscanf(argc,"%s",argv[1]);
this cannot work
double *p1=argv[0];
double *p2=argv[1];
Horrible. You can't pretend that strings are pointers-to-double.
I'm not quite sure what to return here and I've tried to compile this code
in many forms. I always get an error with parameter 1 of sscanf(). Should
the be a cast to a type or pointer to or from a type somewhere?

It should certainly not be cast. Read the sscanf documentation: you
can't pass an integer (which is the type of argc) as the first parameter.

I'm not sure what you're trying to do, but this toy program might give
you some ideas:

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

int main (int argc, char *argv[])
{
double x1, x2;

if (argc != 3) {
puts("please supply two doubles");
exit(EXIT_FAILURE);
}

/* illustrates what argv[0] contains */
printf("This program is '%s'\n", argv[0]);

/* If you want proper error-checking, use strtod() */
x1 = atof(argv[1]);
x2 = atof(argv[2]);

printf("The two doubles entered were %g and %g\n", x1, x2);
printf("Their sum is %g\n", x1 + x2);

return 0;
}
 
S

Stephen Sprunk

Bill Cunningham said:
I've read that the only parameters that main takes other than a void is this
main(int argc,char *argv[])
I may be correct on this.
I want to take from the command line prompt 2 doubles, pass the values
stored in the array to another function which does some calculations and
returns the address of doubles to the main function.

I can't parse this into anything logical so I'll take a stab at what I
_think_ you want.
rel.c /* name of source */

#include "main.h"
main.h reads as thus...
#include <stdio.h>
#include <math.h>

double relstr(double *p1,double *p2);
/* first line of rel.c */
main(int argc,char *argv[])
{if (argc!=2) {puts("command error"); exit(1);}
sscanf(argc,"%s",argv[1]);
double *p1=argv[0];
double *p2=argv[1];

I'm not quite sure what to return here and I've tried to compile this code
in many forms. I always get an error with parameter 1 of sscanf(). Should
the be a cast to a type or pointer to or from a type somewhere?

Did you mean:

/* rel.c */
#include <stdio.h>
#include <stdlib.h>

double relstr(double d1, double d2);

int main(int argc, char *argv[]) {

double d1, d2, d3;

if (argc != 3) {
puts("command error");
exit(EXIT_FAILURE);
}

d1 = strtod(argv[1], NULL);
d2 = strtod(argv[2], NULL);

d3 = relstr(d1, d2);

/* whatever else you need to do */

exit(EXIT_SUCCESS);
}
 
B

Bill Cunningham

I can't parse this into anything logical so I'll take a stab at what I
_think_ you want.
rel.c /* name of source */

#include "main.h"
main.h reads as thus...
#include <stdio.h>
#include <math.h>

double relstr(double *p1,double *p2);
/* first line of rel.c */
main(int argc,char *argv[])
{if (argc!=2) {puts("command error"); exit(1);}
sscanf(argc,"%s",argv[1]);
double *p1=argv[0];
double *p2=argv[1];

I'm not quite sure what to return here and I've tried to compile this code
in many forms. I always get an error with parameter 1 of sscanf(). Should
the be a cast to a type or pointer to or from a type somewhere?

Did you mean:

/* rel.c */
#include <stdio.h>
#include <stdlib.h>

double relstr(double d1, double d2);

int main(int argc, char *argv[]) {

double d1, d2, d3;

if (argc != 3) {
puts("command error");
exit(EXIT_FAILURE);
}

d1 = strtod(argv[1], NULL);
d2 = strtod(argv[2], NULL);

d3 = relstr(d1, d2);

/* whatever else you need to do */

exit(EXIT_SUCCESS);
}
I'm not familiar yet with the stdlib library functions. I'll have to
study them if the tutorial I'm using gets there.stod() I guess is string to
double. Just what I need. I need to get beyond stdio.h. Thanks.

Bill
 
B

Bill Cunningham

Allin Cottrell said:
Bill said:
I've read that the only parameters that main takes other than a void is this
main(int argc,char *argv[])
I may be correct on this.
I want to take from the command line prompt 2 doubles, pass the values
stored in the array to another function which does some calculations and
returns the address of doubles to the main function.

rel.c /* name of source */

#include "main.h"
main.h reads as thus...
#include <stdio.h>
#include <math.h>

double relstr(double *p1,double *p2);
/* first line of rel.c */
main(int argc,char *argv[])
that's int main
{if (argc!=2) {puts("command error"); exit(1);}
you want argc = 3
sscanf(argc,"%s",argv[1]);
this cannot work
double *p1=argv[0];
double *p2=argv[1];
Horrible. You can't pretend that strings are pointers-to-double.
I'm not quite sure what to return here and I've tried to compile this code
in many forms. I always get an error with parameter 1 of sscanf(). Should
the be a cast to a type or pointer to or from a type somewhere?

It should certainly not be cast. Read the sscanf documentation: you
can't pass an integer (which is the type of argc) as the first parameter.

I'm not sure what you're trying to do, but this toy program might give
you some ideas:

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

int main (int argc, char *argv[])
{
double x1, x2;

if (argc != 3) {
puts("please supply two doubles");
exit(EXIT_FAILURE);
}

/* illustrates what argv[0] contains */
printf("This program is '%s'\n", argv[0]);

/* If you want proper error-checking, use strtod() */
x1 = atof(argv[1]);
x2 = atof(argv[2]);

printf("The two doubles entered were %g and %g\n", x1, x2);
printf("Their sum is %g\n", x1 + x2);

return 0;
}
Actually I wanted to divide the inputs,
n=x1/x2;
but thats' the right track. If I can do it in one file binary that would be
geat. I want to be able to type
rel 12 6
the result that should be returned is 2 or 2.0000 with zeros.
And what did you mean I ned argc to equal 3?

Bill
 
A

Allin Cottrell

Bill said:
#include <stdio.h>
#include <stdlib.h>

int main (int argc, char *argv[])
{
double x1, x2;

if (argc != 3) {
puts("please supply two doubles");
exit(EXIT_FAILURE);
}

/* illustrates what argv[0] contains */
printf("This program is '%s'\n", argv[0]);

/* If you want proper error-checking, use strtod() */
x1 = atof(argv[1]);
x2 = atof(argv[2]);

printf("The two doubles entered were %g and %g\n", x1, x2);
printf("Their sum is %g\n", x1 + x2);

return 0;
}

Actually I wanted to divide the inputs,
n=x1/x2;
but thats' the right track.

Whatever -- adding them was just illustrative.

If I can do it in one file binary that would be
geat. I want to be able to type
rel 12 6
the result that should be returned is 2 or 2.0000 with zeros.

Replace the "%g" specifier for printf with, e.g. "%f" if you
want (the default) 6 decimal places, "%.4f" for 4 places...
And what did you mean I ned argc to equal 3?

I mean the argument count (argc) includes the name of the called
program. So if you want two numbers appended ("rel 12 6") you
require argc == 3 as the condition for valid input.

Allin Cottrell
 
Z

ZAPPLE

Allin Cottrell said:
Bill said:
#include <stdio.h>
#include <stdlib.h>

int main (int argc, char *argv[])
{
double x1, x2;

if (argc != 3) {
puts("please supply two doubles");
exit(EXIT_FAILURE);
}

/* illustrates what argv[0] contains */
printf("This program is '%s'\n", argv[0]);

/* If you want proper error-checking, use strtod() */
x1 = atof(argv[1]);
x2 = atof(argv[2]);

printf("The two doubles entered were %g and %g\n", x1, x2);
printf("Their sum is %g\n", x1 + x2);

return 0;
}

Actually I wanted to divide the inputs,
n=x1/x2;
but thats' the right track.

Whatever -- adding them was just illustrative.

If I can do it in one file binary that would be
geat. I want to be able to type
rel 12 6
the result that should be returned is 2 or 2.0000 with zeros.

Replace the "%g" specifier for printf with, e.g. "%f" if you
want (the default) 6 decimal places, "%.4f" for 4 places...
And what did you mean I ned argc to equal 3?

I mean the argument count (argc) includes the name of the called
program. So if you want two numbers appended ("rel 12 6") you
require argc == 3 as the condition for valid input.

Allin Cottrell


Hi Guys,
Great information. Till now,(ofcourse till the last min) I dont know
anything about strtod,atof and also %e,%f,%g stuffs. But I have
programmed for converting the given string in to the integers/doubles
manually and it have taken some time for me.
Anything interesting like this with you? Please leave them here(if you
have time). I could learn.
Thanks
ZAPPLE - My Computer is a clever machine, then me
 
D

Default User

Bill said:
sscanf(argc,"%s",argv[1]);


Why don't you get a decent C book, work though the exercises, learn the
basics and STOP WASTING OUR TIME. You've been supposedly learning C for
a year or so, yet you don't have the fundamentals down.

You should be able to look up a function, like sscanf(), look at the
signature, and figure out whether your arguments match.




Brian Rodenborn
 
B

Bill Cunningham

Why don't you get a decent C book, work though the exercises, learn the
basics and STOP WASTING OUR TIME. You've been supposedly learning C for
a year or so, yet you don't have the fundamentals down.

You should be able to look up a function, like sscanf(), look at the
signature, and figure out whether your arguments match.




Brian Rodenborn

Everyone says "Get k&r2! Get k&r2!" I got k&r2 which isn't a tutorial
I've learned now but more of a reference work. Now that I'm working with
tutorials I'm learning alot more. So eat your heart out.

Bill
 
J

Joona I Palaste

Everyone says "Get k&r2! Get k&r2!" I got k&r2 which isn't a tutorial
I've learned now but more of a reference work. Now that I'm working with
tutorials I'm learning alot more. So eat your heart out.

Yes it's a tutorial. But it doesn't spoon feed you like other
tutorials do, instead it simply explains what C is like and leaves it
up to you to understand it. It's more like a description of the C
language than a "Let's learn C in 21 minutes!" kind of book.
 
D

Default User

Bill said:
Everyone says "Get k&r2! Get k&r2!" I got k&r2 which isn't a tutorial
I've learned now but more of a reference work. Now that I'm working with
tutorials I'm learning alot more. So eat your heart out.



Then explain why you, after all this time, are unable to look at the
signature of function and give it the correct type arguments? I mean,
argc as the first argument to sscanf()? Not to mention trying to read
INTO argv[1]?



Brian Rodenborn
 
B

Bill Cunningham

Then explain why you, after all this time, are unable to look at the
signature of function and give it the correct type arguments? I mean,
argc as the first argument to sscanf()? Not to mention trying to read
INTO argv[1]?
I've never come across a decent C tutorial. K&r2 isn't it. Besides I
can't remember the day sometimes, let alone function parameters. That's why
I take aricept. I don't have alzheimers but my thinking processes are
confused by dysthymia and Major depressive disorders.
I have the intellect. But my learning is hindered. And I've always used
vod as the only parameter to main, up til now.

Bill
 
A

Al Bowers

Bill said:
Then explain why you, after all this time, are unable to look at the
signature of function and give it the correct type arguments? I mean,
argc as the first argument to sscanf()? Not to mention trying to read
INTO argv[1]?

I've never come across a decent C tutorial. K&r2 isn't it. Besides I
can't remember the day sometimes, let alone function parameters. That's why
I take aricept. I don't have alzheimers but my thinking processes are
confused by dysthymia and Major depressive disorders.
I have the intellect. But my learning is hindered. And I've always used
vod as the only parameter to main, up til now.

You certainly know enough to comment on the subject a month or so ago.

From: Bill Cunningham ([email protected])
Subject: Re: sscanf
Newsgroups: comp.lang.c
Date: 2004-04-18 10:34:07 PST

You said, "sscanf reads a string instead of input from a keyboard."

You've had ample opportunity to get it straight, even in that thread.
I think you are a fraud, a humbug. You may be able to
redirect my opinion should you supply medical statements of your
psychosis or pathology.
 
D

Dan Pop

Then explain why you, after all this time, are unable to look at the
signature of function and give it the correct type arguments? I mean,
argc as the first argument to sscanf()? Not to mention trying to read
INTO argv[1]?
I've never come across a decent C tutorial.

No one has ever managed to write one that could teach C even to an idiot.
So, give up C: you're clearly wasting your time.

Dan
 
S

Sten Westerback

Bill Cunningham said:
Then explain why you, after all this time, are unable to look at the
signature of function and give it the correct type arguments? I mean,
argc as the first argument to sscanf()? Not to mention trying to read
INTO argv[1]?
I've never come across a decent C tutorial. K&r2 isn't it. Besides I
can't remember the day sometimes, let alone function parameters.

To get the day.. look at your watch, in your calendar or consult your
computer...

You do NOT memorize function parameters -- you look them up and
then it may happen, at least of most ppl, that they start to come back
to you automatically over time. Until then, select function name and
press F1 to bring up it's help... or similar method in other compiler
environments.

But of course you should spend some time learning the two IMHO
most important "issues" in C learning:
- difference between function header declaration and function calls
- pointers -- why, when, where and how on both sides of a function call.
I have the intellect. But my learning is hindered. And I've always used
vod as the only parameter to main, up til now.

Vod ? ;-)

And why did you do that while your reference specify other parameters? :)

- Sten
 
B

Bill Cunningham

So, give up C: you're clearly wasting your time.
I'll never give up C. I'll learn C. On Dennis Ritchie's eventual grave I
won't give up. No matter how long it takes me. Learn to love it.
 
B

Bill Cunningham

You do NOT memorize function parameters -- you look them up and
then it may happen, at least of most ppl, that they start to come back
to you automatically over time. Until then, select function name and
press F1 to bring up it's help... or similar method in other compiler
environments.

But of course you should spend some time learning the two IMHO
most important "issues" in C learning:
- difference between function header declaration and function calls
- pointers -- why, when, where and how on both sides of a function call.


Vod ? ;-)

And why did you do that while your reference specify other parameters? :)

- Sten

I don't know how Vod got in there. I think Dan Pop put it there.
 
B

Bill Cunningham

You certainly know enough to comment on the subject a month or so ago.

From: Bill Cunningham ([email protected])
Subject: Re: sscanf
Newsgroups: comp.lang.c
Date: 2004-04-18 10:34:07 PST

You said, "sscanf reads a string instead of input from a keyboard."

You've had ample opportunity to get it straight, even in that thread.
I think you are a fraud, a humbug. You may be able to
redirect my opinion should you supply medical statements of your
psychosis or pathology.
I'm not a psychotic, I'm not a psychotic. Your talking about Dan Pop.
Besides we're talking about main() not sscanf(). I've always used main(void)
and just started using main(int argc,char *argv[])

Bill
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top