logo

C의 함수 프로토타입

소개:

C 프로그래밍에서는 함수 프로토타입 선언하는 데 사용됩니다. 서명 다음을 포함하는 함수의 이름, 반환 유형 , 그리고 매개변수 . 함수 프로토타입은 함수가 호출되기 전에 컴파일러에 함수의 인터페이스를 알리고 적절한 유형 검사 및 오류 처리를 가능하게 하기 때문에 중요합니다. 이 글에서는 C 프로그래밍에서 함수 프로토타입의 중요성과 사용법에 대해 논의할 것입니다.

함수 프로토타입을 사용하는 이유는 무엇입니까?

함수 프로토타입 여러 가지 이유로 C 프로그래밍에서 중요합니다. 가장 중요한 이유 중 하나는 그들이 다음을 허용한다는 것입니다. 컴파일러 프로그램이 실제로 실행되기 전에 오류를 확인합니다. 잘못된 개수나 인수 유형을 사용하여 함수를 호출하면 컴파일러는 다음을 생성합니다. 에러 메시지 , 런타임 시 프로그램이 충돌하거나 예기치 않게 동작하는 것을 방지합니다.

함수 프로토타입을 사용하는 또 다른 중요한 이유는 모듈식 프로그래밍을 가능하게 하기 위해서입니다. C에서 함수는 일반적으로 기본 프로그램과 별도의 파일에 정의되며 컴파일 타임에 함께 연결됩니다. 기본 프로그램과 함수 정의 파일에 모두 포함된 헤더 파일에 함수 프로토타입을 선언하면 함수의 구현 세부 사항에 액세스하지 않고도 프로그램의 어느 부분에서나 함수를 호출할 수 있습니다.

함수 프로토타입 또한 코드를 더 쉽게 읽고 이해할 수 있습니다. 소스 코드에 함수의 시그니처를 포함하면 다른 개발자가 함수의 기능, 인수, 반환 유형을 쉽게 확인할 수 있습니다. 이는 코드를 보다 자체적으로 문서화하고 코드에 대한 오해나 잘못된 해석으로 인해 발생할 수 있는 버그의 가능성을 줄여줍니다.

카잘 아가르왈

함수 프로토타입의 구문:

C 프로그래밍에서 함수 프로토타입의 구문은 다음과 같습니다.

 return_type function_name(parameter_list); 

그만큼 반환_유형 데이터 유형은 다음과 같습니다. 함수 반환 , 와 같은 정수, 부동 , 또는 . 그만큼 함수_이름 의 이름이다 기능 , 그리고 매개변수_목록 쉼표로 구분된 목록입니다. 매개변수 그 기능이 걸립니다. 각 매개변수는 매개변수_목록 데이터 유형과 그 뒤에 오는 데이터 유형으로 구성됩니다. 매개변수 이름 .

예를 들어, 다음은 두 개의 함수를 취하는 함수의 함수 프로토타입입니다. 정수 인수로 사용하고 해당 합계를 반환합니다.

 int add(int num1, int num2); 

이 예에서 반환 유형은 다음과 같습니다. 정수 , 함수 이름은 다음과 같습니다. 추가하다 , 매개변수 목록은 다음과 같은 두 개의 정수로 구성됩니다. 숫자1 그리고 숫자 2 .

기본 함수 프로토타입:

C 프로그래밍에서 함수가 호출되기 전에 호출되는 경우 한정된 또는 선언하다 , 컴파일러는 기본 함수 프로토타입을 가정합니다. 그만큼 기본 함수 프로토타입 함수가 다음을 반환한다고 가정합니다. 정수 모든 유형의 인수를 원하는 만큼 취합니다.

예를 들어 다음 코드를 고려해보세요.

 #include int main() { printf('The sum is %d
', add(2, 3)); return 0; } int add(int num1, int num2) { return num1 + num2; } 

산출:

 The sum is 5 

설명:

