Thread: Invalid command Color (Doesn't work in turbo C but works fine in Code::Blocks)

  1. #1
    Registered User
    Join Date
    Aug 2017
    Posts
    28

    Invalid command Color (Doesn't work in turbo C but works fine in Code::Blocks)

    Here is my code:

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    main()
    {
    clrscr();
    system("Color F0");
    
    
    printf("Hello world!");
    getch();
    return 0;
    
    
    }

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Do you have a question hidden somewhere in your post?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Aug 2017
    Posts
    28
    In code::blocks when i run it

    It runs fine with output having black text in white background

    But in turbo c

    It shows invalid color command and then shows normal text.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by kdushyant297 View Post
    In code::blocks when i run it

    It runs fine with output having black text in white background

    But in turbo c

    It shows invalid color command and then shows normal text.
    It still the statement - not a question.

    So instead of answer - I have 2 comments:

    1. system is not a portable solution so while it can work in one environment do not expect it to work in another.
    2. turbo C is 25 years old. Use it only if you have somewhere stored a 25 years old computer and plan to run your programs on it. IBM XT maybe.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    If it's just colored text then you could use <windows.h> and SetConsoleTextAttribute() with a modern compiler.

    Code:
    #include <stdio.h>
    #include <windows.h>
    
    int main(void) {
        HANDLE hwnd = GetStdHandle(STD_OUTPUT_HANDLE);
        if (hwnd == INVALID_HANDLE_VALUE) {
            printf("No handle, no color...\n");
            return 0;
        }
        CONSOLE_SCREEN_BUFFER_INFO hwndInfo;
        DWORD oldAttributes;
        
        /* get and save old attributes */
        GetConsoleScreenBufferInfo(hwnd, &hwndInfo);
        oldAttributes = hwndInfo.wAttributes;
        
        /* You must combine red, blue, and green constants, on the foreground and background to make color.
           Leaving out the foreground (as in the example) or the background will make it black. */
        SetConsoleTextAttribute(hwnd, BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);
        printf("Colored text example in \n");
        
        SetConsoleTextAttribute(hwnd, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
        printf("Yellow, ");
        
        SetConsoleTextAttribute(hwnd, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
        printf("cyan, ");
        
        SetConsoleTextAttribute(hwnd, FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
        printf("and magenta.\n");
        
        SetConsoleTextAttribute(hwnd, oldAttributes);
        printf("Think in 8-bit RGB combinations.\n");
        
        return 0;
    }
    See all the color attributes here.
    Last edited by whiteflags; 09-09-2017 at 01:52 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Changing the left margin color in Code Blocks
    By Jefff in forum C++ Programming
    Replies: 2
    Last Post: 03-25-2016, 12:45 PM
  2. Replies: 1
    Last Post: 03-28-2015, 08:59 PM
  3. Code::Blocks F9 Doesn't Do Anything
    By Tarebare30 in forum C++ Programming
    Replies: 3
    Last Post: 10-30-2012, 07:48 AM
  4. Replies: 2
    Last Post: 03-06-2012, 08:04 AM
  5. Why This doesn't work and the other one works?
    By chottachatri in forum C++ Programming
    Replies: 21
    Last Post: 03-26-2008, 08:01 AM

Tags for this Thread