Jacquelyn.Marquardt
Jacquelyn.Marquardt

Reputation: 630

how to pass a variable to command in c

I've written a C program on Linux to calculate the perimeter (I've called the perimeter variable "p") of a square from its side length ("l"). Now I want to use Espeak, a speech synthesis program on Linux to speak the result. I've thought of using the "system" method.

For example, if I want to make Espeak speak "hello" inside my program I would do:

system("espeak -v it Hello");

Now how can I do with the perimeter?

system ("espeak -v it The perimeter is p"); 

doesn't work.

Upvotes: 0

Views: 295

Answers (2)

Jacquelyn.Marquardt
Jacquelyn.Marquardt

Reputation: 630

Thanks to all! I just solved with

#include<stdio.h>
int main()
{
    char command[128];
    int p=40;
    snprintf(command, sizeof(command), "espeak -v it  \"Il perimetro è %d\"",p);
    printf("%s\n",command);
    system(command);
}

Upvotes: -1

Iharob Al Asimi
Iharob Al Asimi

Reputation: 53006

You could build the command like in

char command[128];
snprintf(command, sizeof command, "espeak -v it The perimeter is %d", p);

if p is an integer, you should change the format specifier if the type of p is different.

Upvotes: 3

Related Questions