To void parameter 'argc' is never used

H

hpy_awad

What should I do to avoid the message :
parameter 'argc' is never used

My program is down :
*--------------------
/* Book name : The prodessional programmers guide to C
File name : E:\programs\bc\witi01\ch10\main.c
Program discription: TV rental system menu driven-ver01-W
*/

#include <stdio.h>

main(int argc,char *argv[])
{
int option;
do
{
display_menu();
option=user_selection();
if (option!=7) /*Exit*/
call_program(option,argv);
} while (option!=7);

}

display_menu()
{
system("cls");
printf ("\n TV Rental system");
printf ("\n ----------------");
printf("\n\n 1 Set up new customer");
printf("\n\n 2 Change existing customer record");
printf("\n\n 3 Add new customer record");
printf("\n\n 4 Delete customer record");
printf("\n\n 5 Print customer bills");
printf("\n\n 6 display a customer record");
printf("\n\n 7 Exit");
}


user_selection()
{
int opt;
printf("\n Enter required option(1-7) : ");
/*scanf("\n");*/
scanf("%d",&opt);
return (opt);
}

call_program(int opt,char *argv[])
{
switch(opt)
{
case 1:
/*module OK 1 Set up new customer*/
spawnvp(0,"tvfsup2.exe",argv);
delay();
break;
case 2:
/*module OK 2 Change existing customer record*/
spawnvp(0,"tvfalt2.exe",argv);
delay();
break;
case 3:
/*module OK 3 Add new customer record*/
spawnvp(0,"tvfadd2.exe",argv);
delay();
break;
case 4:
/*module OK 4 Delete customer record*/
spawnvp(0,"tvfdel2.exe",argv);
delay();
break;
case 5:
/*module OK 5 Print customer bills*/
spawnvp(0,"tv19.exe",argv);
delay();
break;
case 6:
/*module OK 6 display a customer record*/
spawnvp(0,"tvfrec2.exe",argv);
delay();
break;

default:
printf("\n incoreect input");
delay();
exit(0);
}
}

delay()
{
double i;
for (i=0;i<31000;i=i+0.5);
}
 
G

Gibby Koldenhof

It's a warning saying that you are not using argc; this is not problematic,
the compiler is just saying; hey you defined this function with a parameter
but you don't use that parameter within the scope of that function.

If it really bothers you, you can add:

argc = argc ;

in main().

