# wchar

***

**1. Displaying Unicode Characters**

```c
#include <stdio.h>
#include <wchar.h>
int main() {
  wchar_t euro = L'\u20AC';  // Euro symbol
  wprintf(L"%lc", euro);
  return 0;
}
```

**2. Converting ASCII Strings to Wide Strings**

```c
#include <stdio.h>
#include <wchar.h>
int main() {
  char *ascii = "Hello world";
  wchar_t *wide = (wchar_t *)malloc(strlen(ascii) * sizeof(wchar_t));
  mbstowcs(wide, ascii, strlen(ascii));
  wprintf(L"%ls", wide);
  return 0;
}
```

**3. Converting Wide Strings to ASCII Strings**

```c
#include <stdio.h>
#include <wchar.h>
int main() {
  wchar_t *wide = L"Hello world";
  char *ascii = (char *)malloc(wcslen(wide) * sizeof(char));
  wcstombs(ascii, wide, wcslen(wide));
  printf("%s", ascii);
  return 0;
}
```

**4. Comparing Wide Strings**

```c
#include <stdio.h>
#include <wchar.h>
int main() {
  wchar_t *str1 = L"Hello world";
  wchar_t *str2 = L"Hello world";
  int result = wcscmp(str1, str2);
  printf("%d", result);  // Output: 0 (strings are equal)
  return 0;
}
```

**5. Concatenating Wide Strings**

```c
#include <stdio.h>
#include <wchar.h>
int main() {
  wchar_t *str1 = L"Hello";
  wchar_t *str2 = L" world";
  wchar_t *concat = (wchar_t *)malloc((wcslen(str1) + wcslen(str2) + 1) * sizeof(wchar_t));
  wcscat(concat, str1);
  wcscat(concat, str2);
  wprintf(L"%ls", concat);
  return 0;
}
```

**6. Searching for a Wide Character in a String**

```c
#include <stdio.h>
#include <wchar.h>
int main() {
  wchar_t *str = L"Hello world";
  wchar_t target = L'w';
  wchar_t *found = wcschr(str, target);
  if (found) {
    wprintf(L"%ls", found);  // Output: world
  }
  return 0;
}
```

**7. Manipulating Wide Strings with Pointers**

```c
#include <stdio.h>
#include <wchar.h>
int main() {
  wchar_t str[] = L"Hello world";
  wchar_t *ptr = str;
  ptr += 5;  // Move pointer to character 'w'
  *ptr = L'i';  // Modify character to 'i'
  wprintf(L"%ls", str);  // Output: Hello iorld
  return 0;
}
```

**8. Reading Wide Characters from a File**

```c
#include <stdio.h>
#include <wchar.h>
int main() {
  FILE *file = fopen("test.txt", "r");
  wchar_t ch;
  while (fgetwc(&ch, file) != WEOF) {
    wprintf(L"%lc", ch);
  }
  return 0;
}
```

**9. Writing Wide Characters to a File**

```c
#include <stdio.h>
#include <wchar.h>
int main() {
  FILE *file = fopen("test.txt", "w");
  wchar_t str[] = L"Hello world";
  fputwcs(str, file);
  return 0;
}
```

**10. Using Wide Strings in Arrays**

```c
#include <stdio.h>
#include <wchar.h>
int main() {
  wchar_t greetings[][10] = {L"Hello", L"Bonjour", L"Hola"};
  for (int i = 0; i < 3; i++) {
    wprintf(L"%ls\n", greetings[i]);
  }
  return 0;
}
```

**11. Using Wide Strings as Command-Line Arguments**

```c
#include <stdio.h>
#include <wchar.h>
int main(int argc, wchar_t **argv) {
  for (int i = 0; i < argc; i++) {
    wprintf(L"%ls\n", argv[i]);
  }
  return 0;
}
```

**12. Using Wide Strings in Structures**

```c
#include <stdio.h>
#include <wchar.h>
typedef struct {
  wchar_t name[100];
  int age;
} Person;

int main() {
  Person person = {L"John Doe", 30};
  wprintf(L"Name: %ls, Age: %d\n", person.name, person.age);
  return 0;
}
```

**13. Using Wide Strings in Unions**

```c
#include <stdio.h>
#include <wchar.h>
typedef union {
  wchar_t name[100];
  int number;
} Data;

int main() {
  Data data = {L"John Doe"};
  wprintf(L"Name: %ls\n", data.name);
  return 0;
}
```

**14. Using Wide Strings in Macros**

```c
#include <stdio.h>
#include <wchar.h>
#define HELLO L"Hello world"

int main() {
  wprintf(L"%ls\n", HELLO);
  return 0;
}
```

