Thread: How do I clear screen in a C program?

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    2

    How do I clear screen in a C program?

    Hi. Kinda new to C. After I ask a user >

    printf("Please type your first name");
    scanf("%s", &name);

    /* I now want to clear screen

    puts("Now enter your age");
    scanf("%d", &age);

    I use Linux and was trying to use this>
    system(clear)

    I inluded <stdlib.h>
    at beginning of my simple program but the program failed.

    How should I clear the screen by calling 'clear'

    thanks

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    http://www.cprogramming.com/boardfaq.html#clear

    Read away my friend, read away.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    >system(clear)
    If clear is not a declared variable holding the shell command to clear the screen, then it's wrong. You need to do:
    Code:
    system("clear");  /*   UNIX   */
    system("cls");    /*   DOS   */

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    64
    If you wish to use clearscreen in testmode..
    I can use clrscr();
    Remember to include ...
    #include <conio.h>
    Conio is not ANSI C, but Borland Extension I think!!!

    Then you can write
    clrscr();
    to clear screen....

    But since you use Linux this info. is no use for you :-)
    Didn't see that at first.

    But if others is using Borland this can do it..
    Last edited by Gugge; 08-21-2002 at 10:53 AM.
    !G!

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >How should I clear the screen by calling 'clear'
    system takes a string as the argument, so your command should be:

    system ( "clear" );

    -Prelude
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Aug 2002
    Posts
    2
    so I have to #include <stdlib.h>
    and then whereever I want to clear the screen I simply throw in system("clear"); ??

    Remember I am programming in Linux so I don't use bordland.




    thanks dudes!

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >so I have to #include <stdlib.h>
    >and then whereever I want to clear the screen I simply throw in system("clear"); ??
    Correct, but use it sparingly. The system function is frighteningly slow and many calls to it will seriously effect your program's running time. You would also do well to think ahead and create a macro to make porting to a different system easier:
    Code:
    #if defined POSIX
      #define CLEARSCR system ( "clear" )
    #elif defined MSDOS || defined WIN32
      #define CLEARSCR system ( "cls" )
    #endif
    -Prelude
    My best code is written with the delete key.

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