I get an output of "hi!". Why is this not also printing "something"?
#include <stdio.h>
#include <unistd.h>
int main(int argc, char** argv) {
char* program_name = "echo";
char* args[]= {program_name,"hi!",NULL};
printf("something");
execvp(program_name,args);
return 0;
}
I know I'm not creating a child process first. If I take out the execvp line, it works as expected. Weird. (Note: "echo" refers to https://en.wikipedia.org/wiki/Echo_(command))
Your printf s and scanf s are executed in order. The output looks messed up because of buffering. When "power:" gets printed, it ends up in the buffer that does not get flushed until a number gets entered. Adding \n solves this for console output.
The most basic printing functions would be puts and putchar which print a string and char respectively. f is for formatted. printf (unlike puts or putchar ) prints formatted output, hence printf. For example it can print an int in hexadecimal, or a float rounded to three decimal places, or a string left padded.
The printf() function sends a formatted string to the standard output (the display). This string can display formatted variables and special control characters, such as new lines ('\n'), backspaces ('\b') and tabspaces ('\t'); these are listed in Table 2.1.
%s and string We can print the string using %s format specifier in printf function. It will print the string from the given starting address to the null '\0' character. String name itself the starting address of the string. So, if we give string name it will print the entire string.
The string is in the io buffer - so pull the chain and flush that buffer
i.e. add
fflush(stdout)
after the printf
(or add \n
to the printf
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With