2014-01-20 51 views
-2
#include <stdio.h> 

int main(){ 

    char last[20]; 
    char first[20]; 

    printf("Please enter your last name:"); 
    scanf("%s",last); 
    printf("\nPlease enter your first name:"); 
    scanf("%s",first); 
    printf("Here your email address\n",last,first,@student.com); //last [email protected] 

} 

我希望用戶寫出他們的姓名,我會自動輸出他們的電子郵件。詢問用戶的姓名和電子郵件地址作爲輸出

+1

的printf不起作用這種方式。 –

+0

http://www.cprogramming.com/tutorial/printf-format-strings.html – phimuemue

回答

7

變化:

printf("Here your email address\n",last,first,@student.com); 

要:

printf("Here your email address: %s%[email protected]\n",last,first); 
0

的問題是在代碼的最後一行程序結束前:

printf("Here your email address\n",last,first,@student.com); //last [email protected] 

它看起來像你」重新嘗試使用printf,就像使用Python或其他語言一樣,您可以將字符串添加到一起,然後顯示他們。這不是printf的工作方式。如果你看看documentation for printf,在第一個參數中你基本上是爲結果字符串定義一個模板。 「嘿,我想打印一些文本,然後在這裏我想顯示一個字符串變量的結果,這裏我想顯示一個整數,」等等。然後,其餘的參數是你想要顯示的變量字符串,在你的第一個參數列出的順序排列:

// NOT WORKING CODE! 
printf(template string, var1, var2, ..., varX); 

當您創建的模板字符串,你告訴別人你想利用所謂的格式說明某一個地方一個變量C。有一大堆,他們告訴C你會有什麼樣的變量。有關可用格式說明符的表格,請參閱上面的鏈接。就你而言,因爲你想顯示一個字符串,格式說明符是%s。 (請注意,格式說明是一樣的scanf),那麼最後一行將下面,假設你想要的電子郵件地址之後的換行符:

printf("Here your email address %s%[email protected]\n",last,first); //last [email protected] 
相關問題