**15. Using Wide Strings in Conditional Statements**

```c
#include <stdio.h>
#include <wchar.h>
int main() {
  wchar_t ch = L'\u0041';
  if (ch == L'\u0041') {
    wprintf(L"%lc is the letter 'A'\n", ch);
  }
  return 0;
}
```

**16. Using Wide Strings in Loops**

```c
#include <stdio.h>
#include <wchar.h>
int main() {
  wchar_t str[] = L"Hello world";
  for (wchar_t *ptr = str; *ptr != L'\0'; ptr++) {
    wprintf(L"%lc", *ptr);
  }
  return 0;
}
```

**17. Using Wide Strings in Functions**

```c
#include <stdio.h>
#include <wchar.h>
wchar_t *convert_to_uppercase(wchar_t *str) {
  for (wchar_t *ptr = str; *ptr != L'\0'; ptr++) {
    if (*ptr >= L'a' && *ptr <= L'z') {
      *ptr -= L'a' - L'A';
    }
  }
  return str;
}

int main() {
  wchar_t str[] = L"Hello world";
  wprintf(L"%ls\n", convert_to_uppercase(str));
  return 0;
}
```

**18. Using Wide Strings in Switches**

```c
#include <stdio.h>
#include <wchar.h>
int main() {
  wchar_t ch = L'a';
  switch (ch) {
    case L'a':
      wprintf(L"The character is 'a'\n");
      break;
    case L'b':
      wprintf(L"The character is 'b'\n");
      break;
    default:
      wprintf(L"Unknown character\n");
      break;
  }
  return 0;
}
```

**19. Using Wide Strings in Arrays of Pointers**

```c
#include <stdio.h>
#include <wchar.h>
int main() {
  wchar_t *strs[] = {L"Hello", L"Bonjour", L"Hola"};
  for (int i = 0; strs[i] != NULL; i++) {
    wprintf(L"%ls\n", strs[i]);
  }
  return 0;
}
```

**20. Using Wide Strings in Dynamically Allocated Arrays**

```c
#include <stdio.h>
#include <wchar.h>
int main() {
  wchar_t **strs = (wchar_t **)malloc(3 * sizeof(wchar_t *));
  strs[0] = (wchar_t *)malloc(6 * sizeof(wchar_t));
  strs[1] = (wchar_t *)malloc(7 * sizeof(wchar_t));
  strs[2] = (wchar_t *)malloc(5 * sizeof(wchar_t));
  wcscpy(strs[0], L"Hello");
  wcscpy(strs[1], L"Bonjour");
  wcscpy(strs[2], L"Hola");
  for (int i = 0; strs[i] != NULL; i++) {
    wprintf(L"%ls\n", strs[i]);
  }
  return 0;
}
```

**21. Using Wide Strings in Multi-dimensional Arrays**

```c
#include <stdio.h>
#include <wchar.h>
int main() {
  wchar_t strs[2][10] = {L"Hello", L"Bonjour"};
  for (int i = 0; i < 2; i++) {
    for (int j = 0; strs[i][j] != L'\0'; j++) {
      wprintf(L"%lc", strs[i][j]);
    }
    wprintf(L"\n");
  }
  return 0;
}
```

**22. Using Wide Strings in Dynamically Allocated Multi-Dimensional Arrays**

```c
#include <stdio.h>
#include <wchar.h>
int main() {
  wchar_t ***strs = (wchar_t ***)malloc(2 * sizeof(wchar_t **));
  for (int i = 0; i < 2; i++) {
    strs[i] = (wchar_t **)malloc(10 * sizeof(wchar_t *));
    for (int j = 0; j < 10; j++) {
      strs[i][j] = (wchar_t *)malloc(6 * sizeof(wchar_t));
    }
  }
  wcscpy(strs[0][0], L"Hello");
  wcscpy(strs[0][1], L"Bonjour");
  wcscpy(strs[1][0], L"Hola");
  wcscpy(strs[1][1], L"Ciao");
  for (int i = 0; i < 2; i++) {
    for (int j = 0; strs[i][j] != NULL; j++) {
      wprintf(L"%ls\n", strs[i][j]);
    }
  }
  return 0;
}
```

**23. Using Wide Strings in Function Pointers**

```c
#include <stdio.h>
#include <wchar.h>
int compare_wide_strings(const wchar_t *str1, const wchar_t *str2) {
  return wcscmp(str1, str2);
}

int main() {
  wchar_t *str1 = L"Hello";
  wchar_t *str2 = L"Bonjour";
  int result = compare_wide_strings(str1, str2);
  printf("%d\n", result);  // Output: -1 (str1 is lexicographically less than str2)
  return 0;
}
```

