Solved

printing arguments from commandline prints garbage.

Posted on 2007-03-23
3
302 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

Portable, direct connect server access

The ATEN CV211 connects a laptop directly to any server allowing you instant access to perform data maintenance and local operations, for quick troubleshooting, updating, service and repair.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
c language help - file paths 7 150
reading tzdatabase for timezone definitions 5 145
How do I avoid pointer to integer casting errors in C programming? 4 243
C hashtable library 3 100
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…
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.

820 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