이 코드에서는 기능 추가 그 전에 호출된다 선언하다 또는 한정된 . 그러나 컴파일러는 기본 함수 프로토타입을 가정하기 때문에 프로그램은 없이 컴파일됩니다. 오류 올바른 출력을 생성합니다.

기본 함수 프로토타입이 편리한 경우도 있지만 미묘한 버그와 오류가 발생할 수 있으므로 일반적으로 권장되지 않습니다. 잠재적인 문제를 방지하려면 함수 프로토타입을 명시적으로 선언하는 것이 가장 좋습니다.

함수 프로토타입 및 헤더 파일:

C 프로그래밍에서는 함수 프로토타입 헤더 파일에 포함되는 경우가 많으며, 헤더 파일은 기본 프로그램과 기능 정의 파일에 모두 포함됩니다. 이를 통해 함수의 구현 세부 사항에 액세스하지 않고도 모든 프로그램 부분에서 함수를 호출할 수 있습니다.

헤더 파일에는 일반적으로 .h 확장자 , 만 포함 함수 프로토타입 , 유형 정의 , 및 기타 선언 메인 프로그램이나 다른 파일에 필요한 것입니다. 다음은 이전의 add 함수를 선언하는 헤더 파일의 예입니다.

 #ifndef ADD_H #define ADD_H int add(int num1, int num2) 

이 예에서는 ifndef 지시문은 다음을 확인합니다. ADD_H 이미 정의되어 있습니다. 그렇지 않은 경우 다음을 정의합니다. ADD_H 그리고 추가를 위한 함수 프로토타입을 포함시킵니다.

그만큼 정의하다 지시문은 매크로 명명 된 ADD_H , 헤더 파일이 각 파일에 한 번만 포함되도록 하는 데 사용할 수 있습니다. 동일한 함수를 여러 번 선언하면 오류가 발생할 수 있으므로 방지하는 것이 중요합니다. 그만큼 함수 프로토타입 for add는 단순히 함수가 두 개의 정수를 인수로 취하고 정수를 반환한다고 선언합니다. 어떻게 구현되었는지 알지 못해도 add 함수를 올바르게 호출하려면 기본 프로그램과 기타 파일에 대한 충분한 정보입니다.

헤더 파일이 포함된 경우 C 프로그램 , 전처리기 대체합니다 #포함하다 내용이 포함된 지시문 헤더 파일 . 이를 통해 메인 프로그램과 기타 파일이 헤더 파일의 함수 프로토타입과 기타 선언에 액세스할 수 있습니다.

C 함수 프로토타입의 몇 가지 중요한 점:

함수 프로토타입은 오류를 잡는 데 도움이 됩니다.

언제 함수 프로토타입 C 프로그램에 포함되어 있으면 컴파일러는 프로그램을 실행하기 전에 해당 함수가 올바르게 사용되었는지 확인합니다. 프로그램이 실행되기 전에 오류를 조기에 잡는 데 도움이 됩니다.

함수 프로토타입은 대규모 프로그램에서 필수적입니다.

개발자 모드를 비활성화하는 방법

대규모 프로그램에서는 서로 다른 기능 간의 문제를 명확하게 구분하는 것이 중요합니다. 함수 프로토타입을 사용하면 다른 함수의 구현 세부 사항을 알지 못해도 각 함수를 독립적으로 개발할 수 있으므로 이러한 분리가 가능합니다.

함수 프로토타입은 헤더 파일에서 선언될 수 있습니다.

앞서 언급했듯이 함수 프로토타입은 일반적으로 헤더 파일에 선언됩니다. 그러면 헤더 파일이 기본 프로그램과 함수 정의 파일 모두에 포함되어 프로그램의 어느 부분에서나 함수에 액세스할 수 있습니다.

함수 프로토타입은 오버로드될 수 있습니다.

C는 다른 프로그래밍 언어처럼 함수 오버로드를 지원하지 않지만, 함수 프로토타입은 다른 인수 유형과 숫자를 사용하여 오버로드될 수 있습니다. 동일한 함수 이름을 다른 목적으로 사용할 수 있습니다.

