I am using an executable made from C, and I am trying to pass variables in R to it.
C code:
#include <stdio.h>
#include <stdlib.h>
int sumUp(int x, int y, int z, int *sum);
int main()
{
int x1,x2,x3;
int total = 0;
scanf("%d %d %d",&x1,&x2,&x3);
sumUp(x1,x2,x3,&total);
printf("Your total is :%d\n", total);
system("pause");
}
int sumUp(int x, int y, int z, int *sum)
{
*sum = x + y + z;
}
and Here is my R code:
x <- 0
y <- 0
z <- 0
readint <-function(){
a <- readline(prompt = "Enter a number: \n")
}
x <- as.numeric(readint())
y <- as.numeric(readint())
z <- as.numeric(readint())
system("Practice.exe", intern = TRUE,input = "x y z")
I am having an issue when I use variables for input as it will print an incorrect value. However, when I use direct input, like input = "1 2 3", I will get the correct answer. I checked the types for for my variables, and they seem to be correct. Here is the output for reference:

Is there something wonky going on between R and C, or am I doing something incorrect with system? Even if I vary the values of x,y,z, I will get 84. So I imagine there is something going on under the hood here that I am missing. I tried using args instead of input, but ran into the same issue, except that it gave a value of 126 no matter what I entered, variable or directly. Any help is appreciated, thank you.
system("pause")from your C code, it has no place in non-interactive programs and is unnecessary beyond that in 99.99% of cases. Secondly, you’re using theinputparameter incorrectly in R. You’re supposed to pass a character vector of the actual values here, not of variable names.system2is recommended for new code.pastefunction for converting multiple values to string and concatenating them.