**24. Using Wide Strings in Callback Functions**

```c
#include <stdio.h>
#include <wchar.h>
typedef void (*wide_string_processor)(wchar_t *);

void print_wide_string(wchar_t *str) {
  wprintf(L"%ls", str);
}

int main() {
  wchar_t *str = L"Hello world";
  wide_string_processor processor = print_wide_string;
  processor(str);
  return 0;
}
```

**25. Using Wide Strings in Threads**

```c
#include <stdio.h>
#include <wchar.h>
#include <pthread.h>

void *thread_function(void *arg) {
  wchar_t *str = (wchar_t *)arg;
  wprintf(L"%ls\n", str);
  return NULL;
}

int main() {
  wchar_t *str = L"Hello world";
  pthread_t thread;
  pthread_create(&thread, NULL, thread_function, (void *)str);
  pthread_join(thread, NULL);
  return 0;
}
```

**26. Using Wide Strings in Socket Programming**

```c
#include <stdio.h>
#include <wchar.h>
#include <sys/socket.h>
#include <netinet/in.h>

int main() {
  int sockfd = socket(AF_INET, SOCK_STREAM, 0);
  struct sockaddr_in servaddr;
  servaddr.sin_family = AF_INET;
  servaddr.sin_port = htons(8080);
  inet_pton(AF_INET, "127.0.0.1", &servaddr.sin_addr);
  connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
  wchar_t send_buffer[] = L"Hello server";
  wchar_t recv_buffer[1024];
  send(sockfd, send_buffer, wcslen(send_buffer) * sizeof(wchar_t), 0);
  int recv_length = recv(sockfd, recv_buffer, sizeof(recv_buffer), 0);
  recv_buffer[recv_length / sizeof(wchar_t)] = L'\0';
  wprintf(L"Received from server: %ls\n", recv_buffer);
  return 0;
}
```

**27. Using Wide Strings in Database Applications**

```c
#include <stdio.h>
#include <wchar.h>
#include <sqlite3.h>

int main() {
  sqlite3 *db;
  wchar_t *sql = L"CREATE TABLE test(name TEXT);";
  int rc = sqlite3_open("test.db", &db);
  if (rc == SQLITE_OK) {
    rc = sqlite3_exec(db, sql, NULL, NULL, NULL);
    if (rc == SQLITE_OK) {
      wprintf(L"Table created successfully.\n");
    } else {
      wprintf(L"Error creating table: %s\n", sqlite3_errmsg(db));
    }
    sqlite3_close(db);
  } else {
    wprintf(L"Error opening database: %s\n", sqlite3_errmsg(db));
  }
  return 0;
}
```

**28. Using Wide Strings in Character Manipulation**

```c
#include <stdio.h>
#include <wchar.h>
#include <locale.h>

int main() {
  setlocale(LC_CTYPE, "");
  wchar_t str[] = L"This is a test string";
  wprintf(L"Original string: %ls\n", str);
  wchar_t *ptr = wcschr(str, L's');
  *ptr = L'S';
  wprintf(L"String after replacing 's' with 'S': %ls\n", str);
  return 0;
}
```

**29. Using Wide Strings in Regular Expressions**

```c
#include <stdio.h>
#include <wchar.h>
#include <regex.h>

int main() {
  wchar_t *pattern = L"Hello";
  wchar_t *string = L"Hello world";
  int result = regexec(pattern, string, 0, NULL, 0);
  if (result == 0) {
    wprintf(L"Match found.\n");
  } else {
    wprintf(L"Match not found.\n");
  }
  return 0;
}
```

**30. Using Wide Strings in String Formatting**

```c
#include <stdio.h>
#include <wchar.h>

int main() {
  wchar_t name[] = L"John Doe";
  int age = 30;
  wprintf(L"Name: %ls, Age: %d\n", name, age);
  return 0;
}
```

**31. Using Wide Strings in Number Formatting**

```c
#include <stdio.h>
#include <wchar.h>

int main() {
  double value = 123.456;
  wprintf(L"Value: %.2lf\n", value);
  return 0;
}
```

**32. Using Wide Strings in I/O Formatting**

```c
#include <stdio.h>
#include <wchar.h>

int main() {
  FILE *file = fopen("test.txt", "w");
  fwprintf(file, L"This is a wide string written to a file.\n");
  fclose(file);
  return 0;
}
```

**33. Using Wide Strings in Error Handling**