함수 프로토타입에는 기본 인수 값이 포함될 수 있습니다.

C는 다른 프로그래밍 언어처럼 기본 인수 값을 지원하지 않지만 함수 프로토타입은 특수 구문을 사용하여 선택적 인수를 포함할 수 있습니다. 특정 인수를 사용하거나 사용하지 않고 동일한 함수를 사용할 수 있습니다.

함수 프로토타입은 전방 선언이 가능합니다.

어떤 경우에는 구현이 가능해지기 전에 함수 프로토타입을 선언해야 할 수도 있습니다. 그것은이라고 전방 선언 함수 선언 시 함수 구현을 알 수 없는 복잡한 프로그램에 유용할 수 있습니다.

다음은 C 프로그래밍의 함수 프로토타입에 대한 몇 가지 추가 예입니다.

예시 1:

 #include float calculate_average(int arr[], int size); int main() { int arr[] = {1, 2, 3, 4, 5}; int size = 5; float average = calculate_average(arr, size); printf(&apos;The average is: %.2f&apos;, average); return 0; } float calculate_average(int arr[], int size) { float sum = 0.0; for (int i = 0; i<size; i++) { sum +="arr[i];" } return size; < pre> <p> <strong>Output:</strong> </p> <pre> The average is: 3.00 </pre> <p> <strong>Explanation:</strong> </p> <p>In this example, we first declare the <strong> <em>calculate_average</em> </strong> function prototype at the beginning of our program before the main function. After that, inside the main function, we declare an integer array <strong> <em>arr</em> </strong> with some values and a size of <strong> <em>5</em> </strong> . After that, we call the <strong> <em>calculate_average function</em> </strong> , passing in the <strong> <em>arr array</em> </strong> and its size, and store the result in a <strong> <em>float variable</em> </strong> named <strong> <em>average</em> </strong> . Finally, we print out the result using printf.</p> <p>The <strong> <em>calculate_average</em> </strong> function takes in the integer <strong> <em>array arr</em> </strong> and its size as arguments and returns the average value of the array as a <strong> <em>float</em> </strong> . We first declare a float variable named <strong> <em>sum</em> </strong> inside the function and initialize it to <strong> <em>0.0</em> </strong> . After that, we loop through each element in the array using a <strong> <em>for loop</em> </strong> , adding each element to the sum variable. Finally, we return the result of dividing the sum variable by the array size.</p> <p>Its average is <strong> <em>3.00</em> </strong> because the <strong> <em>arr</em> </strong> array contains the values <strong> <em>{1, 2, 3, 4, 5}</em> </strong> , and the average of these values is <strong> <em>(1+2+3+4+5)/5 = 3.00</em> </strong> . The <strong> <em>printf</em> </strong> statement in the main function uses the <strong> <em>%f format specifier</em> </strong> to print out the average value as a floating-point number. The <strong> <em>.2 modifier</em> </strong> specifies that we want to print only two decimal places.</p> <p> <strong>Example 2:</strong> </p> <pre> #include void print_message(char *msg); int main() { char *msg = &apos;Hello, world!&apos;; print_message(msg); return 0; } void print_message(char *msg) { printf(&apos;%s
