Open In App
Related Articles

Amazing stuff with system() in C / C++

Improve Article
Improve
Save Article
Save
Like Article
Like

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.
IP address
Let’s see how.

Windows




// C program to get the IP Address of your 
// Windows system.
  
//<stdlib.h> library has system() library function.
#include<stdlib.h> 
  
int main()
{
   system("C:\\Windows\\System32\\ipconfig");
}


Linux




// C program to get the IP Address of your
// Linux system.
  
//<stdlib.h> library has system() library function.
#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




// C code to Shut Down your Windows system
#include<stdlib.h>
using namespace std; 
  
int main()
{
   // Using the system() library.
   system("C:\\WINDOWS\\System32\\shutdown /s");
     
   // For Windows XP
   // system("C:\\WINDOWS\\System32\\shutdown -s");
}


Linux




// C program to get the IP Address of your
// Linux system.
  
//<stdlib.h> library has system() library function.
#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. 🙂

If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 29 May, 2017
Like Article
Save Article
Similar Reads