```c
#include <stdio.h>
#include <wchar.h>

int main() {
  FILE *file = fopen("test.txt", "r");
  if (file == NULL) {
    wprintf(L"Error opening file: %ls\n", L"test.txt");
    return 1;
  }
  // ...
  return 0;
}
```

**34. Using Wide Strings in Dynamic Library Loading**

```c
#include <stdio.h>
#include <wchar.h>
#include <dlfcn.h>

int main() {
  void *handle = dlopen(L"test.dll", RTLD_LAZY);
  if (handle == NULL) {
    wprintf(L"Error loading library: %ls\n", dlerror());
    return 1;
  }
  // ...
  return 0;
}
```

**35. Using Wide Strings in Memory Allocation**

```c
#include <stdio.h>
#include <wchar.h>
#include <stdlib.h>

int main() {
  wchar_t *ptr = (wchar_t *)malloc(sizeof(wchar_t) * 100);
  if (ptr == NULL) {
    wprintf(L"Error allocating memory.\n");
    return 1;
  }
  // ...
  return 0;
}
```

**36. Using Wide Strings in File System Operations**

```c
#include <stdio.h>
#include <wchar.h>
#include <direct.h>

int main() {
  if (_wmkdir(L"test_dir") == 0) {
    wprintf(L"Directory created successfully.\n");
  } else {
    wprintf(L"Error creating directory.\n");
  }
  return 0;
}
```

**37. Using Wide Strings in Command-Line Arguments Parsing**

```c
#include <stdio.h>
#include <wchar.h>

int main(int argc, wchar_t **argv) {
  for (int i = 0; i < argc; i++) {
    wprintf(L"Argument %d: %ls\n", i, argv[i]);
  }
  return 0;
}
```

**38. Using Wide Strings in Resource Files**

```c
#include <stdio.h>
#include <wchar.h>
#include <windows.h>

int main() {
  HRSRC hRes = FindResource(NULL, MAKEINTRESOURCE(42), RT_STRING);
  if (hRes == NULL) {
    wprintf(L"Error loading resource.\n");
    return 1;
  }
  HGLOBAL hResData = LoadResource(NULL, hRes);
  if (hResData == NULL) {
    wprintf(L"Error loading resource data.\n");
    return 1;
  }
  wchar_t *ptr = (wchar_t *)LockResource(hResData);
  if (ptr == NULL) {
    wprintf(L"Error locking resource data.\n");
    return 1;
  }
  wprintf(L"Resource: %ls\n", ptr);
  UnlockResource(hResData);
  FreeResource(hResData);
  return 0;
}
```

**39. Using Wide Strings in OLE Automation**

```c
#include <stdio.h>
#include <wchar.h>
#include <oleauto.h>

int main() {
  BSTR bstr = SysAllocString(L"Hello world");
  wprintf(L"BSTR: %ls\n", bstr);
  SysFreeString(bstr);
  return 0;
}
```

**40. Using Wide Strings in COM Interop**

```c
#include <stdio.h>
#include <wchar.h>
#include <comdef.h>

int main() {
  wchar_t *str = L"Hello world";
  BSTR bstr = SysAllocString(str);
  wprintf(L"BSTR: %ls\n", bstr);
  SysFreeString(bstr);
  return 0;
}
```

**41. Using Wide Strings in Web Development**

```c
#include <stdio.h>
#include <wchar.h>
#include <curl/curl.h>

int main() {
  CURL *curl = curl_easy_init();
  if (curl) {
    curl_easy_setopt(curl, CURLOPT_URL, L"https://example.com");
    char *response;
    size_t response_size;
    curl_easy_perform(curl);
    curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_size);
    curl_easy_cleanup(curl);
  }
  return 0;
}
```

**42. Using Wide Strings in XML Processing**

```c
#include <stdio.h>
#include <wchar.h>
#include <libxml/parser.h>

int main() {
  xmlDoc *doc = xmlParseFile(L"test.xml");
  if (doc) {
    xmlNode *node = xmlDocGetRootElement(doc);
    wprintf(L"Root node: %ls\n", node->name);
    xmlFreeDoc(doc);
  }
  return 0;
}
```

**43. Using Wide Strings in JSON Processing**

```c
#include <stdio.h>
#include <wchar.h>
#include <rapidjson/document.h>

int main() {
  wchar_t *json = L"{ \"name\": \"John Doe\", \"age\": 30 }";
  rapidjson::Document doc;
  doc.Parse(json);
  wprintf(L"Name: %ls, Age: %d\n", doc["name"].GetString(), doc["age"].GetInt());
  return 0;
}
```