&apos;, msg); } </pre> <p> <strong>Output:</strong> </p> <pre> Hello, world! </pre> <p> <strong>Explanation:</strong> </p> <p>In this example, we first declare the <strong> <em>print_message</em> </strong> function prototype at the beginning of our program, before the main function. Then, inside the main function, we declare a character pointer <strong> <em>msg</em> </strong> and initialize it to point to a string literal <strong> <em>&apos;Hello, world!&apos;</em> </strong> . After that, we call the <strong> <em>print_message</em> </strong> function, passing in the <strong> <em>msg pointer</em> </strong> .</p> <p>The <strong> <em>print_message</em> </strong> function takes in a character pointer <strong> <em>msg</em> </strong> as an argument, and returns nothing <strong> <em>(void)</em> </strong> . Inside the function, we use the <strong> <em>printf function</em> </strong> to print out the string pointed to by <strong> <em>msg</em> </strong> , followed by a <strong> <em>newline character (
)</em> </strong> . The <strong> <em>%s</em> </strong> format specifier is used to print out a string.</p> <p>The Output is <strong> <em>Hello, world!</em> </strong> . Because the <strong> <em>print_message</em> </strong> function prints out the string pointed to by the <strong> <em>msg pointer</em> </strong> , which in this case is <strong> <em>&apos;Hello, world!&apos;</em> </strong> , followed by a newline character.</p> <p> <strong>Example 3:</strong> </p> <pre> #include int factorial(int n); int main() { int n = 5; int result = factorial(n); printf(&apos;%d! = %d
&apos;, n, result); return 0; } int factorial(int n) { if (n == 0) { return 1; } else { return n * factorial(n-1); } } </pre> <p> <strong>Explanation:</strong> </p> <p>In this example, we first declare the <strong> <em>factorial function</em> </strong> prototype at the beginning of our program, before the main function. Then, inside the main function, we declare an integer variable <strong> <em>n</em> </strong> and initialize it to <strong> <em>5</em> </strong> . After that, we call the factorial function, passing in <strong> <em>n</em> </strong> , and store the result in an integer variable named <strong> <em>result</em> </strong> . Finally, we print out the result using <strong> <em>printf</em> </strong> .</p> <p>The factorial function takes in an integer <strong> <em>n</em> </strong> as an argument, and returns its factorial as an <strong> <em>integer</em> </strong> . Inside the function, we first check if <strong> <em>n</em> </strong> is equal to <strong> <em>0</em> </strong> . If it is, we return <strong> <em>1</em> </strong> , since <strong> <em>0! = 1</em> </strong> by definition. Otherwise, we return <strong> <em>n * factorial(n-1)</em> </strong> , which is the factorial of <strong> <em>n</em> </strong> calculated recursively as the product of <strong> <em>n</em> </strong> and the factorial of <strong> <em>n-1</em> </strong> .</p> <p>The output of the code will be:</p> <pre> 5! = 120 </pre> <p>This is because the <strong> <em>factorial function</em> </strong> calculates <strong> <em>5!</em> </strong> as <strong> <em>5 * 4 * 3 * 2 * 1 = 120</em> </strong> , and this result is printed out using <strong> <em>printf</em> </strong> .</p> <p> <strong>Example 4:</strong> </p> <pre> #include int find_max(int arr[], int size); int main() { int arr[] = {3, 5, 2, 8, 1}; int size = sizeof(arr) / sizeof(int); int max = find_max(arr, size); printf(&apos;The maximum value in the array is: %d
&apos;, max); return 0; } int find_max(int arr[], int size) { int max = arr[0]; for (int i = 1; i max) { max = arr[i]; } } return max; } </pre> <p> <strong>Explanation:</strong> </p> <p>In this example, we first declare the <strong> <em>find_max</em> </strong> function prototype at the beginning of our program, before the main function. Then, inside the main function, we declare an integer <strong> <em>array arr</em> </strong> and initialize it with some values, and a variable size that stores the size of the array. After that, we call the <strong> <em>find_max function</em> </strong> , passing in the <strong> <em>arr array</em> </strong> and <strong> <em>size</em> </strong> , and store the result in an integer variable named <strong> <em>max</em> </strong> . Finally, we print out the result using <strong> <em>printf</em> </strong> .</p> <p>The <strong> <em>find_max function</em> </strong> takes in an integer array <strong> <em>arr</em> </strong> and its size <strong> <em>size</em> </strong> as arguments, and returns the maximum value in the array as an integer. Inside the function, we first initialize a variable max with the first element of the array arr. After that, we loop over the remaining elements of the array using a for loop, comparing each element to the current maximum value using an if statement. If the current element is greater than the current maximum, we update max to the value of the current element. After the loop finishes, we return the final value of max.</p> <p>The <strong> <em>output</em> </strong> of the code will be:</p> <pre> The maximum value in the array is: 8 </pre> <p>This is because the <strong> <em>find_max</em> </strong> function searches through the array <strong> <em>{3, 5, 2, 8, 1}</em> </strong> and finds that the maximum value is <strong> <em>8</em> </strong> , which is then printed out using <strong> <em>printf</em> </strong> .</p> <p>Overall, function prototypes are an essential part of C programming that allow for <strong> <em>modular programming</em> , <em>type checking</em> , <em>error handling</em> </strong> , and <strong> <em>self-documenting code</em> </strong> . By declaring function prototypes, developers can write more robust, maintainable, and error-free code.</p> <p> <strong>Example 5:</strong> </p> <pre> #include void greet_user(char *name); int main() { char name[50]; printf(&apos;What is your name? &apos;); scanf(&apos;%s&apos;, name); greet_user(name); return 0; } void greet_user(char *name) { printf(&apos;Hello, %s! Nice to meet you.
&apos;, name); } </pre> <p> <strong>Explanation:</strong> </p> <p>In this example, we first declare the <strong> <em>greet_user function</em> </strong> prototype at the beginning of our program, before the main function. Then, inside the main function, we declare a character array name with a size of <strong> <em>50</em> </strong> , and use <strong> <em>printf</em> </strong> and <strong> <em>scanf</em> </strong> to ask the user for their name and read it into the name array. After that, we call the <strong> <em>greet_user function</em> </strong> , passing in the name array as an argument.</p> <p>The <strong> <em>greet_user function</em> </strong> takes in a character pointer name as an argument, which is a pointer to the first character of a string. Inside the function, we use <strong> <em>printf</em> </strong> to print out a greeting message that includes the user&apos;s name, and a friendly message.</p> <p>The output of the code will depend on the user&apos;s input. Here&apos;s an example of what the output might look like:</p> <pre> What is your name? suman Hello, suman! Nice to meet you. </pre> <p>In this case, the user enters the name <strong> <em>&apos;suman&apos;</em> </strong> , and the program prints out a greeting message that includes their name.</p> <h2>Conclusion:</h2> <p> <strong> <em>Function prototypes</em> </strong> are an important part of C programming, enabling modular programming, error checking, and self-documenting code. By declaring the signature of a function before it is called, function prototypes allow the compiler to check for errors, enable modular programming, and make code easier to read and understand.</p> <p>In C programming, function prototypes are typically included in <strong> <em>header files</em> </strong> , which are then included in both the main program and the function definition files. It allows functions to be called from any part of the program without requiring access to the function&apos;s implementation details. By understanding the importance of function prototypes and how they are used in C programming, developers can write more robust, maintainable, and error-free code.</p> <hr></size;>

