can anyone point me to the problem over here? This compiles but it won’t print anything. I need to compare the string from command line argument with the string “hello”.
Thanks!
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
if (argc == 0)
{
printf("No arguments passed!\n");
}
char *str = argv[1];
if(strcmp("hello", str)==0)
{
printf("Yes, I find it");
}
else
{
printf("nothing");
}
return 0;
}
My ESP suggests that you’re running this in an interactive editor/debugger, such as Microsoft Studio. You probably haven’t configured the environment to pass any command-line parameters, so you expect to see
nothingas your output.However, you access
argv[1], which does not exist, creating a seg-fault, and the program aborts before there is any output.To fix this, check the value of
argcfirst, and make sure you don’t access invalid memory.Also, I recommend putting a
\nat the end of eachprintfto help flush any buffered output to the console.When you run this, you should see: