logo

C의 isdigit() 함수

이 항목에서는 C 언어의 isdigit() 함수에 대해 설명합니다. isdigit() 함수는 C 라이브러리에 미리 정의된 함수로 해당 문자가 0부터 9까지의 숫자인지 확인하는 데 사용됩니다. 그리고 주어진 문자가 숫자 또는 숫자인 경우 실제 부울 값 또는 0이 아닌 값을 반환합니다. 그렇지 않으면 0 또는 거짓 값을 반환합니다. isdigit 함수는 ctype.h 헤더 파일 내부에 선언되어 있으므로 C 프로그램에 추가해야 합니다.

예를 들어, isdigit() 함수에 문자 'h'를 입력한다고 가정해 보겠습니다. 이 함수는 입력 문자가 숫자인지 여부를 확인합니다. 여기서 문자는 숫자가 아닙니다. 따라서 isdigit() 함수는 0 또는 거짓 값을 반환합니다. 마찬가지로 isdigit() 함수에 숫자 '5'를 다시 입력합니다. 이번에는 '5'가 0부터 9자리까지의 숫자이기 때문에 함수는 true 또는 0이 아닌 값을 반환합니다.

CSS에서 이미지 중앙 정렬

isdigit() 함수의 구문

다음은 C 언어의 isdigit() 함수 구문입니다.

 int isdigit (int ch); 

매개변수:

채널 - isdigit() 함수에 전달될 숫자 문자를 정의합니다.

반환 값:

isdigit() 함수는 'ch' 인수를 확인하고 전달된 문자가 숫자이면 0이 아닌 값을 반환합니다. 그렇지 않으면 0 또는 거짓 부울 값이 표시됩니다.

슈레야 고샬

예제 1: 주어진 문자가 숫자인지 확인하는 프로그램

