C 庫中的 wprintf() 和 wscanf


在這裡我們將瞭解 C 中的 wprintf() 和 wscanf() 函式。這些是針對寬字元的 printf() 和 scanf() 函式。這些函式存在於 wchar.h

wprintf() 函式用於將寬字元列印到標準輸出中。寬字串格式可以包含格式說明符,從 % 符號開始,它們會被傳遞給 wprintf() 的變數值替換。

語法如下 -

int wprintf (const wchar_t* format, ...);

此函式使用格式。此格式是一個以空結尾的寬字串指標,將在控制檯寫入。它將包含寬字元和一些以 % 開頭的格式說明符。然後,(…) 表示附加引數。這些是要列印的資料,它們根據格式說明符以序列出現。

此函式返回列印的字元數。如果失敗,它可能會返回負值。

示例

#include <stdio.h>
#include <wchar.h>
main() {
   wint_t my_int = 10;
   wchar_t string[] = L"Hello World";
   wprintf(L"The my_int is: %d 
", my_int);    wprintf(L"The string is: %ls
", string); }

輸出

The my_int is: 10
The string is: Hello World

函式 wscanf() 用於從控制檯中獲取資料並將其儲存到合適的變數中。其他引數應指向已經分配的物件,該物件型別由格式字串中相應的格式說明符指定。

示例

#include <stdio.h>
#include <wchar.h>
main() {
   wint_t my_int = 10;
   wprintf(L"Enter a number: ");
   wscanf(L"%d", &my_int);
   wprintf(L"The given integer is: %d 
", my_int); }

輸出

Enter a number: 40
The given integer is: 40

更新於: 2019 年 7 月 30 日

1K+ 檢視

開啟您的職業生涯

獲得認證,完成課程

開始
廣告
© . All rights reserved.