Thread: clrscr code not working

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    129

    clrscr code not working

    Hello
    when I try to compile this code. my compiler show error message. I have mention code and error message. I think it s compiler error. I am using minGW
    Code:
        #include<stdio.h>
        #include<conio.h>
        void main()
        {
        int a=10, b=20;
        int sum=0;
        clrscr();  // use clrscr() after variable declaration
        sum=a+b;
        printf("Sum: %d",s);
        getch();
        }
    hello.c: In function 'main':
    hello.c:7:5: warning: implicit declaration of function 'clrscr' [-Wimplicit-function-declaration]
    clrscr(); // use clrscr() after variable declaration
    ^
    hello.c:9:22: error: 's' undeclared (first use in this function)
    printf("Sum: %d",s);
    ^
    hello.c:9:22: note: each undeclared identifier is reported only once for each function it appears in

    How to remove error

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Well.

    1. clrscr() comes from the DOS world, but you're using a Win32 compiler.

    2. main returns int, not void - another common DOS-world mis-conception.

    3. In your printf statement, you use 's' where you should be using sum.
    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.

  3. #3
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    Code:
    #include <conio.h>
    That's a vendor-specific (and outdated) header file. It doesn't exist in modern standard C, nor does any modern C compiler (that I know of) support it.

    In general, that's a platform-specific operation. There are some libraries (see CURSES) that are written for Windows and Linux that abstract terminal operations such as that. Even such a thing as "clearing the screen" is a complex operation that will require you to link to a 3rd party library that abstracts the complexity of that operation into a simple "clrscr" call.

    Maybe you can simply remove the clrscr() call, and the include. There's really no reason for it in this simple program.
    Last edited by MacNilly; 05-15-2017 at 04:23 AM.

  4. #4
    Registered User
    Join Date
    May 2017
    Posts
    129
    I don't have much knowledge. I am just doing simple program actually I wanted to see that how does Clrscr statement work in program. I tried different type of program. but still have looking same error.

  5. #5
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,101
    Quote Originally Posted by MacNilly View Post
    Code:
    #include <conio.h>
    That's a vendor-specific (and outdated) header file. It doesn't exist in modern standard C, nor does any modern C compiler (that I know of) support it.

    In general, that's a platform-specific operation. There are some libraries (see CURSES) that are written for Windows and Linux that abstract terminal operations such as that. Even such a thing as "clearing the screen" is a complex operation that will require you to link to a 3rd party library that abstracts the complexity of that operation into a simple "clrscr" call.

    Maybe you can simply remove the clrscr() call, and the include. There's really no reason for it in this simple program.
    For the record, "conio.h" is NOT a "Vendor Specific" header file. It was first created by the Lattice C Compiler, back in 1982!

    Before the C Programming Language was standardized in the 1989/1990 ANSI/ISO Standard, (And later by C99 & C11) compilers were free to implement their "Standard" Libraries, as they saw fit, primarily based on the K&R book, but with other functions (And "features") of their own.

    Microsoft bought Lattice C to create their first compiler, including conio.h, and other compilers from that era also implemented conio.h in their compilers, Turbo-C among others.

    As for "modern" C compilers, I can't say which ones still have conio.h, and/or other Non-Standard Windows specific functions, as I only use and recommend Gcc and Clang. Programmers on all platforms should only use functions specified by the C Standards, except for some O/S specific reason.

    In any case, functions provided by conio.h should never be used!

  6. #6
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    conio.h is perfectly useable on Windows. It's just not a portable way to do things. Also, it doesn't seem to contain the clrscr command these days. Here's three possible replacements:

    See if the following works for you. I've heard that ANSI Control Codes actually work on some versions of Windows.
    Code:
    void cls(void) { printf("\033[1J\033[H"); }
    If that doesn't work then you could always use a system call. Since this calls a separate program it's not a good way to do it if you're clearing the screen a lot.
    Code:
    system("cls");   // On linux, use "clear" instead of "cls"
    Or you could use the proper Windows functions. (I wish I could test this, but it's just not physically (or mentally) possible. I'm particularly unsure about the GetStdHandle call.)
    Code:
    #include <windows.h>
    void cls(void) {
        HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
        COORD coordScreen = {0, 0};
        DWORD cCharsWritten;
        CONSOLE_SCREEN_BUFFER_INFO csbi;
        DWORD dwConSize;
        GetConsoleScreenBufferInfo(hConsole, &csbi);
        dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
        FillConsoleOutputCharacter(hConsole, (TCHAR)' ',
           dwConSize, coordScreen, &cCharsWritten);
        GetConsoleScreenBufferInfo(hConsole, &csbi);
        FillConsoleOutputAttribute(hConsole, csbi.wAttributes,
           dwConSize, coordScreen, &cCharsWritten);
        SetConsoleCursorPosition(hConsole, coordScreen);
    }
    EDIT: Having said all that, you should probably also see if _clrscr() works (just in case!).
    Last edited by algorism; 05-15-2017 at 09:57 AM.

  7. #7
    Registered User
    Join Date
    Apr 2012
    Posts
    99
    Thank you so much for your efforts. I tried your all tips but problem still remaining. should I change compiler. Should I use turbo c++

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Should I use turbo c++
    NO!

    Your best bet is to forget about trying to clear the screen, and if you do need to clear the screen just issue a bunch of line feeds until the screen is empty.

    Also what type of project did you create? All of the functions defined in the conio.h file will only work with console programs, if you try to create some kind of Windows program they will not be available.


    Jim
    Last edited by jimblumberg; 05-15-2017 at 10:59 AM.

  9. #9
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    vead (who is not the OP) is lying/joking.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Quote Originally Posted by algorism View Post
    vead (who is not the OP) is lying/joking.
    But they do have the same IP address.
    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.

  11. #11
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    "But they do have the same IP address."

    Woah!

    Anyways... Maybe I meant "non-standard" instead of "vendor-specific". Although, I bet different vendors DID provide different prototypes within "conio.h". I mean, since its not standard, every "conio.h" you'll see is probably different. ANd Yes, you should use Tubro C++!!

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    > Maybe I meant "non-standard" instead of "vendor-specific".

    Meh, the end result is the same: you can't depend on it being available or the same across compilers.

    > I bet different vendors DID provide different prototypes within "conio.h".

    Yes, they did indeed. For example, Microsoft's compiler has conio.h, but clrscr isn't provided. The behavior of the functions tends to be consistent, but lack of a guarantee is troublesome. As such, I typically recommend not using conio without an extremely strong reason. And most of the reasons don't make the cut.

    > ANd Yes, you should use Tubro C++!!

    Why? And which version? Inquiring minds want to know, because unless you're maintaining legacy code written in Turbo C++, this is almost guaranteed to be bad advice.
    My best code is written with the delete key.

  13. #13
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,101
    Quote Originally Posted by Prelude View Post
    > ANd Yes, you should use Tubro C++!!

    Why? And which version? Inquiring minds want to know, because unless you're maintaining legacy code written in Turbo C++, this is almost guaranteed to be bad advice.
    It's a joke son! I say a joke! ;^)

    @Macnilly was only joking! Turbo C & C++ are both a joke! ;^)

    gcc & clang are the only compilers I recommend and use.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with clrscr();
    By C_programmer.C in forum C Programming
    Replies: 11
    Last Post: 04-10-2011, 10:53 PM
  2. clrscr()
    By koyboy in forum C Programming
    Replies: 3
    Last Post: 05-24-2008, 11:01 AM
  3. Help with clrscr
    By Mrazerty in forum C++ Programming
    Replies: 7
    Last Post: 04-22-2008, 11:20 AM
  4. do i need a lib for clrscr()?
    By FingerPrint in forum C++ Programming
    Replies: 2
    Last Post: 08-02-2006, 12:51 PM
  5. Clrscr()
    By Jpre in forum C Programming
    Replies: 3
    Last Post: 03-05-2002, 12:42 PM

Tags for this Thread