?
Solved

printing arguments from commandline prints garbage.

Posted on 2007-03-23
3
Medium Priority
?
314 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 1000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Suggested Courses

752 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