C programming system stdlib.h


In C programming <stdlib.h> system function help you to interact directly with the command processor or the more common name is command prompt in Windows and Terminal in Linux.

int system(const char *string) ;

Parameters:
const char* -A command name that is understandable by the command prompt or the terminal and which it use for executing some operation.

Return type
int -1 or it is implementation-defined as the output depends on what command you have given to the system(explain more later).

Some points to note:

i) If the argument is a null string or nullptr the system function checks if the OS support command processor.When null string is passed,if the function returns 1 that means the host environment(OS) support command processor.Try running the code below in Windows and Linux

Code example

printf(“%i”, system( nullptr) );

Output in Windows and Linux

1





ii) If the argument is not a null string then it must be some legal command which the command prompt or the terminal can understand.If they cannot understand the string passed they simply returns 1.

The first code example is meant for Windows fan.

Code example

printf(“%i”, system( “HELP” ) ) ; ///Try typing ‘help’ in command prompt you will get the same result as given by this function execution.

printf(“%i”, system( “SYSTEMINFO” ) ); ///Displays information about your system

You can also type “SYSTEMINFO” directly into your command prompt you will get the same output.

This second code example is meant for Linux fan.

Code example

printf(“%i”, system( “INFO” ) ); ///find out more about commands not listed when you type ‘help’ in the terminal

printf(“%i”, system( “info bash” ) ); ///Displays information about your the shell in general.

(Run the two lines of codes separately in two different programs else the output might be confusing)

If you press just ‘F8’ you may see something like this as the output “info: Terminal type `dumb’ is not smart enough to run Info“.To avoid this error press ‘build and run‘ button (found just below the ‘wxSmith’ menu) instead.


Related links

->C getenv stdlib.h