Home > database >  C How does if (system("CLS") {system("clear)} work
C How does if (system("CLS") {system("clear)} work

Time:11-16

C How does this work

if (system("cls"))
{
    system("clear")
}

I was trying to find a cross-platform way to clear console in c and I remembered some code with this syntax so I tried it and it worked but I want to know how it works like does it return an error or something if the command was not found

sorry if it was a stupid question

CodePudding user response:

cls and clear are terminal/command prompt commands used to clear the screen.

system is a c command used to interact with the cmd/terminal directly. It returns 0 if a command was completed successfully.

In this case, if cls fails to clear the screen (in other words, the system command returns something other than 0) then we issue the clear command via system. One of those commands will work on the OS that the app is being run on.

  • Related