Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม C

wprintf() และ wscanf ใน C Library


wprintf() และ wscanf() เป็น printf() และ scanf() สำหรับ wide characters อยู่ใน wchar.h

wprintf() พิมพ์ wide character ไปยัง standard output wide string format อาจมี format specifiers เริ่มต้น % แทนที่ด้วยค่าตัวแปรที่ส่งเข้าไป

ไวยากรณ์:

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

รับ format เป็นพอยน์เตอร์ไปยัง null-terminated wide string เขียนในคอนโซล เก็บ wide characters และ format specifiers เริ่มต้น % (...) แสดงอาร์กิวเมนต์เพิ่มเติม ข้อมูลที่จะพิมพ์ ตามลำดับ format specifiers

คืนค่าจำนวนตัวอักษรที่พิมพ์ ล้มเหลวอาจคืนค่าติดลบ

ตัวอย่าง

#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 \n", my_int); wprintf(L"The string is: %ls \n", string); }

ผลลัพธ์

The my_int is: 10 The string is: Hello World

wscanf() รับข้อมูลจากคอนโซลเก็บในตัวแปร อาร์กิวเมนต์เพิ่มเติมควรชี้ไปที่ออบเจ็กต์ที่จัดสรรแล้ว ตามชนิดที่ระบุใน format specifier ภายใน format string

ตัวอย่าง

#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 \n", my_int); }

ผลลัพธ์

Enter a number: 40 The given integer is: 40