[英]im trying to display the full name like the output did , but whenever i hit enter the program just print out 1 word rather than 2 word
[英]Extra Character Whenever I Print Out at An Output
我正在嘗試做一個FLAMES程序作為賦值,並且由於我無法在此處准確地發布整個代碼,因此我將鍵入似乎導致我出錯的代碼部分,因為每當我打印出某些東西時,都不會多余的字符與輸出一起使用。
我使用了與此類似的代碼:
int main(){
char chari[100], temp[100];
int i, c;
printf("Enter a name: \n");
scanf(" %[^\n]s", chari);
for (i=1; chari[i]!='\0'; i++)
{
printf("%i\n", i);
}
c = i;
for (i=0; i<c; i++)
{
printf("%i < %i\n", i, c);
temp[i] = chari[i];
}
printf("%s \n", temp);
return 0;
}
我已經調整了幾個小時的代碼,但似乎仍然找不到問題。 我也在計算字符串中的字母數,以便稍后可以停止程序的某些部分。
輸入:很酷
預期產量:涼爽
實際輸出:冷卻(<-t
您忘記復制終止的空字符。
在第一個循環之后, c = i;
保留0字節的索引。 在第二個循環中,您運行直到i < c
,即您不復制該0字節而不終止nul,只要在內存中找到另一個隨機的0字節,您的字符串就可以。
在函數內,僅靜態變量被初始化。 因此,您的temp
數組擁有不確定的值,並且您不能依賴在需要的地方獲取0字符。
您需要再復制1個字節。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.