0

I want to set two different colors for main and add function. But in this way, when add function is executed whole output text color turning into red.

#include<stdio.h>
#include<windows.h>

int add(a,b){
    system("Color 4");
    printf("the sum of two numbers %d",a+b);

}

int main(){
    int a,b;
    system("Color A");
    printf("\nEnter two numbers\n");
    scanf("%d %d",&a,&b);
    add(a,b);
    return 0;

}
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
Ishan Ahmed
  • 105
  • 11
  • 5
    Does this answer your question? [Colorizing text in the console with C++](https://stackoverflow.com/questions/4053837/colorizing-text-in-the-console-with-c) – Stack Danny Apr 10 '21 at 11:32
  • can i do similar thing with c? – Ishan Ahmed Apr 10 '21 at 11:39
  • 1
    @IshanAhmed Yes `GetStdHandle` and `SetConsoleTextAttribute` are both C functions. – Irelia Apr 10 '21 at 11:42
  • 1
    @IshanAhmed: The accepted answer of the linked question also applies to C, except for the line containing `cout`, as that is only C++ and not C. Just pretend that the line is the following `printf` statement: `printf( "%d I want to be nice today!", k );` – Andreas Wenzel Apr 10 '21 at 11:44
  • 2
    You are missing one important thing and that is checking if `scanf` was actually successful: `if(scanf("%d %d",&a,&b) == 2) { add(a, b); }` – Ted Lyngmo Apr 10 '21 at 11:47

0 Answers0