How to Clear Terminal in C Without Using #Include? [Closed]

I want to clear terminal in C without using #include.

Here is my code:

int printf(const char *format, ...);

int main()
{
    printf("%c2J", 27);
}
8

You likely won't be able to clear a terminal without using functions in the standard library, but probably the closest thing would be to use ANSI escape sequences, which might work depending on your terminal.

You'll have to output \x1b[2J (probably followed by \x1b[H). These are the ANSI terminal escape sequences for clearing the screen and repositioning the cursor in the upper left "home" position, respectively.

#include <stdio.h>


int main(void) {
    printf("\x1b[2J\x1b[H");
    return 0;
}

Please note that even if you specify the function prototype for printf(), as you have done above, you are still using the standard library and not making your own. I can only think of one reason you'd ever want to do that: to make a quine fit on one line.

5

If you want to write your own function you should not be using the standatd library. Invlude does not matter as .h files are not libraries. You should compile the code with -nostdlib option and write your own function.

In your code you simply use the standard library function - not your own one.

Chloe Bennett

Chloe Bennett

Culture, Media & Entertainment Columnist

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.

Share this article
Twitter Facebook Pinterest