Developing A Simple Screensaver Console Application In C

A Screensaver is a simple console application. Earlier when keypad phones used to rule the market, there happened to be a feature that was called screensaver which during idle mode used to get activated quickly.

Today we will see how we can develop a simple randomly color-changing screensaver application in C language.

About Application

The Screensaver application simply keeps on changing the color of the screen.

Tools used

system() Function

The system() is an inbuilt function in C language, located in the standard library of C, stdlib.h. It is used to pass the commands that can be executed by the Command Processor/Command Line/Command Prompt/Terminal of the Operating System. After the given command has been successfully executed without any errors, returns 0.

Syntax of system() functiion is given below,

int system(char command);

Sleep() Function

The sleep() method, is used to pause the implementation of the program for a specified number of seconds, Seconds are a significant amount of time, particularly with a computer where things happen swiftly in microseconds. As a result, there is a demand for a function that pauses execution for shorter intervals. The sleep() method in the C programming language allows you to wait for just a current thread for a fixed amount of time. The sleep() function will sleep the present executable for the time specified by the thread. Meanwhile, the CPU and other operations will function normally. The sleep() function suspends the execution of the requesting thread until the number of real-time seconds provided by the argument seconds has passed. It's defined in the header file, windows.h.

Syntax of Sleep Function,

int Sleep(unsigned seconds);

while() loop

while() loop is an entry controlled loop that is used to execute a certain task or a set of statements till the condition is true. Here we will explicitly hardcode the condition of the while() loop to be 1, i.,e True, So that we can make an infinite loop here.

color command

color command is an internal command in MS-DOS and other Windows Operating systems that are used to change the color of text and background of the screen.

Code

#include <stdlib.h>
#include <windows.h>

int main(){

    while(1){
        system("cls");
        system("color 5f");
        Sleep(2000);
        system("color 4f");
        Sleep(2000);
        system("color 2f");
        Sleep(2000);
        system("color 1f");
        Sleep(2000);
    }
    return 0;
}

Code Explanation

  1. The first two lines of the Program i.e, #include<stdlib.h> and #include<windows.h>  is used to import the Standard Library as well as the Windows Header file to our Program.
  2. From stdlib.h, we will be calling the inbuilt system() function and from windows.h, we will be invoking the Sleep() function.
  3. int main() is the driver program or we can say the main function. Every C Program contains the main function.
  4. Using the while() loop, we iterate system() commands to change screen color.
  5. Sleep() function is used to pause the program, thereby making it appear like a live screensaver.
  6. system("cls") is used to clear the screen at every iteration.

Output

Summary

Here we learned that by using simple Programming Concepts DOS Queries in a C-like system() we can create many types of creative and simplest applications. Not only this but we can also make a C program open a URL in the browser using C, start a Program on Computer, Shutdown/Restart Computer, etc., and many other tasks.


Similar Articles