**44. Using Wide Strings in Data Compression**

```c
#include <stdio.h>
#include <wchar.h>
#include <zlib.h>

int main() {
  wchar_t *str = L"Hello world";
  unsigned char *compressed_buffer = (unsigned char *)malloc(sizeof(unsigned char) * 100);
  unsigned long compressed_size = compress(compressed_buffer, 100, (unsigned char *)str, wcslen(str) * sizeof(wchar_t));
  unsigned char *decompressed_buffer = (unsigned char *)malloc(sizeof(unsigned char) * 100);
  unsigned long decompressed_size = uncompress(decompressed_buffer, 100, compressed_buffer, compressed_size);
  wprintf(L"Decompressed string: %ls\n", (wchar_t *)decompressed_buffer);
  free(compressed_buffer);
  free(decompressed_buffer);
  return 0;
}
```

**45. Using Wide Strings in Encryption**

```c
#include <stdio.h>
#include <wchar.h>
#include <openssl/evp.h>

int main() {
  wchar_t *plaintext = L"Hello world";
  wchar_t *ciphertext = (wchar_t *)malloc(sizeof(wchar_t) * 100);
  unsigned char *key = (unsigned char *)"1234567890123456";
  int ciphertext_length = EVP_EncryptInit(NULL, EVP_aes_128_cbc(), key, NULL);
  EVP_EncryptUpdate(NULL, (unsigned char *)ciphertext, &ciphertext_length, (unsigned char *)plaintext, wcslen(plaintext) * sizeof(wchar_t));
  EVP_EncryptFinal(NULL, (unsigned char *)ciphertext + ciphertext_length, &ciphertext_length);
  wprintf(L"Ciphertext: %ls\n", ciphertext);
  free(ciphertext);
  return 0;
}
```

**46. Using Wide Strings in Graphics**

```c
#include <stdio.h>
#include <wchar.h>
#include <gdiplus.h>

int main() {
  GdiplusStartupInput input;
  ULONG_PTR token;
  GdiplusStartup(&token, &input, NULL);
  HDC hdc = GetDC(NULL);
  wchar_t *text = L"Hello world";
  Gdiplus::Graphics graphics(hdc);
  Gdiplus::Font font(L"Arial", 20);
  Gdiplus::Brush brush(Gdiplus::Color::Black);
  graphics.DrawString(text, wcslen(text), &font, Gdiplus::PointF(10, 10), &brush);
  ReleaseDC(NULL, hdc);
  GdiplusShutdown(token);
  return 0;
}
```

**47. Using Wide Strings in Multimedia**

```c
#include <stdio.h>
#include <wchar.h>
#include <mmdevapi.h>

int main() {
  wchar_t *device_name = L"Realtek High Definition Audio";
  MMDeviceEnumerator *enumerator;
  MMDeviceCollection *devices;
  MMDevice *device;
  enumerator = new MMDeviceEnumerator;
  devices = enumerator->EnumAudioEndpoints(eRender, DEVICE_STATE_ACTIVE);
  device = devices->GetDevice(device_name);
  wprintf(L"Device name: %ls\n", device->GetId());
  return 0;
}
```

**48. Using Wide Strings in Networking**

```c
#include <stdio.h>
#include <wchar.h>
#include <winsock2.h>

int main() {
  WSADATA wsaData;
  SOCKET sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  struct sockaddr_in servaddr;
  servaddr.sin_family = AF_INET;
  servaddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
  servaddr.sin_port = htons(8080);
  connect(sock, (struct sockaddr *)&servaddr, sizeof(servaddr));
  wchar_t *hello_world = L"Hello world";
  send(sock, hello_world, wcslen(hello_world) * sizeof(wchar_t), 0);
  closesocket(sock);
  return 0;
}
```

**49. Using Wide Strings in File I/O**

```c
#include <stdio.h>
#include <wchar.h>
#include <windows.h>

int main() {
  wchar_t *filename = L"test.txt";
  HANDLE file = CreateFile(filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  if (file != INVALID_HANDLE_VALUE) {
    wchar_t *data = L"Hello world";
    DWORD bytes_written;
    WriteFile(file, data, wcslen(data) * sizeof(wchar_t), &bytes_written, NULL);
    CloseHandle(file);
  }
  return 0;
}
```

**50. Using Wide Strings in Unicode Support**

```c
#include <stdio.h>
#include <wchar.h>

int main() {
  wchar_t euro = L'\u20AC';
  wprintf(L"Euro symbol: %lc\n", euro);
  return 0;
}
```
