C Programming Language Tutorial

Variables and Data Types

Input/Output

Looping and Selection Structures

Array

Functions

Preprocessing Command

Pointer

Structure

File Operations

Important Knowledge

Using English Characters in C Programming Language

We have mentioned strings many times before. A string is a collection of multiple characters surrounded by " ", such as "http://www.iditect.com" , "iditect" . The characters in the string are arranged in order and next to each other in the memory, and the entire string occupies a contiguous piece of memory.

Of course, the string can also contain only one character, such as "A" , "6" ; but for the convenience of operation, we generally use a special character type to deal with.

The character type that beginners often use is char, which has a length of 1 and can only accommodate characters in the ASCII code table, that is, English characters.

If you want to deal with characters other than English such as Chinese, Japanese, and Korean, you have to use other character types, which char cannot do.

representation of characters

Character types are surrounded by single quotes ' ' and strings are surrounded by double quotes " " .

The following example demonstrates how to assign a value to a variable of type char:
//correct spelling
char a = '1';
char b = '$';
char c = 'X';
char d = ' '; // space is also a character
// wrong spelling
char x = '그'; //char type cannot contain characters other than ASCII encoding
char y = 'A'; //A is a full-width character
char z = "t"; //Character type should be surrounded by single quotes
Note: In the character set, the numbers (or encoding values) corresponding to full-width characters and half-width characters are different, and they are two characters; ASCII encoding only defines half-width characters, not full-width characters.

character output

There are two ways to output characters of type char, namely:
  • Use the special character output function putchar;
  • Using the general formatted output function printf, the format control character corresponding to char is %c .

See the demo below:
#include <stdio.h>
int main() {
    char a = '1';
    char b = '$';
    char c = 'X';
    char d = ' ';
    // use putchar output
    putchar(a); putchar(d);
    putchar(b); putchar(d);
    putchar(c); putchar('\n');
    // use printf output
    printf("%c %c %c\n", a, b, c);
    return 0;
}
Running result:
1 $ X
1 $ X The

putchar function can only output one character at a time, and it needs to be called multiple times to output multiple characters.

characters and integers

We know that when a computer stores a character, it does not really store the character entity, but stores the number (also called the encoded value) of the character in the character set. For the char type, it actually stores the ASCII code of the character.

In any character set, a character number is an integer; from this perspective, there is essentially no difference between a character type and an integer type.

We can assign an integer to the character type, or output the character type as an integer. Conversely, you can assign a character to an integer type, or output an integer type as a character.

See the example below:
#include <stdio.h>
int main()
{
    char a = 'E';
    char b = 70;
    int c = 71;
    int d = 'H';
    printf("a: %c, %d\n", a, a);
    printf("b: %c, %d\n", b, b);
    printf("c: %c, %d\n", c, c);
    printf("d: %c, %d\n", d, d);
    return 0;
}
Output result:
a: E, 69
b: F, 70
c: G, 71
d: H, 72

In the ASCII code table, the numbers corresponding to the characters 'E', 'F', 'G', and 'H' are respectively 69, 70, 71, 72.

a, b, c, d actually store integers:
  • When assigning a character to a and d, the character will be converted into ASCII code before storage;
  • When assigning an integer to b and c, no conversion is required, and it can be stored directly;
  • When a, b, c, d are output with %c, the integer will be converted into the corresponding character according to the ASCII code table;
  • When outputting a, b, c, d with %d, no conversion is required, and direct output is fine.

It can be said that the ASCII code table associates English characters with integers.

Let's talk about strings again

Earlier we talked about the concept of strings and the output of strings, but we haven't talked about how to store a string in a variable. In fact, there is no special string type in the C programming language, we can only use arrays or pointers to store strings indirectly.

It is very contradictory to talk about strings here. Although we have not learned arrays and pointers for the time being, we cannot analyze them in depth, but strings are commonly used, and we have to say it again. So I won't explain too much in this section, you just need to memorize the following two representations:
char str1[] = "http://www.iditect.com";
char *str2 = "iditect";
str1 and str2 are the names of strings, and the following [ ] and the preceding * are fixed. Beginners can temporarily think that these two storage methods are equivalent, and they can be output through the dedicated puts function and the general printf function.

Full demo code:
#include <stdio.h>
int main()
{
    char web_url[] = "http://www.iditect.com";
    char *web_name = "iditect";
    puts(web_url);
    puts(web_name);
    printf("%s\n%s\n", web_url, web_name);
    return 0;
}
  1. ASCII encoding and representation of English characters in C:

    • Description: ASCII (American Standard Code for Information Interchange) represents English characters using integer values ranging from 0 to 127.
    • Example:
      #include <stdio.h>
      
      // ASCII encoding and representation of English characters in C
      int main() {
          char englishChar = 'A';
          printf("ASCII Value of %c: %d\n", englishChar, (int)englishChar);
      
          return 0;
      }
      
  2. Printing English characters using printf in C language:

    • Description: The %c format specifier in printf is used to print English characters.
    • Example:
      #include <stdio.h>
      
      // Printing English characters using printf in C
      int main() {
          char englishChar = 'B';
          printf("English Character: %c\n", englishChar);
      
          return 0;
      }
      
  3. Reading English characters from the keyboard in C:

    • Description: The scanf function with the %c specifier is used to read English characters from the keyboard.
    • Example:
      #include <stdio.h>
      
      // Reading English characters from the keyboard in C
      int main() {
          char userInput;
          printf("Enter an English character: ");
          scanf(" %c", &userInput);  // Note the space before %c to consume the newline
      
          printf("You entered: %c\n", userInput);
      
          return 0;
      }
      
  4. Handling English characters in string manipulation in C programming:

    • Description: Strings in C are arrays of characters, and various string manipulation functions can be used with English characters.
    • Example:
      #include <stdio.h>
      #include <string.h>
      
      // Handling English characters in string manipulation in C programming
      int main() {
          char name[20] = "John";
          printf("Length of name: %zu\n", strlen(name));
      
          return 0;
      }
      
  5. Comparing and sorting English characters in C:

    • Description: English characters can be compared and sorted using functions like strcmp and qsort.
    • Example:
      #include <stdio.h>
      #include <string.h>
      
      // Comparing and sorting English characters in C
      int main() {
          char str1[] = "apple";
          char str2[] = "banana";
      
          // Comparing strings
          if (strcmp(str1, str2) < 0) {
              printf("%s comes before %s\n", str1, str2);
          } else {
              printf("%s comes before %s\n", str2, str1);
          }
      
          return 0;
      }
      
  6. Working with English characters in character arrays in C:

    • Description: Character arrays can be used to store and manipulate sequences of English characters.
    • Example:
      #include <stdio.h>
      
      // Working with English characters in character arrays in C
      int main() {
          char greeting[] = "Hello, World!";
          printf("Greeting: %s\n", greeting);
      
          return 0;
      }
      
  7. File I/O operations with English characters in C language:

    • Description: English characters can be read from and written to files using file I/O functions.
    • Example:
      #include <stdio.h>
      
      // File I/O operations with English characters in C language
      int main() {
          FILE *file = fopen("example.txt", "w");
          if (file != NULL) {
              fprintf(file, "This is an English text.\n");
              fclose(file);
          }
      
          return 0;
      }
      
  8. Unicode and extended character sets in C programming:

    • Description: C supports Unicode and extended character sets, allowing representation of characters beyond the ASCII range.
    • Example:
      #include <stdio.h>
      
      // Unicode and extended character sets in C programming
      int main() {
          wchar_t unicodeChar = L'�';  // Unicode character
          printf("Unicode Character: %lc\n", unicodeChar);
      
          return 0;
      }