[英]How to add chars in C?
So this is my code:所以这是我的代码:
#include<stdio.h>
int main(){
char username[100];
int password, i;
char temp;
printf("Enter username: ");
scanf("%s", username);
for(i=0;i < strlen(username);i++){
printf("\n%d", username[i]);
temp = username[i];
password = password + temp;
}
printf("\n");
printf("Your password: %d", password);
return 0;
}
I know its a bit messy.我知道它有点乱。 I've spend HOURS trying to find out why this doesn't work, whereas this does:
我花了 HOURS 试图找出为什么这不起作用,而这样做:
#include<stdio.h>
int main(){
char r = 'r';
char a = 'a';
char f = 'f';
int i;
i = r + a + f;
printf("%d", i);
return 0;
}
first of all learn the basics of C. you want to print out a decimal number with "%d".首先学习C的基础知识。你想用“%d”打印出一个十进制数。 To output chars on console you need to use printf("%c", char_var).
要在控制台上输出字符,您需要使用 printf("%c", char_var)。 if its more than one character you need printf("%s", char[100]) (%s stands for string)
如果它不止一个字符,你需要 printf("%s", char[100]) (%s 代表字符串)
Don't use 'scanf' to take string inputs.不要使用“scanf”来获取字符串输入。 Use 'fgets'.
使用'fgets'。 Username array contains characters.
用户名数组包含字符。 You can't display them as integers.
您不能将它们显示为整数。 You can't sum up the characters.
你无法总结人物。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.