我的代码是

#include <stdio.h>
int main()
{
    int name;
    scanf("%d",&name);
    printf("%d",name);
}


为什么当我输入“ Hello”,“ World”,“ Good”等时必须显示2?
为什么2不是其他数字?
如果我想扫描字符串和printf的ASCII码,该怎么办?
谢谢

最佳答案

要打印输入字符串的ascii值:

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

int main(){
  char array[100];
    int i=0;
    printf("Enter string : ");
    scanf("%s",array);    // fixed as suggested in comments

    for(i=0;i<strlen(array);i++){

        printf("%d-",array[i]);

    }
    printf("\n");
    return 0;
}


此代码的输出:

Enter string : hello
104-101-108-108-111-


编辑:

正如好人在评论中指出的那样...确保为某些值定义了数组大小,您认为输入大小永远不会超过...

关于c - 为什么我用C语言的int变量charf和具有%d的printf用c语言scanf必须显示2?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19222623/

10-11 22:04
查看更多