설명:

이 예에서는 먼저 다음을 선언합니다. 계산_평균 프로그램 시작 부분에 함수 프로토타입을 메인 함수 앞에 둡니다. 그 후, 메인 함수 내에서 정수 배열을 선언합니다. 도착 일부 값과 크기 5 . 그 후, 우리는 계산_평균 함수 , 전달 배열 도착 크기와 결과를 저장합니다. 부동 변수 명명 된 평균 . 마지막으로 printf를 사용하여 결과를 인쇄합니다.

그만큼 계산_평균 함수는 정수를 받아들인다 배열 도착 크기를 인수로 사용하고 배열의 평균 값을 반환합니다. 뜨다 . 먼저 float라는 변수를 선언합니다. 합집합 함수 내부에서 다음과 같이 초기화합니다. 0.0 . 그 후에는 다음을 사용하여 배열의 각 요소를 반복합니다. for 루프 , 합계 변수에 각 요소를 추가합니다. 마지막으로 합계 변수를 배열 크기로 나눈 결과를 반환합니다.

그 평균은 3.00 왜냐하면 도착 배열에는 값이 포함되어 있습니다. {1, 2, 3, 4, 5} , 이 값의 평균은 다음과 같습니다. (1+2+3+4+5)/5 = 3.00 . 그만큼 printf main 함수의 문에서는 다음을 사용합니다. %f 형식 지정자 평균값을 부동소수점 숫자로 인쇄합니다. 그만큼 .2 수정자 소수점 이하 두 자리만 인쇄하도록 지정합니다.

