Amazing stuff with system() in C / C++
Last Updated :
29 May, 2017
system() plays a very special role in executing Operating System Commands.
By using this library function we can run all those terminal commands that our Operating System allows us to perform, just by using our C program.
Now, we will learn a very simple code to fetch the IP Address – to identify each computer using that very internet Protocol to communicate.
Let’s see how.
Windows
#include<stdlib.h>
int main()
{
system ( "C:\\Windows\\System32\\ipconfig" );
}
|
Linux
#include<stdlib.h>
int main()
{
system ( "/sbin/ifconfig" );
}
|
Output :
Same output as you get on writing ipconfig in Windows or ifconfig in Linux on your terminal.
You are just using ipconfig/ifconfig on terminal but using C code isn't it cool.
Now we are looking at a code to ShutDown your system.
Windows
#include<stdlib.h>
using namespace std;
int main()
{
system ( "C:\\WINDOWS\\System32\\shutdown /s" );
}
|
Linux
#include<stdlib.h>
int main()
{
system ( "sudo shutdown now" );
}
|
Output :
An alert box appears telling you that your System will Shut Down.
Run these codes on your System and have fun. 🙂
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...