I have started looking into command processing with C but I have hit a problem with this C program. It is executing the ls
command before it is intended.
Gcc info:
gcc version 6.2.1 20161124 (Debian 6.2.1-5)
This is the code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i;
printf("Is command processor available?\n");
if (system(NULL))
{
printf("Command processor available!\n");
}
else
{
printf("Command processor not available!\n");
exit(1);
}
printf("Executing command ls");
i=system("ls");
printf("Returned value is: %d.\n",i);
return 0;
}
The piece of code I am speaking about is this specific line:
printf("Executing command: ls");
If the program is ran with that piece of code the output is:
Is command processor available?
Command processor is available
systemProcessing systemProcessing.c
Executing command: lsReturned value is: 0.
It executes the command before actually being told to
But when I finish the code off with a new line '\n', its output is as expected:
Is command processor available?
Command processor is available
Executing command: ls
systemProcessing systemProcessing.c
Returned value is: 0.
Why is it that with the newline added to the string the code prints what it is about to do before executing, but without it it executes and then prints that is is going to execute?