i have a piece of code like this:
int main (int argc, char *argv[])
{
printf("%d\t",(int)argv[1]);
printf("%s\t",(int)argv[1]);
}
and in shell i do this:
./test 7
but the first printf result is not 7, how can I get argv[] as a int? many thanks
argv[1]is a pointer to a string.You can print the string it points to using
printf("%s\n", argv[1]);To get an integer from a string you have first to convert it. Use
strtolto convert a string to anint.