3

system("cls") is no working in C language . I added conio.h header file but it always say cls not found. I am using xcode. But same code executes perfectly on visual studio.

enter image description here

alk
  • 69,737
  • 10
  • 105
  • 255
Abdul Samad Butt
  • 31
  • 1
  • 1
  • 2
  • this is trying to run a command 'cls'. What system are you running it on? – pm100 Nov 21 '16 at 19:14
  • 2
    On unix-like systems, you might have `clear` instead of `cls` – anatolyg Nov 21 '16 at 19:14
  • 1
    C language has no standard way to clear the screen. You need system("clear") on a mac. Better is to use curses – pm100 Nov 21 '16 at 19:19
  • BTW your code, output window and text of the question are all different. You might want to [edit] your question and provide your *actual* code and *actual* output, not as a picture but as text (use copy-paste) - for everyone's convenience and to reduce confusion, please do that! – anatolyg Nov 21 '16 at 19:21
  • for (ttt = 1; ttt <= 3;ttt++) { system("clear"); for (int delay = 0; delay <= 100000000; delay++) { } i have included its still giving the same error . – Abdul Samad Butt Nov 21 '16 at 19:24
  • Try system("CLS") – Xantium Sep 04 '17 at 13:49

3 Answers3

1

That's because system("cls"); has nothing to do with the c language.

The conio.h header is as far as I know an old MS-DOS header, you can't use it portably. The system() function executes external programs from within a c program, cls is an MS-DOS program to clear the text buffer of a MS-DOS console.

In your picture, it's clear that you are not executing the program in an MS-DOS console so it wont work.

Using external programs is almost always a bad idea, except if those programs are guaranteed to be installed with your's1. The reason is that any program that relies on other programs being available in the target environment, will fail when the external programs aren't.

I understand that it's easy to see a lot of code using non standard tricks like system("cls"), but if you find good learning resources that will not be the case. Try to study each and every function you learn and determine whether or not it's a standard function and a good practice to use it the way you see it.


1TeX distributions work like that, they are just several programs that exchange text, following very closely the UNIX Philosophy. But they are all distributed together.

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
  • 1
    It is true that `conio` is not portable, but its console functions are still implemented by MSVC. Not to be confused with the Borland library of the same name, which provided many more functions than the present MSVC does. – Weather Vane Nov 21 '16 at 19:18
  • @WeatherVane Thanks for the clarification, I will leave it as the comment because I did not know that. – Iharob Al Asimi Nov 21 '16 at 19:19
  • Strangely, the MSVC `conio` library does not have a clear screen function. Even the [Windows console functions](https://msdn.microsoft.com/en-us/library/windows/desktop/ms682073(v=vs.85).aspx) do not have one, although it can be contrived. – Weather Vane Nov 21 '16 at 19:37
0

You are trying to run cls command (to clear the screen) on the console using the system(). cls command only exists on DOS or command prompt on Windows.

system("cls");

If your program is running on Bash on Linux or MacOSX, you can try clear.

system("clear");

0

It's not working because the system() is a library function of stdlib.
You need to use #include<stdlib.h> in order to use system("cls") in C.

SawOnGam
  • 142
  • 3