When i compile and run it tons of random letters appear in it closes
#include <stdio.h>
int main(int argc, int* argv[])
{
int x;
for(x = 0; x < argc; x )
{
while(*argv[x])
{
putchar(*argv[x]);
*argv[x] ;
}
putchar('\n');
}
return 0;
}
This Programm should take the input from the comand line and print it(i wanted it in this format to test why and how *argv[] works)
CodePudding user response:
You may consider checking for errors.
#include <stdio.h>
int main(int argc, char *argv[])
{
// Exit status if the argument count is incorrect
if (argc != 2)
{
printf("Usage: ./program string\n");
return 1;
}
Also you may want to print the string without writing a loop using printf()
fucntion.
// Print the whole string in one step
printf("%s\n", argv[1]);
return 0;
}
CodePudding user response:
Problem is in the third line:
int main(int argc, int* argv[])
It should be char *agrv[]
, not int *argv[]
. Writing int *argv[]
is wrong as int
is of four bytes and char
is of only one
Writing this instead should fix the issue:
int main(int argc, char* argv[])