Solved

printing arguments from commandline prints garbage.

Posted on 2007-03-23
3
309 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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 switch statements in the C programming language.
Suggested Courses

732 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