我在CS50 IDE中遇到了printf函數的問題。當我使用printf打印出一個字符串(此代碼中的鹽)時,正在輸出的字符不在原始參數(argv)中。CS50 IDE:printf返回額外的字符
下面發佈是我的代碼。任何幫助,將不勝感激。謝謝。
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
int main(int argc, string argv[])
{
// ensuring that only 1 command-line argument is inputted
if (argc != 2)
{
return 1;
}
char salt[2];
for (int i = 0; i < 2; i++)
{
char c = argv[1][i];
salt[i] = c;
}
printf("the first 2 characters of the argument is %s\n", salt);
}
C字符串必須是_null-terminated_,但您的字符串不是。 – ForceBru
向我們展示原始參數(argv)._ – BLUEPIXY
'printf(「參數的前2個字符是%s \ n」,salt);' - >'printf(「參數的前2個字符是% .2s \ n「,salt);' – BLUEPIXY