Solved

printing arguments from commandline prints garbage.

Posted on 2007-03-23
3
277 Views
Last Modified: 2010-04-15
can someone tell me what is wrong with this. I get garbage when I print the arguments.
Calling it as below gives me:
argv[0] is I
argv[1] is Ö
argv[2] is U
argv[3] is _
argv[4] is ä
argv[5] is é


testapp.exe one two three four five

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

        int q;
        for(q=0;q<=argc;q++) {
                printf("argv[%d] is %s\n",q,argv[q]);
        }
return 0;
}

0
Comment
Question by:TristinColby
  • 2
3 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 18783415
Your code actually does what it is suppoesd to do:

#include <stdio.h>

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

        int q;
        for(q=0;q<=argc;q++) {
                printf("argv[%d] is %s\n",q,argv[q]);
        }
return 0;
}

C:\tmp\cc>cl cmdarg.c
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8804 for 80x86
Copyright (C) Microsoft Corp 1984-1998. All rights reserved.

cmdarg.c
Microsoft (R) Incremental Linker Version 6.00.8447
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.

/out:cmdarg.exe
cmdarg.obj

C:\tmp\cc>cmdarg one two three four
argv[0] is cmdarg
argv[1] is one
argv[2] is two
argv[3] is three
argv[4] is four
argv[5] is (null)
0
 
LVL 86

Expert Comment

by:jkr
ID: 18783417
Oh, make that loop

        int q;
        for(q=0;q<argc;q++) { /* NO <=! */
                printf("argv[%d] is %s\n",q,argv[q]);

Indices are zero based.
0
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 18791289
You could try:

               printf("argv[%d] is %ls\n",q,argv[q]);

Paul
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now