• C

argc parameter

I have a problem in the following code, the problem is that when I dubug it I found that it print the first massege and then exit immidiatly without going to the code that will take the file name (not that I try to simulate compiler's work) I noticed that the error may in argc, but I really don't know how it is work???

main( int argc, char * argv[] )
  char pgm[120]; /* source code file name */
  if (argc != 2)
    { fprintf(stderr,"usage: %s <filename>\n",argv[0]);
  strcpy(pgm,argv[1]) ;
  if (strchr (pgm, '.') == NULL)
  source = fopen(pgm,"r");
  if (source==NULL)
  { fprintf(stderr,"File %s not found\n",pgm);
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

This means you run program without arguments.
Your debugger should allow to pass arguments to debugged program. For example, VC++ allows to add arguments in Project - Settings - Debug - Program arguments. If you are working with another debugger, find such option in it.
romramAuthor Commented:
can you tell me how to do such thing???
romramAuthor Commented:
I put a file with .tny extention in the same root of this file , you don't think that it may be the problem...
Cyber security certifications or degree?

Cyber security is in demand—big-time. So what do you need to build a career in this lucrative field? Is a degree a must-have, or are industry-leading certifications more sought-after? Is it possible to break into cybersecurity without a bachelor’s or master’s degree in the field?

Your program should be called with argument:

myprogram.exe filename

If it is called without argument, it prints Usage message and exits.
romramAuthor Commented:
what do yo mean by this?
"Your program should be called with argument:"
I would like to explain my program more for you:
1. the cod I put is a part of a program that is going to work as a compiler
2. so this is part of my main file which will start with the code you see
3. I put in the same root with all the files of my small compiler ,a .tny file "a file for the language of my compiler"

4.strcpy(pgm,argv[1]) ;
  if (strchr (pgm, '.') == NULL)
  source = fopen(pgm,"r");
  if (source==NULL)
  { fprintf(stderr,"File %s not found\n",pgm);

this code must take the file .tny file and open it>>>but I dont reach this stage because of the argc problem...


romram  .. argc is the number of parameters that you pass to your program for example when you run a program like notepad.exe as

$ Notepad.exe     romram.txt  

then you are passing  romram.txt as an argument to your program ..

In  C the  program name is also considered as a argument so in the above program the argc variable will be     2

A simple example
int main(int argc,char * argv)
  int i =0;
  while( i < argc) {printf("ARGUMENT  NUMBER %d  is %s\n",i,argv[i]);i++;}
Now suppose you run compile this program and make an  example.exe out of it then
when you run it like this
$example.exe    abc    xyz  
the output will be

$ARGUMENT NUMBER 0 is  example.exe


so what you need to do in your program is that pass  ".tny "  as the argument to the prgoram

Hope you understand

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial

There is no problem with argc. It is just a parameter to the main() function.
argc will get its value when main() is called....
Who calls the main funcion!?
main() is called by the startup routine......and this takes the number of command line paramters that you gave while executing the program and assigns it to argc!
So, if the name of your executable is my_prog and at command prompt(%) you type:
%my_prog file1
and hit enter, then argc will get the value of 1.

I think the problem is not faced during execution....it is during debugging only.....am I right!?:-)
If yes, then do this.
Supposing you are using gdb (gnu debugger) and sending commands thru command prompt, type the following command:
%gdb my_prog
Now you will get gdb prompt and at that prompt type:
gdb: run file1

Now it will not goto the first printf() and will reach the next statements.....

But before all this......
You must compile your program with debugging flag enabled. So, if you are using gcc, then the compilation command will be:
%gcc -g my_prog.c -o my_prog

You might have done all this and still facing problem......if that's true, then you will have to explain the problem more clearly so that we will understand it and can think of some solution....:-)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.