user975582
user975582

Reputation: 205

Need advice on sending string to function

I am kind of new to c , but i cant figure out how to send this string to the function. I tried several things but it tells me it is expecting something

    program.c: In function ‘main’:
    program.c:48:87: error: expected identifier or ‘(’ before ‘long’

       char string1[] =
 "This is process %d with ID %ld and parent id %ld\n", i,  (long)getpid(), (long)getppid());

    write(wrfd1,string1, strlen(string1));

Is there a better way to do this? Thank you

Upvotes: 0

Views: 76

Answers (2)

Gooker_young
Gooker_young

Reputation: 11

The first problem is:

 (long)getppid())

the bracket number doesn't match.

Another one is that you cannot assign a string like this:

int a = 100;
char str[] = "A is %d", a;

You should use 'sprintf' to do this, as mentioned above.

Upvotes: 1

Jacob Relkin
Jacob Relkin

Reputation: 163228

I think you meant to use sprintf:

int length = 100;
char string1[length];
if(sprintf(string1, "This is process %d with ID %ld and parent id %ld\n", i,  (long)getpid(), (long)getppid())) {
   write(wrfd1,string1, length);
}

Upvotes: 3

Related Questions