Tuesday, January 29, 2019

How to shutdown, log off and restart the computer with C Programming?

Hello guys i know some of you might not be familiar with the C programming, but this is very basic programming language for programming,and today we are going to learn some source codes that helps to create .EXE file that does some sign out options for you computer, if you are tired of going to start menu and choose the option to shut down the computer, this tutorial is for you, you can simply have a file in desktop that will shut down or restart or log off your computer.  however you have to use compilers to compile this language, i would suggest "Code::Blocks" for a perfect IDE for beginners. If you don't have the software, make sure you do, it is totally free of cost, Click Here . Anyway let's get into the tutorial,

After you have an IDE, you have to type down this source code.

If you want to make a .EXE file to shutdown the system: 

#include<stdio.h>
void main()
{
system("c:\\windows\\System32\\shutdown -s");
}

To restart the computer: 

#include<stdio.h>
void main()
{
system("c:\\windows\\System32\\shutdown -r");
}

To logg off the computer: 

#include<stdio.h>
void main()
{
system("c:\\windows\\System32\\shutdown -l");
}

if you are still confused at how to use the Code::Blocks, i will make another tutorial for that too, but for now, i think this helps you, have a good day guys.

This C language code is for demonstrating how stack operations are done.

Program 1. Program to organize stack.

#include<stdio.h>
#define maxsize 10
int top=-1;
int stk[maxsize];
void pop()
{
    if(top==-1)
        printf("The stack is empty.");
    else
    {
    stk[top]=NULL;
    top--;
    system("cls");
    printf("Item popped.");
    }
    return;
}
void push(int n)
{
    if(top==maxsize-1)
        {
            system("cls");
            printf("The stack is full. Can't push any more items.");
        }
    else
    {
        top++;
        stk[top]=n;
        system("cls");
    }
    return;
}
void display()
{
    int i;
    if(top==-1)
        printf("The stack is empty");
    else
    {
        for(i=0; i<=top; i++)
        printf("%d: %d\n",i,stk[i]);
    }
    return;
}
int main()
{
    int num, ch;

    start:

    printf("Do you want to\n1.Pop\n2.Push\n3.Display\n");
    scanf("%d",&ch);

    switch (ch)

    {
        case 1:
            {
                pop();
                goto start;
            }
        case 2:
            {
                printf("Enter your input: ");
                scanf("%d",&num);
                push(num);
                goto start;
            }
        case 3:
            {
                display();
                goto start;
            }
        default:
            {
                system("cls");
                printf("Wrong input, try again");
                goto start;
            }
    }
}
That's all. Please like my page for updates.