#include <stdlib.h>
#include <stdio.h>
#include<conio.h>

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();
}

Dani AI

Generated

A few practical clarifications and a safer, native option.

The sample in the first post calls out to an OS command, which is the simplest route but is inherently OS‑specific and often blocked by modern privilege/UAC policies (which is why saw "Access is denied"). For a reliable, programmatic shutdown on Windows use the Win32 API and explicitly enable the shutdown privilege instead of shelling out. The snippet below demonstrates the minimum: enable SE_SHUTDOWN_NAME, call ExitWindowsEx, and report failures so you can diagnose permission problems.

/* Windows: enable shutdown privilege and request power-off */
#include <windows.h>
#include <stdio.h>

static BOOL enable_shutdown_privilege(void)
{
    HANDLE hToken;
    TOKEN_PRIVILEGES tp = {0};

    if (!OpenProcessToken(GetCurrentProcess(),
                          TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) {
        fprintf(stderr, "OpenProcessToken failed: %lu\n", GetLastError());
        return FALSE;
    }

    tp.PrivilegeCount = 1;
    tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
    if (!LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tp.Privileges[0].Luid)) {
        fprintf(stderr, "LookupPrivilegeValue failed: %lu\n", GetLastError());
        CloseHandle(hToken);
        return FALSE;
    }

    AdjustTokenPrivileges(hToken, FALSE, &tp, 0, NULL, NULL);
    if (GetLastError() != ERROR_SUCCESS) {
        fprintf(stderr, "AdjustTokenPrivileges failed: %lu\n", GetLastError());
        CloseHandle(hToken);
        return FALSE;
    }

    CloseHandle(hToken);
    return TRUE;
}

int main(void)
{
    if (!enable_shutdown_privilege()) return 1;
    if (!ExitWindowsEx(EWX_SHUTDOWN | EWX_POWEROFF, SHTDN_REASON_MAJOR_APPLICATION)) {
        fprintf(stderr, "ExitWindowsEx failed: %lu\n", GetLastError());
        return 1;
    }
    return 0;
}

Cross‑platform notes: on Linux/Unix there is no single portable syscall for polite shutdown — distributions may use systemd (use the login1 D‑Bus API or systemctl poweroff with appropriate privileges) or require root for a reboot(2)/poweroff syscall. Any production tool that must shut down machines for arbitrary users should either run as a privileged service/daemon or use the platform’s authorized API (polkit/DBus on systemd systems).

Troubleshooting checklist (builds on points raised by , , ): use int main(void), check and log return codes and GetLastError/errno, run elevated to satisfy privileges, avoid force flags unless you accept data loss, and test in a VM before using on a real workstation.

Recommended Answers

All 8 Replies

And your question about this poor code is... what?

You want to show how to shutdown ??????????????????????

And your question about this poor code is... what?

hi...why is the code poor? i mean, i didn't even know we could shutdown the pc using c.
can u please tell me a (better) way? Thanks

@Jason

conio.h it is not part of the C standard library,

clrscr also not standard function ..

And he not doing any magic, just giving his work to system..

It's poor because of

#include<conio.h>
void main()
clrscr();
getch();

And the formatting is only fair.

Second what WaltP says, plus don't use scanf for interactive user input, check the return values of standard library functions, and use fflush(stdout) after printing a prompt that doesn't end with '\n' to make sure it gets printed.

1 is not a portable value to pass to exit(), and returning non-zero usually means that some error occurred. Selecting the "exit" function from the menu shouldn't result in an error.

Finally, I can't imagine what version of "shutdown" you're using if -s means "halt", -l means "log off", and -h means "hibernate".

when I run the code and press y it says access is denied. So what can I do?

when I run the code and press y it says access is denied. So what can I do?

Log in with an account that has permission to shut the computer down and then run the program.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.