C 프로그래밍의 isdigit() 함수를 사용하여 주어진 문자가 숫자인지 확인하는 프로그램을 만들어 보겠습니다.

 /* Check whether the given characters are digits or not in C. */ #include #include int main () { // Use the isdigit() function to check the character is digit or not. */ if (isdigit ( 'P' ) == 0) //check 'P' is digit { printf (' 
 The given character 'P' is not digit'. '); } else { printf ('
 The given character 'P' is a digit. '); } if (isdigit ( '3' ) == 0) //check for '3' { printf (' 
 The given character '3' is not digit'. '); } else { printf ('
 The given character '3' is a digit. '); } if (isdigit ( '!' ) == 0) //check for '!' character { printf (' 
 The given character '!' is not digit'. '); } else { printf ('
 The given character '!' is a digit. '); } if (isdigit ( '' ) == 0) //check for '44' { printf (' 
 The given character '' is not digit'. '); } else { printf ('
 The given character '' is a digit. '); } if (isdigit ( '0' ) == 0) //check for '0' { printf (' 
 The given character '0' is not a digit. '); } else { printf ('
 The given character '0' is a digit. '); } return 0; } 

산출:

 The given character 'P' is not a digit. The given character '3' is a digit. The given character '!' is not a digit. The given character '' is not a digit. The given character '0' is not a digit. 

위 프로그램에서는 isdigit() 함수를 사용하여 'P', '3', '!', '', 0과 같은 다양한 문자를 정의하여 이러한 문자가 유효한 문자인지 여부를 확인했습니다. 그런 다음 문자 'P', '1', 가 숫자가 아닌지 확인하고 반환하는 isdigit() 함수를 사용합니다.

예제 2: 사용자가 입력한 문자가 숫자인지 확인하는 프로그램

C의 isdigit() 함수를 이용하여 입력된 문자가 유효한지 여부를 알아내는 프로그램을 작성해보자.

선반 개
 /* Check whether the given characters are digits or not in C. */ #include #include int main () { char n; // declare an integer type variable n printf (' Enter a number to check for valid digits: '); scanf ('%c', &n); // use the isdigit() function to check the digit if (isdigit (n)) { printf (' 
 It is a digit. '); } else { printf (' 
 It is not a digit. '); } return 0; } 

산출:

 Enter a number to check for valid digits: 5 It is a digit. 

위 프로그램에서는 사용자로부터 '5' 문자를 입력한 후 isdigit 함수를 사용하여 전달된 인수가 숫자인지 확인합니다. 여기서 전달된 문자는 숫자이므로 isdigit() 함수는 'It is a digit'라는 구문을 반환합니다.

2nd실행

 Enter a number to check for valid digits: A It is not a digit. 

마찬가지로 isdigit() 함수에 문자 'A'를 입력하면 함수는 유효한 문자를 확인하고 문자 'A'는 숫자가 아님을 알 수 있습니다. 따라서 함수는 '숫자가 아닙니다'라는 문장을 반환합니다.

예제 3: C에서 전달된 문자에 대해 0과 0이 아닌 숫자를 인쇄하는 프로그램

주어진 모든 문자를 확인하고 C의 isdigit() 함수에 전달된 문자를 기반으로 0과 0이 아닌 값을 반환하는 프로그램을 작성해 보겠습니다.

JS 교체
 /* create a simple program to return zeroes and non-zeroes values based on the given characters. */ #include #include int main () { char num; // declare num as the character type variable num = '0'; // check character 0 using the isdigit() function printf (' 
 The isdigit() function returns result based on character (0) passed: %d ', isdigit (num)); num = 'o'; // check character o using the isdigit() function printf (' 
 The isdigit() function returns result based on character (o) passed: %d ', isdigit (num)); num = '08'; // check character 08 using the isdigit() function printf (' 
 The isdigit() function returns result based on character (08) passed: %d ', isdigit (num)); num = '+'; // check character + using the isdigit() function printf (' 
 The isdigit() function returns result based on character (+) passed: %d ', isdigit (num)); num = '%'; // check character % using the isdigit() function printf (' 
 The isdigit() function returns result based on character ('%') passed: %d ', isdigit (num)); return 0; } 

산출:

 The isdigit() function returns result based on character (0) passed: 1 The isdigit() function returns result based on character (o) passed: 0 The isdigit() function returns result based on character (08) passed: 1 The isdigit() function returns result based on character (+) passed: 0 The isdigit() function returns result based on character (%) passed: 0 

위 프로그램에서는 isdigit() 함수를 사용하여 주어진 유효한 숫자 문자를 테스트합니다. isdigit() 함수는 숫자에 대해서는 1을 반환하고 숫자가 아닌 프로그램에 정의된 알파벳이나 특수 기호에 대해서는 0을 반환합니다.

예제 4: isdigit() 함수를 사용하여 모든 배열 요소를 확인하는 프로그램

C의 isdigit() 함수를 사용하여 배열 요소의 유효한 숫자를 모두 찾는 프로그램을 작성해 보겠습니다.

 #include #include #include int main () { int i; // declare and initialize the character type array char arr[8] = {&apos;E&apos;, &apos;08&apos;, &apos;@&apos;, &apos;-&apos;, &apos;3&apos;, &apos;/&apos;, &apos;007&apos;, &apos;$&apos;}; int arr2[8] = {0, 0, 0, 0, 0, 0, 0, 0}; // initialize arr2 with containing all zeroes element // use for loop to iterate all elements for (i = 0; i <8; i++) { check and assign digits to the arr2[i] using isdigit() function (arr[i]); } for (i="0;" i < 8; display array characters printf (' 
 '%c'', arr[i]); elements if (arr2[i] !="0)" is a digit character. '); else not valid return 0; pre> <p> <strong>Output:</strong> </p> <pre> &apos;E&apos; is not a valid digit character. &apos;8&apos; is a digit character. &apos;@&apos; is not a valid digit character. &apos;-&apos; is not a valid digit character. &apos;3&apos; is a digit character. &apos;/&apos; is not a valid digit character. &apos;7&apos; is a digit character. &apos;$&apos; is not a valid digit character. </pre> <p>We declared and initialized the arr[] variable with some elements in the above program. And then, we create another array arr2[] that contains zero (0) to assign the result to those elements that are not valid digits. The isdigit() function checks all the arr[] array elements and returns non-zero values to the valid digit elements. Else it returns zeroes (0) to non-digit elements.</p> <hr></8;>

위 프로그램의 일부 요소를 사용하여 arr[] 변수를 선언하고 초기화했습니다. 그런 다음 영(0)을 포함하는 또 다른 배열 arr2[]를 만들어 유효한 숫자가 아닌 요소에 결과를 할당합니다. isdigit() 함수는 모든 arr[] 배열 요소를 확인하고 유효한 숫자 요소에 0이 아닌 값을 반환합니다. 그렇지 않으면 숫자가 아닌 요소에 0을 반환합니다.