다른 형식의 인수를받는 함수를 어떻게 정의 할 수 있는지 알고 싶습니다.다른 인수를 가진 함수
예를 들어 인쇄 할 문자열과 첫 번째 문자열의 배경색을 나타내는 정수를 정의하려는 경우 (콘솔에서 색상을 변경하는 데는 문제가 없습니다)
그러나 함수가 첫 번째 문자열 만받는 경우 배경색을 기본값 (예 : 검정색)으로 선택해야합니다.
"main"함수에이 기능이 있기 때문에이 질문에 대한 답을 얻을 수 있다고 생각합니다. 인수 나 argc 및 argv를받을 수 없습니다.
저는 초보자입니다 C 프로그래머입니다.
편집:
//suppose I have defined colors
#define first(a,...) (a)
#define second(a,b,...) (b)
#define third(a,b,c,...) (c)
#define myprint(...) (myPrintf(first(__VA_ARGS__,BLACK),second(__VA_ARGS__,GRAY),third(__VA_ARGS__)))
void myPrintf(int backgroundColor,int textColor,char * string){
int color=16*backgroundColor+textColor;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),color);
printf("%s",string);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),0x07);
}
int main(){
myprint(,,"hello");//error
}
하지만 : 나는 void myPrintf(int backgroundColor,int textColor,char * string)
기능이이 코드를 작성하고 내가 같은 결과를 원하는
Frxstrem의 대답 한 후 Frxstrem의이 개 인수 기능에 대한 답변으로 이 오류가 발생합니다 :
error C2059: syntax error : ')'
가변 기능에 대해 알아보십시오. http : // ko에서 이렇게하기 시작할 수도 있습니다.wikipedia.org/wiki/Variadic_function – alk
함수 오버로딩에 대한 자세한 내용은 http://stackoverflow.com/questions/479207/function-overloading-in-c – kwierman
'메인'에 대해서는 [두 가지 형식이 정의되어 있습니다 ] (http://stackoverflow.com/questions/4207134/what-is-the-proper-declaration-of-main). – usr2564301