?
Solved

Argc and Argv in C

Posted on 2009-04-22
5
Medium Priority
?
644 Views
Last Modified: 2012-06-21
I've written this code, but I'm having problems executing it.  The assignment is to use argsort function to put words in alphabetic order.  What am I missing???
#include <stdio.h>
#include <string.h>
 
   void main( int argc, char *argv[] )
   {
	   int i;
	   char word;
	   int len = 0;
 
    if (len < 3)
    {
		printf("Enter a word:\n");
		scanf("%c\n", word);
//	            return 1;
	}
	 
	for( i=0; i<argc; i++ ){
       printf("argv[%d] = %s\n",i,argv[i]);
	}
//   return 1;
   }

Open in new window

0
Comment
Question by:clscor2
  • 2
  • 2
5 Comments
 
LVL 53

Accepted Solution

by:
Infinity08 earned 1000 total points
ID: 24209317
A few things. See my comments in the code below. Note that I also properly indented the code so it's easier to read.
#include <stdio.h>
/*#include <string.h>*/                           /* <--- not used, so you can leave it out */
 
int main(int argc, char *argv[]) {                /* <--- main HAS to return int, not void */
    int i = 0;                                    /* <--- always initialize your variables */
    char word[128] = "";                          /* <--- a word will probably be more than 1 character,
                                                          so make it an array of characters (better
                                                          known as a string) instead */
    int len = 0;
 
    if (len < 3) {
        printf("Enter a word:\n");
        scanf("%s", word);                        /* <--- %s for a string, %c for a character -
                                                          leave out the \n - you don't need it */
    }
         
    for (i = 0; i < argc; i++) {
        printf("argv[%d] = %s\n", i, argv[i]);
    }
 
    return 0;                                     /* <--- usually, in case of success, 0 is returned */
}

Open in new window

0
 
LVL 85

Expert Comment

by:ozo
ID: 24209579
where is the argsort function?
0
 

Author Comment

by:clscor2
ID: 24209586
Thank you!  Your help was much appreciated.  I do have another question.  If I needed to prompt for several strings (rather than just 1), then alphabetize them, what am I missing?
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 24209651
>> If I needed to prompt for several strings (rather than just 1),

Use a loop, instead of a single if.

>> then alphabetize them, what am I missing?

Store each of the strings you read into an array, and then sort the array.
0
 

Author Closing Comment

by:clscor2
ID: 31573515
Thank you!
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

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…
The goal of this video is to provide viewers with basic examples to understand and use structures 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

850 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