Reputation: 117
So I have a string passed into main function: int main(int argc, char* argv[])
I understand argc (which is 2 in this case), but don't understand how I can read argv[] character by character? When I print argv[0] shouldn't that print the first character in the array of characters for that string?
Thanks
Upvotes: 2
Views: 34046
Reputation: 6057
One more example:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (int argc, char *argv[])
{
if(argc != 2)
{
//argv[0] is name of executable
printf("Usage: %s argument\n", argv[0]);
exit(1);
}
else
{
int i;
int length = strlen(argv[1]);
for(i=0;i<length;i++)
{
printf("%c",argv[1][i]);
}
printf("\n");
return 0;
}
}
Upvotes: 0
Reputation: 40145
sample
#include <stdio.h>
int main(int argc, char *argv[]){
int i,j;
for(i=0; i<argc; ++i){
for(j=0; argv[i][j] != '\0'; ++j){
printf("(%c)", argv[i][j]);
}
printf("\n");
}
return 0;
}
Upvotes: 7