Clear Screen 
Author Message
 Clear Screen

Umm..., I am a new programmer to C, and also a new poster to comp.lang.c
after browsing some of the posts I noticed someone asked how to clear
the screen.  The following method seems to me to be pretty portable, but
I can't be sure.

------------------------------------------------------------------------
/* This program calls a function that I made to clear the screen, simply
   call clear_screen () */

#include <stdio.h>

void clear_screen (void)
{
        int n;

        for (n = 1; n <= 30; ++n)
                printf("\n");

Quote:
}

main ()
{

        char c;

        printf("Press c to clear the screen\n");
        scanf("%i", c);

        if (c = 'c' || 'C')
        {
                clear_screen ();
        }

Quote:
}



Sat, 23 Dec 2000 03:00:00 GMT  
 Clear Screen

Quote:

> The following method seems to me to be pretty portable,

It isn't.

--
(initiator of the campaign for grumpiness where grumpiness is due in c.l.c)

This article is copyright 1998 by firewind. It is licensed under the
OpenContent license (http://www.opencontent.org/opl.txt).



Sat, 23 Dec 2000 03:00:00 GMT  
 Clear Screen

Quote:

> void clear_screen (void)

<snip `output 30 newlines' technique>

Hmm, yes.  Well, it's portable to anything with no more than 30 lines on a
screen.  Not *particularly* portable, then.  It also doesn't do what most
people want -- namely, moving the cursor to the top left of the screen.

Quote:
> main ()

It's *much* better to avoid implicit int and K+R-style parameter lists.  Say
what you mean: use `int main (void)'.

Quote:
> {
>    char c;

>    printf("Press c to clear the screen\n");
>    scanf("%i", c);

This isn't correct, on several counts.  First, if you want to use scanf() to
read a character, then the format string is "%c".  Secondly, every argument
to scanf() must be a pointer; here, you should replace `c' with `&c'.
Thirdly, using scanf() to get a character of input is immoral; you should
use fgetc() (or one of its pseudonyms getc() or getchar()) instead:

    int c;

    c = getchar ();

Quote:
>    if (c = 'c' || 'C')

This condition will always be true.  The compiler first assigns 'c' to the
variable c (since `=' is an assignment operator, not a comparison
operator).  If the result of that assignment is non-zero, then the
disjunction stops evaluating and yields 1.  The result of the assignment is
the value 'c', which is always non-zero, so the buck stops here.

If you change the assignment operator to a comparison `==', what happens
then?  Well, now the first half of the disjunction fails if the variable c
didn't contain 'c', and evaluation progresses to the second half of the
disjunction.  This second half of the disjunction is the value 'C', which is
always non-zero, so again the condition succeeds.

This is what you probably meant to say:

    if (c == 'c' || c == 'C')

Quote:
>    {
>            clear_screen ();
>    }

This is an int-valued function, but it returns no value.  Add this line:

    return 0;

Quote:
> }

--



Sat, 23 Dec 2000 03:00:00 GMT  
 Clear Screen

   Umm..., I am a new programmer to C, and also a new poster to comp.lang.c
   after browsing some of the posts I noticed someone asked how to clear
   the screen.  The following method seems to me to be pretty portable, but
   I can't be sure.

[...code that "clears the screen" by outputting 30 newlines...]

Yep, that's just about the most portable method.  It'll really
irritate anyone with a hardcopy terminal, of course.

I can tell that you have a good future here because you bothered to
read posts and you didn't post system-dependent code.  You should also
read the FAQ if you haven't already.
--
(supporter of the campaign for grumpiness where grumpiness is due in c.l.c)

Please: do not email me copies of your posts to comp.lang.c
        do not ask me C questions via email; post them instead



Sun, 24 Dec 2000 03:00:00 GMT  
 Clear Screen

Quote:


> > The following method seems to me to be pretty portable,

> It isn't.

It's portable, it's just not *really* clearing the screen.

--

I believe we can change anything.
I believe in my dream.
    - Joe Satriani



Sun, 24 Dec 2000 03:00:00 GMT  
 Clear Screen

Quote:

>Umm..., I am a new programmer to C, and also a new poster to comp.lang.c
>after browsing some of the posts I noticed someone asked how to clear
>the screen.  The following method seems to me to be pretty portable, but
>I can't be sure.

>------------------------------------------------------------------------
>/* This program calls a function that I made to clear the screen, simply
>   call clear_screen () */

>#include <stdio.h>

>void clear_screen (void)
>{
>        int n;

>        for (n = 1; n <= 30; ++n)
>                printf("\n");
>}

This function will compile and execute on any C platform. The problem
is that it doesn't portably have the effect of clearing the screen. Obvious
problems are:

1. stdout may not be attached to a screen device

2. Some terminals don't scroll

3. Many terminals or terminal emulations have more than 30 lines. There's
   no satisfactory value to use here.

Also most people would expect a clear screen function to position the
cursor at the top left position of the screen.

Probably the best you can do in C is simply putchar('\f'). A number of
terminals do recognise this as a clear-screen code but there are still no
guarantees.

--
-----------------------------------------


-----------------------------------------



Sun, 24 Dec 2000 03:00:00 GMT  
 Clear Screen

Quote:


> > void clear_screen (void)
> <snip `output 30 newlines' technique>

> ...It also doesn't do what most people want -- namely, moving the
> cursor to the top left of the screen.

That's a good point.  I think that the c.l.c FAQ 19.4 should include
that observation if it is going to suggest the multiple newline
"solution".

 - Larry Weiss



Sun, 24 Dec 2000 03:00:00 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Init Screen w/o Clear Screen

2. Clearing screen before displaying menu

3. clearing screen

4. Clear Screen

5. ANSI clear screen

6. Help clearing screen

7. clear screen for Linux

8. Clear screen and cursor-position

9. Clear screen

10. Clear screen

11. clear screen for Linux

12. CLEAR SCREEN?

 

 
Powered by phpBB® Forum Software