2014-07-08 2 views
0

나는 왜 프로그램이 작동하지 않는지 알아 내려고 노력했다. 그것은 대문자로 소문자로 바뀝니다. "k"를 입력하면 K를 반환합니다. 그런 다음 타이핑 "A"를 계속하고 "a"를 반환하지 않습니다. 하지만 왜?대문자와 소문자로 된 C

void main(void) 
    { 
     char c = getchar(); 
     printf("c = %d %c\n", c, c); 
     c = getchar(); 
     printf("c = %d %c\n", c, c); 
    } 

당신은이 출력이 표시됩니다 : 컴파일하고이 코드를 실행하면

#include <stdio.h> 
#include <stdlib.h> 


int main(){ 


char UPPER,LOWER; 

printf("Enter UPPERCASE\n"); 
UPPER = getchar(); 
if (UPPER >= 65 && UPPER <= 90) 

{ 
    UPPER = UPPER + 32; 
    printf("The UPPERCASE now is %c\n", UPPER); 

} 

printf("Enter lowercase\n"); 
LOWER = getchar(); 
if (LOWER >= 97 && LOWER <= 122) 

{ 

    LOWER = LOWER - 32; 
    printf("The lowercase now is %c\n", LOWER); 

} 


getchar(); 
getchar(); 

} 
+1

당신이 (대신 lower'', BTW 나쁜 변수 이름'upper'을 시도하고) 디버거 또는 인쇄'UPPER'와'LOWER'에서 그것을 실행하면 무엇을 자신의 가치? – John3136

답변

0

: 여기 코드는

[email protected] ~/work/tmp $ ./test 
    a 
    c = 97 a 
    c = 10 
    /* new line there*/ 

이 코드는 동일하지 않습니다,하지만 작동합니다

#include <stdlib.h> 
    #include <stdio.h> 

    #define BUFSIZE 4 
    int main(void) 
    { 
     char UPPER[BUFSIZE] = {0}, LOWER[BUFSIZE] = {0}; 
     int i; 

     printf("Enter UPPERCASE\n"); 
     fgets(UPPER, BUFSIZE, stdin); 
     for(i = 0; i < BUFSIZE; i++) 
     { 
      if (UPPER[i] >= 65 && UPPER[i] <= 90) 

      { 
       UPPER[i] = UPPER[i] + 32; 
      } 
     } 
     printf("The UPPERCASE now is %s", UPPER); 

     printf("Enter LOWERCASE\n"); 
     fgets(LOWER, BUFSIZE, stdin); 

     for(i = 0; i < BUFSIZE; i++) 
     { 
      if (LOWER[i] >= 97 && LOWER[i] <= 122) 

      { 
       LOWER[i] = LOWER[i] - 32; 
      } 
     } 
     printf("The LOWERCASE now is %s", LOWER); 
     return 0; 
    } 
0

(printf("The UPPERCASE now is %c\n", UPPER); 다음에 printf("The lowercase now is %c\n", LOWER); 이후에) getchar();을 추가하십시오. 프로그램의 대부분은 getch()로 끝납니다. 그래서 우리는 getch()가 출력을 표시하는 데 사용된다고 생각하지만 잘못되었습니다. 콘솔에서 단일 문자를 가져 오는 데 사용됩니다. 올바른 코드는 다음과 같아야합니다

#include <stdio.h> 
#include <stdlib.h> 
int main() 
{ 
char UPPER, LOWER; 
printf("Enter UPPERCASE\n"); 
UPPER = getchar(); 
if (UPPER >= 65 && UPPER <= 90) 

{ 
UPPER = UPPER + 32; 
printf("The UPPERCASE now is %c\n", UPPER); 

} 
getchar(); 
printf("Enter lowercase\n"); 
LOWER = getchar(); 
if (LOWER >= 97 && LOWER <= 122) 

{ 

LOWER = LOWER - 32; 
printf("The lowercase now is %c\n", LOWER); 

} 
getchar(); 
} 
+0

감사! 위대한 작품! – user2309261

+0

이제 EOF를 입력 할 때까지 모든 코드를 while 루프에 넣으면됩니다. 어떤 제안? – user2309261

+0

그게 효과가 있습니다. 어떤 문제에 직면 해 있습니까? – JerryGoyal

관련 문제