예 2:

 #include void print_message(char *msg); int main() { char *msg = &apos;Hello, world!&apos;; print_message(msg); return 0; } void print_message(char *msg) { printf(&apos;%s
&apos;, msg); } 

산출:

 Hello, world! 

설명:

이 예에서는 먼저 다음을 선언합니다. print_message 함수 프로토타입은 프로그램 시작 부분, 주 함수 앞에 위치합니다. 그런 다음 메인 함수 내에서 문자 포인터를 선언합니다. 메시지 문자열 리터럴을 가리키도록 초기화합니다. '안녕하세요, 세상!' . 그 후, 우리는 print_message 함수, 전달 메시지 포인터 .

그만큼 print_message 함수는 문자 포인터를 받아들입니다 메시지 인수로 사용되며 아무것도 반환하지 않습니다. (무효의) . 함수 내에서 우리는 printf 함수 가 가리키는 문자열을 인쇄하려면 메시지 , 다음에 개행 문자( ) . 그만큼 %에스 형식 지정자는 문자열을 인쇄하는 데 사용됩니다.

출력은 안녕, 세상! . 왜냐하면 print_message 함수가 가리키는 문자열을 인쇄합니다. 메시지 포인터 , 이 경우에는 '안녕하세요, 세상!' , 그 뒤에 개행 문자가 옵니다.

예시 3:

 #include int factorial(int n); int main() { int n = 5; int result = factorial(n); printf(&apos;%d! = %d
&apos;, n, result); return 0; } int factorial(int n) { if (n == 0) { return 1; } else { return n * factorial(n-1); } } 

설명:

이 예에서는 먼저 다음을 선언합니다. 계승 함수 프로그램 시작 부분, main 함수 앞에 프로토타입을 추가합니다. 그런 다음 메인 함수 내에서 정수 변수를 선언합니다. N 그리고 그것을 초기화 5 . 그런 다음 계승 함수를 호출하여 전달합니다. N 을 입력하고 그 결과를 정수 변수에 저장합니다. 결과 . 마지막으로 다음을 사용하여 결과를 인쇄합니다. printf .

자바 초기화 배열

계승 함수는 정수를 취합니다. N 인수로 사용하고 계승값을 반환합니다. 정수 . 함수 내부에서 먼저 다음 사항을 확인합니다. N 동일하다 0 . 그렇다면 우리는 돌아간다 1 , 부터 0! = 1 정의상. 그렇지 않으면 우리는 돌아갑니다 n * 계승(n-1) , 이는 다음의 계승입니다. N 의 곱으로 재귀적으로 계산됩니다. N 그리고 계승 n-1 .

코드의 출력은 다음과 같습니다:

 5! = 120 

이는 계승 함수 계산하다 5! ~처럼 5*4*3*2*1 = 120 , 이 결과는 다음을 사용하여 인쇄됩니다. printf .

예시 4:

 #include int find_max(int arr[], int size); int main() { int arr[] = {3, 5, 2, 8, 1}; int size = sizeof(arr) / sizeof(int); int max = find_max(arr, size); printf(&apos;The maximum value in the array is: %d
&apos;, max); return 0; } int find_max(int arr[], int size) { int max = arr[0]; for (int i = 1; i max) { max = arr[i]; } } return max; } 

설명:

이 예에서는 먼저 다음을 선언합니다. find_max 함수 프로토타입은 프로그램 시작 부분, 주 함수 앞에 위치합니다. 그런 다음 메인 함수 내에서 정수를 선언합니다. 배열 도착 일부 값과 배열의 크기를 저장하는 변수 크기로 초기화합니다. 그 후, 우리는 find_max 함수 , 전달 배열 도착 그리고 크기 을 입력하고 그 결과를 정수 변수에 저장합니다. 최대 . 마지막으로 다음을 사용하여 결과를 인쇄합니다. printf .

