Thread: How to: "Clear screen" in C program

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    3

    How to: "Clear screen" in C program

    Okay I'm trying to understand how to "refresh" or "clear" the screen in a C program.

    I'm not sure if I'm describing what I'm trying to do well, so here's what I mean:

    So far all I know how to do is outprint more under what I've outprint. For example:

    Code:
    printf("Print 1 statement\n");
    printf("Print 2 statement\n");
    printf("Print 3 statement\n");
    
    
    And that would do the following in the comand promt when I run:
    
    Print 1 statement
    Print 2 statement
    Print 3 statement
    Okay, well, as one could see all the program is doing is outprinting more program after previous program. What I'm looking to do is:

    Code:
    And the command promt do this:
    
    Print 1 statement
    
    *erases command promt back to "empty"*
    
    Print 2 statement
    
    *erases command prompt back to "empty"*
    
    Print 3 statement
    Where each print statement is said, then erased, then the next is said in the same position of the previous. Is there a way to do this?

    My "knowledge" of C is limited up to chapter 8 of my book (arrays) so I don't have a deep understanding of programming relative to many...otherwise I would probably have a solution for this myself.

    Please help if you can. Thanks.
    Matt

  2. #2
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195
    I don't know of a platform independent way of doing it, but in windows one possibility is to use the system command:
    Code:
    system("cls");
    Otherwise you could use the windows API to get the position of the cursor and while it is not at (0,0), continue to type '\b' characters, but that is slightly more indepth.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    You can always spit out a bunch of '\n' characters to scroll everything off the top of the console window.
    If you understand what you're doing, you're not learning anything.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Or even just read the FAQ.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to clear the screen?
    By Zopyrus in forum C++ Programming
    Replies: 8
    Last Post: 11-07-2003, 10:20 PM
  2. Clear Screen
    By KneeGrow in forum C++ Programming
    Replies: 1
    Last Post: 10-24-2003, 10:17 PM
  3. more specific clear screen
    By UniqueuserName in forum C++ Programming
    Replies: 2
    Last Post: 12-10-2002, 11:38 AM
  4. colours & clear screen
    By tomatogen in forum Linux Programming
    Replies: 2
    Last Post: 09-10-2002, 12:36 PM
  5. FYI - a simple way to clear the screen
    By johnc in forum C Programming
    Replies: 13
    Last Post: 07-17-2002, 04:53 PM