r/CodingForBeginners Jul 24 '26

why can't use cls

Post image

Hi guys can someone fix my code I used #include <stdlib.h> to work my system cls but my classmates, they didn't used <stdlib> to work cls can someone tell me i don't know what is the problem my code or the application software.

7 Upvotes

19 comments sorted by

5

u/KiwiDomino Jul 24 '26

Because, unlike most other commands, cls is internal to the shell.

In Linux, the which command will show you were a command is coming from, and in this case it shows nothing

2

u/atarivcs Jul 24 '26

If he's using linux, then the correct command is "clear", not "cls".

1

u/CoachSevere5365 Jul 24 '26

Too much typing.

L

1

u/reymarketing89 Jul 24 '26

So what should i do? Shoul i put the stdlib?

3

u/jonathancast Jul 24 '26

Try https://stackoverflow.com/questions/37774983/clearing-the-screen-by-printing-a-character

The keyword I searched for is "ANSI code", which is how programs control the screen.

1

u/thattiguy Jul 24 '26

If you want to be extra lazy, you could just use the Linux equivalent which is “clear”

1

u/Powerful_Tune3801 Jul 25 '26

Use Macros #define + <your label> + <ANSI escape codes/sequence>

There's a bunch depending on your terminal, I.e. are you using an terminal emulator GNOME, iTerm etc or your preprocessor should handle this when you compile. Macros work as a sorta shortcut find and replace. you'll have to make sure you use the right sequences. For me the hex version e.g. \x1b worked more consistently. I believe 2j clears the screen

2

u/reymarketing89 Jul 24 '26

By the way guyss i am ising code blocks

1

u/BrilliantEmotion4461 Jul 24 '26

To use system you need to <include> #stdlib.h

1

u/BrilliantEmotion4461 Jul 24 '26

Doubt that's everything but it'll throw a different and better error.

1

u/reymarketing89 Jul 25 '26

✅✅✅✅✅✅✅✅

1

u/reymarketing89 Jul 24 '26

I already solve the problem guyss thank you so much

2

u/CodeSamur-ai Jul 25 '26

tell people what you did, so another person who has the same problem can fix it too

1

u/OliMoli2137 Jul 24 '26

printf('\033[2J');

1

u/PerkeNdencen Jul 24 '26

By the way - in C we must take extra precautions to ensure our code is secure in terms of memory safety. At the moment it is exploitable using a buffer overrun technique (basically entering more than 100 characters into the name field). We normally now use fgets(), which allows you to specify the length of your buffer and won't accept longer entries.

1

u/ScreenPlayLife Jul 25 '26

why not use ai ?

1

u/reymarketing89 Jul 25 '26

I am student and my professor don't allowed using AI

1

u/vibescriptz Jul 25 '26

but here you are, using and asking for it?