TECH TIPS Headline Animator

Thursday, December 16, 2010

SHUTDOWN-RESTART-LOGOFF PC with a C program

This is an amazing post with which one can shutdown the computer with the help of a C program.
C has the ability to execute DOS command with the help of stdlib.h header file and system() function.
System invokes a DOS command interpreter from a C file while executing it.
NOTE : on just compiling it will not run, you have to double click the .exe file which will be of the same name as your C file.
For example I saved my file with the name as "system_1.c", So i had to run system_1.exe as shown below



[sourcecode language="c"]
#include
#include
#include

void main()
{
int ch;
clrscr();
printf("\n\n~~~~~~~~~~~~~~~~~SHUTDOWN MENU~~~~~~~~~~~~~~~~~~~\n");
printf("1.Shutdown\n2.Restart\n3.Logoff\n4.Hibernate\n5.exit");
printf("\nEnter choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1:system("shutdown -s");
break;
case 2:system("shutdown -r");
break;
case 3:system("shutdown -l");
break;
case 4:system("shutdown -h");
break;
case 5:exit(1);
break;
default:printf("Invalid choice");
}
getch();
}
[/sourcecode]

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More