Question for the group, why was argc included anyway (what is the history
behind it) - given that NULL == argv[argc] (i.e. it's terminated anway) ?

Cheers,
Gibby
 
C

Chris Torek

Question for the group, why was argc included anyway (what is the history
behind it) - given that NULL == argv[argc] (i.e. it's terminated anway) ?

In particularly ancient, long-pre-standard Unix systems (probably
mid-1970s), argv[argc] either did not exist or was -1 (I am not
sure which). I believe the decision to make argv[argc] exist and
be NULL occurred when environments were added to the Unix exec()
system call (which became execve() rather than execv()).

Only the startup code that called main() for you had to change
to implement the new feature, and it could now count up the argv
elements while searching for the first "envp" pointer and pass
argc to main() to maintain backwards compatibility.
 
V

Vijay Kumar R Zanvar

What should I do to avoid the message :
parameter 'argc' is never used

My program is down :
*--------------------
/* Book name : The prodessional programmers guide to C
File name : E:\programs\bc\witi01\ch10\main.c
Program discription: TV rental system menu driven-ver01-W
*/
[..]

F:\Vijay\C> type main.c
#include <stdlib.h>

#ifdef __FLAG /* enable from command line */

/* Avoid warning: `ident' defined but not used */
#ifdef __GNUC__
#define UNUSED __attribute__ (( unused ))

/* Turbo C/C++ specific */
#elif defined(__TCPLUSPLUS__) || defined(__MSDOS__)
#define UNUSED
#pragma warn -var

#else

#define UNUSED
#endif

#else

#define UNUSED
#endif /* __FLAG */

int
main ( int argc UNUSED, char *argv[] UNUSED )
{
int a;
return EXIT_SUCCESS;
}

F:\Vijay\C> gcc main.c -Wall -W
unused.c: In function `main':
unused.c:27: warning: unused variable `a'
unused.c:25: warning: unused parameter `argc'
unused.c:25: warning: unused parameter `argv'

F:\Vijay\C> gcc main.c -Wall -W -D__FLAG
unused.c: In function `main':
unused.c:27: warning: unused variable `a'

F:\Vijay\C> bcc32 main.c -wuse
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
unused.c:
Warning W8057 unused.c 29: Parameter 'argc' is never used in function main
Warning W8057 unused.c 29: Parameter 'argv' is never used in function main
Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland
 
D

Default User

What should I do to avoid the message :
parameter 'argc' is never used

My program is down :
*--------------------
/* Book name : The prodessional programmers guide to C
File name : E:\programs\bc\witi01\ch10\main.c
Program discription: TV rental system menu driven-ver01-W
*/

#include <stdio.h>

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


Why do you have argc and argv in main() if you aren't using them? Just
change the signature to:


int main(void)





Brian Rodenborn
 
D

Dan Pop

In said:
Question for the group, why was argc included anyway (what is the history
behind it) - given that NULL == argv[argc] (i.e. it's terminated anway) ?

In particularly ancient, long-pre-standard Unix systems (probably
mid-1970s), argv[argc] either did not exist or was -1 (I am not
sure which).

K&R1 doesn't mention argv[argc] at all. argv[argc - 1] is the last
element with a defined meaning. When K&R2 introduced argv[argc], it used
the words "; additionally, the standard requires that argv[argc] be a null
pointer".

Dan
 
D

Dan Pop

In said:
What should I do to avoid the message :
parameter 'argc' is never used

Remove the compiler option that checks for unused function arguments:
IMHO it's a cure worse than the disease.

Dan
 
D

Dan Pop

In said:
Question for the group, why was argc included anyway (what is the history
behind it) - given that NULL == argv[argc] (i.e. it's terminated anway) ?

Isn't it obvious? argv[] hasn't always been a null pointer terminated
array.

Dan
 
D

Dan Pop

F:\Vijay\C> type main.c
#include <stdlib.h>

#ifdef __FLAG /* enable from command line */

/* Avoid warning: `ident' defined but not used */
#ifdef __GNUC__
#define UNUSED __attribute__ (( unused ))

/* Turbo C/C++ specific */
#elif defined(__TCPLUSPLUS__) || defined(__MSDOS__)
#define UNUSED
#pragma warn -var

#else

#define UNUSED
#endif

#else

#define UNUSED
#endif /* __FLAG */

int
main ( int argc UNUSED, char *argv[] UNUSED )
{
int a;
return EXIT_SUCCESS;
}

F:\Vijay\C> gcc main.c -Wall -W
unused.c: In function `main':
unused.c:27: warning: unused variable `a'
unused.c:25: warning: unused parameter `argc'
unused.c:25: warning: unused parameter `argv'

Using the preprocessor to bastardise the language syntax is a cure far
worse than the disease.

In the case of gcc, it's much simpler not to use -W in the first place.
If the -W warnings haven't been included in -Wall, there *must* be a
*good* reason.

fangorn:~/tmp 129> cat test.c
int main(int argc, char **argv)
{
int a;
return 0;
}
fangorn:~/tmp 130> gcc -Wall test.c
test.c: In function `main':
test.c:3: warning: unused variable `a'
fangorn:~/tmp 131>

Dan
 
D

Dan Pop

In said:
What should I do to avoid the message :
parameter 'argc' is never used

My program is down :
*--------------------
/* Book name : The prodessional programmers guide to C
File name : E:\programs\bc\witi01\ch10\main.c
Program discription: TV rental system menu driven-ver01-W
*/

#include <stdio.h>

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

Why do you have argc and argv in main() if you aren't using them? Just
change the signature to:

int main(void)

There are cases when only one of main's arguments is relevant. Trivial
examples (assume <stdio.h> has been included):

int main(int argc, char **argv)
{
printf("Program invoked with %d arguments\n", argc - 1);
return 0;
}

int main(int argc, char **argv)
{
while (*argv != NULL) puts(argv++);
return 0;
}

Dan
 
E

Eric Sosman

Dan said:
Remove the compiler option that checks for unused function arguments:
IMHO it's a cure worse than the disease.

This is a matter on which reasonable people can
disagree, perhaps even amicably. Such a warning can
certainly help catch typos:

double length(double x0, double y0,
double x1, double y1)
{
return hypot(x1 - x0, y1 - x0);
}

If the compiler complains about `y0' being unused, it
has done me a favor. My own preference is to accept
all such favors with gratitude, my preferred gcc options
are "-Wall -W -ansi -pedantic" (omitting the final two
when compiling some kinds of system-specific code).

A source construct that suppresses the "unused"
warning from most compilers (including the gcc versions
I've encountered) is

int main(int unused, char *argv[]) {
...
(void)unused;
...

Of course, the quest to eliminate every warning from
every compiler is ultimately futile. The compiler is
permitted to issue any warnings it feels like. Some may
complain that `(void)unused;' is a statement that doesn't
do anything -- you may simply have traded one warning
for another. That's why I suggest using a name like
`unused', so that when some compiler somewhere complains
despite your best efforts, the programmer who's tracking
down the complaint will recognize it as unimportant.
 
T

those who know me have no need of my name

in comp.lang.c i read:
ask your compiler not to tell you about unused identifiers if they don't
matter to you.
#ifdef __FLAG /* enable from command line */

invading the reserved namespace is dicey at best.
#elif defined(__TCPLUSPLUS__) || defined(__MSDOS__)
#define UNUSED
#pragma warn -var

this disables unused variable warnings for the remainder of the program,
whereas you otherwise (appear to) provide a per-variable mechanism -- this
can lead to surprises.
 
D

Default User

Dan said:
There are cases when only one of main's arguments is relevant.

Tis very true. The example given was not one of those. My desire was to
get the OP thinking about all aspects of his program rather than
engaging in Cargo Cult programming. I think you'd agree that encouraging
someone to engage their brain is a worthwhile pursuit.
Trivial
examples (assume <stdio.h> has been included):

I am quite sure you aren't trying to lecture me on such usuage, but to
provide examples for others. Naturally, I'm quite familiar with most
variations on command-line programs.




Brian Rodenborn
 
K

Kevin Bagust

Tis very true. The example given was not one of those. My desire was to
get the OP thinking about all aspects of his program rather than
engaging in Cargo Cult programming. I think you'd agree that encouraging
someone to engage their brain is a worthwhile pursuit.

The OP's code used argv in the line:

call_program(option,argv);

so it is not possible to remove the arguments from main.

Kevin Bagust.
 
A

Alan Balmer

Question for the group, why was argc included anyway (what is the history
behind it) - given that NULL == argv[argc] (i.e. it's terminated anway) ?

As others have said, it wasn't always that way. Regardless, it's
convenient to have argc. One obvious case is checking that the number
of parameters is correct without having to look at all of them.
 
D

Default User

Kevin said:
The OP's code used argv in the line:

call_program(option,argv);


Ah, I missed that in the initial read-through.

In that case, my comments were inappropriate.




Brian Rodenborn
 
M

Mark McIntyre

The OP's code used argv in the line:
call_program(option,argv);

Quite possibly. I humbly suggest that in that case, he actually needs to
use argc too, since it tells him something important about argv. This is of
course the easiest way to remove the warning.
 
K

Keith Thompson

In <[email protected]> "Gibby Koldenhof"
Question for the group, why was argc included anyway (what is the history
behind it) - given that NULL == argv[argc] (i.e. it's terminated anway) ?

Isn't it obvious? argv[] hasn't always been a null pointer terminated
array.

No, it's not particularly obvious (even though it happens to be true).
 
J

J. J. Farrell

Eric Sosman said:
A source construct that suppresses the "unused"
warning from most compilers (including the gcc versions
I've encountered) is

int main(int unused, char *argv[]) {
...
(void)unused;
...

Another that often works is

unused = unused;
 
D

Dan Pop

In said:
[email protected] (Dan Pop) said:
In <[email protected]> "Gibby Koldenhof"
Question for the group, why was argc included anyway (what is the history
behind it) - given that NULL == argv[argc] (i.e. it's terminated anway) ?

Isn't it obvious? argv[] hasn't always been a null pointer terminated
array.

No, it's not particularly obvious (even though it happens to be true).

The presence of argc is a very strong clue that argv doesn't need a
sentinel value. If such a value exists, nevertheless, it is very likely
that it was added later (for some reason that is not obvious to people
unfamiliar with main's nonstandard third argument), when it was too late
to remove argc.

Dan
 

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,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top