none
一个简单的C语言程序遇到了一个不知道如何解决的问题 RRS feed

  • 问题

  • #include<stdio.h>
    #define MAX 200
    int main(void) {
    	char n[MAX];
    	int temp = 0, sum = 0, count = 0, sum_temp = 0;
    	int val[10];
    	scanf_s("%s", n);
    	for (; n[temp] != '\0'; temp++) {
    		switch (n[temp]) {
    		case '0': break;
    		case '1': sum += 1; break;
    		case '2': sum += 2; break;
    		case '3': sum += 3; break;
    		case '4': sum += 4; break;
    		case '5': sum += 5; break;
    		case '6': sum += 6; break;
    		case '7': sum += 7; break;
    		case '8': sum += 8; break;
    		case '9': sum += 9; break;
    		default: break;
    		}
    	}
    	sum_temp = sum;
    	if (sum_temp == 0) {
    		printf("ling");
    	}
    	else {
    		while (sum_temp) {
    			val[count++] = sum_temp % 10;
    			sum_temp /= 10;
    		}
    		while (count--) {
    			switch (val[count]) {
    			case 0: printf("ling");  break;
    			case 1: printf("yi"); break;
    			case 2: printf("er"); break;
    			case 3: printf("san"); break;
    			case 4: printf("si"); break;
    			case 5: printf("wu"); break;
    			case 6: printf("liu"); break;
    			case 7: printf("qi"); break;
    			case 8: printf("ba"); break;
    			case 9: printf("jiu"); break;
    			default: break;
    			}
    			if (count > 0)
    				putchar(' ');
    		}
    	}
    	return 0;
    }

    源代码如上所示。在Visual Studio 2015上debug时在scanf_s这个语句执行后就弹出了下列提示:

    Exception thrown at 0x0F72FBA4 (ucrtbased.dll) in ConsoleApplication1.exe: 0xC0000005: Access violation writing location 0x00190000.

    If there is a handler for this exception, the program may be safely continued.

    为什么我在其他环境中编译这个程序没有问题却在VS上出了莫名其妙的异常?

    还有怎么关闭对scanf这个函数错误的提示?

    2016年2月3日 15:46

答案

全部回复