开发者

Whats the difference between the two command lines? (SegFault)

开发者 https://www.devze.com 2023-01-15 07:48 出处:网络
I have written a code that maps to a shared memory location,so that the first program opens a shared memory block and stores some data in it.And the second program reads the shared data.

I have written a code that maps to a shared memory location,so that the first program opens a shared memory block and stores some data in it.And the second program reads the shared data.

Whats the difference between the two command lines:

1.

 if(argc<2)
 {
     printf("USAGE:%s text-to-share\n",argv[0]);
 }

This code gives me a Segmentation Fault if I run it without the second argument. However it works fine when I ente开发者_如何学Pythonr in some data.

2.

 if(argc<2)
    return printf("USAGE:%s text-to-share\n",argv[0]);

This one serves my purpose.

But I fail to understand the difference between the two. I'm a novice.For me the two are same,because ideally they should have the same output. Please Help!


The two are obviously not the same:

printf("USAGE:%s text-to-share\n",argv[0]);        // From example 1
return printf("USAGE:%s text-to-share\n",argv[0]); // From example 2

The second line has something the first line does not: a return statement.


why first statment giving you segmentation fault,in C it name of the program which you are executing,so it should be absolutely fine.I am able to execute this testcase properly

int main(int argc,char ** argv){
    if(argc<2)
     {
         printf("USAGE:%s text-to-share\n",argv[0]);
     }
    return 0;
 }

it output :

USAGE:./prog text-to-share

except you are doing something wrong in the code executed before this.

0

精彩评论

暂无评论...
验证码 换一张
取 消