그만큼 find_max 함수 정수 배열을 받아들입니다 도착 그리고 그 크기 크기 인수로 사용하고 배열의 최대값을 정수로 반환합니다. 함수 내에서 먼저 배열 arr의 첫 번째 요소로 변수 max를 초기화합니다. 그런 다음 for 루프를 사용하여 배열의 나머지 요소를 반복하고 if 문을 사용하여 각 요소를 현재 최대값과 비교합니다. 현재 요소가 현재 최대값보다 크면 max를 현재 요소의 값으로 업데이트합니다. 루프가 끝나면 max의 최종 값을 반환합니다.

그만큼 산출 코드는 다음과 같습니다:

 The maximum value in the array is: 8 

이는 find_max 함수는 배열을 검색합니다. {3, 5, 2, 8, 1} 그리고 최대값은 다음과 같다는 것을 알게 됩니다. 8 , 다음을 사용하여 인쇄됩니다. printf .

전반적으로 함수 프로토타입은 다음을 허용하는 C 프로그래밍의 필수 부분입니다. 모듈식 프로그래밍 , 유형 검사 , 오류 처리 , 그리고 자체 문서화 코드 . 함수 프로토타입을 선언함으로써 개발자는 더욱 강력하고 유지 관리가 가능하며 오류 없는 코드를 작성할 수 있습니다.

리눅스에서 스크립트를 실행하는 방법

예시 5:

 #include void greet_user(char *name); int main() { char name[50]; printf(&apos;What is your name? &apos;); scanf(&apos;%s&apos;, name); greet_user(name); return 0; } void greet_user(char *name) { printf(&apos;Hello, %s! Nice to meet you.
&apos;, name); } 

설명:

이 예에서는 먼저 다음을 선언합니다. Greeting_user 함수 프로그램 시작 부분, main 함수 앞에 프로토타입을 추가합니다. 그런 다음 메인 함수 내에서 크기가 다음과 같은 문자 배열 이름을 선언합니다. 오십 , 그리고 사용 printf 그리고 스캔프 사용자에게 이름을 물어보고 이름 배열로 읽어 들입니다. 그 후, 우리는 Greeting_user 함수 , 이름 배열을 인수로 전달합니다.

그만큼 Greeting_user 함수 문자열의 첫 번째 문자에 대한 포인터인 문자 포인터 이름을 인수로 사용합니다. 함수 내부에서 우리는 printf 사용자 이름과 친근한 메시지가 포함된 인사말 메시지를 인쇄합니다.

코드의 출력은 사용자의 입력에 따라 달라집니다. 다음은 출력 결과의 예입니다.

 What is your name? suman Hello, suman! Nice to meet you. 

이 경우 사용자는 이름을 입력합니다. '수맘' , 프로그램은 이름이 포함된 인사말 메시지를 인쇄합니다.

결론:

함수 프로토타입 C 프로그래밍의 중요한 부분으로, 모듈식 프로그래밍, 오류 검사 및 자체 문서화 코드를 가능하게 합니다. 함수 프로토타입을 호출하기 전에 함수의 시그니처를 선언함으로써 컴파일러는 오류를 확인하고, 모듈식 프로그래밍을 활성화하고, 코드를 더 쉽게 읽고 이해할 수 있습니다.

C 프로그래밍에서 함수 프로토타입은 일반적으로 헤더 파일 , 이는 기본 프로그램과 기능 정의 파일 모두에 포함됩니다. 이를 통해 함수의 구현 세부 사항에 액세스하지 않고도 프로그램의 어느 부분에서나 함수를 호출할 수 있습니다. 함수 프로토타입의 중요성과 C 프로그래밍에서 함수 프로토타입이 사용되는 방식을 이해함으로써 개발자는 더욱 강력하고 유지 관리가 가능하며 오류 없는 코드를 작성할 수 있습니다.