Learn C language in 2 days ——- 11. GUI programming and use of graphics libraries

11. GUI programming and use of graphics libraries (expansion, direction of subsequent learning)

1. Graphics library used in c language

In addition to the SDL and GTK+ mentioned above, there are other graphics libraries suitable for C language to choose from, such as:

1. **OpenGL**: OpenGL is a cross-platform graphics rendering API that can be used to create high-performance 2D and 3D graphics applications. Although it is a low-level graphics library, it can be used to create GUI interfaces with other libraries (such as GLUT and GLFW).

2. **Allegro**: Allegro is an easy-to-use game development library that can also be used for GUI programming. It provides functions for handling graphics, sound, input devices, etc., and is suitable for creating 2D games and graphics applications.

3. **Ncurses**: Ncurses is a library for creating text user interfaces on character terminals. It can be used to create terminal applications with menus, dialog boxes, text boxes, etc., and interact with them through keyboard control.

4. **FLTK**: FLTK (Fast Light Toolkit) is a cross-platform C++ GUI development library, but also provides C language bindings. It provides a set of lightweight GUI components that can be used to create simple windows, buttons, text boxes and other interface elements.

5. **SFML (Simple and Fast Multimedia Library)**: SFML is a cross-platform multimedia library that can be used to create 2D games and multimedia applications. It provides graphics drawing, window management, audio processing, input processing and other functions, and can facilitate GUI programming.

6. **Raylib**: Raylib is a simple and easy-to-use 2D game development library, which can also be used to create applications based on graphical interfaces. It is lightweight, cross-platform and high-performance, and provides functions such as graphics processing, input, and window management.

7. **FreeGLUT**: FreeGLUT is an open source window toolkit for OpenGL. It provides functions such as creating windows and processing input events. Although mainly used for OpenGL programming, it can also be combined with other functions and libraries for GUI development.

8. **IUP**: IUP is a cross-platform interface toolkit for creating simple interface applications. It provides functions for creating interface elements such as buttons, text boxes, and dialog boxes, and has event handling and layout management functions.

9. **GTKMM**: GTKMM is the official binding library for GTK+ for C++, but it also provides a C language interface. It allows you to write GTK+ applications in C and provides a set of classes and functions for GUI development.

10. **Qt**: Qt is a popular cross-platform GUI development framework, mainly designed for C++ developers. However, Qt also provides an interface for the C language called Qt for Embedded Linux (also known as Qt for Embedded Systems). Using Qt, you can create feature-rich graphical interface applications.

11. **EFL (Enlightenment Foundation Libraries)**: Enlightenment Foundation Libraries is a collection of powerful graphics libraries for building rich and dynamic user interfaces. It provides many GUI components and tools, suitable for creating windows, buttons, menus and other interface elements.

These libraries have different characteristics and scope of application. Please choose the appropriate library according to your needs for GUI programming in C language. Make sure to prepare the installation and configuration requirements before using these libraries, and refer to the official documentation and sample code for more detailed usage guides and examples.

2. Graphics library used by Gui programming in other languages

GUI programming (Graphical User Interface Programming) refers to a method of developing applications using a graphical interface. Through GUI programming, you can create a user-friendly interface that allows users to interact with applications through mouse, keyboard, etc.

In different programming languages, various graphics libraries are available, which provide functions and methods for creating GUI components such as windows, buttons, text boxes, images, etc.

The following are several commonly used GUI programming and graphics libraries:

1. **Java Swing**: Java provides Swing as its GUI programming toolkit. Swing has a rich component library that can create various interface elements and achieve user interaction through event processing.

2. **C# Windows Forms**: Windows Forms is a GUI programming framework for the .NET platform and is used to develop Windows applications. It provides a large number of visual controls to achieve user interaction in an event-driven manner.

3. **Python Tkinter**: Tkinter is a GUI programming toolkit in the Python standard library, which is based on the Tk graphics library. Tkinter is simple and easy to use. It can create interface elements such as windows, buttons, and labels, and implement interaction through event processing.

4. **C++ Qt**: Qt is a cross-platform C++ application development framework, which provides rich GUI programming tools and supports multiple operating systems. Qt has good customizability and high performance.

These graphics libraries not only provide functions and methods for creating GUI components, but also provide functions such as event handling, layout management, drawing, and multi-threading to meet the needs of various applications.

GUI programming using graphics libraries generally follows the following steps:

1. Import the relevant modules of the graphics library.

2. Create windows and other GUI components, set their properties and layout.

3. Register event processing functions to handle events generated by user interaction.

4. Start the main loop, wait for the event to occur and execute the corresponding event processing function.

5. Release resources and close the window.

The specific usage methods of different graphics libraries and programming languages are different. You can refer to the official documentation and sample code of the relevant graphics library to learn and use it.

12. Standard library functions (classification and summary)

In C language, the standard library provides many functions and components that can be used in many aspects of programming. The following are some commonly used standard library functions and their functional classification:

1. Input and output functions

  • – `printf`: Format output data to the standard output device
  • – `scanf`: Read formatted input data from the standard input device
  • – `fprintf`: Format output data to file
  • – `fscanf`: Read formatted input data from a file
  • – `getchar`: Read a character from the standard input device
  • – `putchar`: Output a character to the standard output device

2. String processing function

  • – `strcpy`: copy string
  • – `strcat`: concatenate strings
  • – `strlen`: Get the string length
  • – `strcmp`: compare two strings
  • – `strstr`: Find substrings in strings
  • – `strchr`: Find characters in a string
  • – `isalpha`: Check whether the character is a letter
  • – `isdigit`: Check whether the character is a digit
  • – `toupper`: Convert characters to uppercase
  • – `tolower`: Convert characters to lowercase
  • – `isupper`: Check whether the character is an uppercase letter
  • – `islower`: Check whether the character is lowercase letter

3. Mathematical functions

  • abs: take the absolute value
  • sqrt: Find the square root
  • pow: exponentiation operation
  • `sin`, `cos`, `tan`: trigonometric functions
  • `floor`, `ceil`: round down and round up
  • `exp`, `log`: exponential and logarithmic functions
  • fabs: take the absolute value function

4. Dynamic memory management function

  • – `malloc`: allocate memory
  • – `calloc`: allocate and clear memory
  • – `realloc`: reallocate memory
  • – `free`: release memory
  • – `memcpy`: Copies a specified length of bytes from the source memory area to the target memory area
  • – `memset`: Set the target memory area to a specific value
  • – `memcmp`: compare the contents of two memory areas

5. Time and date functions

  • – `time`: Get the UNIX timestamp of the current time
  • – `ctime`: Convert UNIX timestamp to local time string
  • – `strftime`: Format the local time into the specified string form
  • – `gmtime`: Convert UNIX timestamp to GMT time structure
  • – `difftime`: Calculate time difference
  • – `mktime`: Convert time to seconds

6. File operation functions

  • – `fopen`: open a file
  • – `fclose`: close the file
  • – `fread`: read data from a file
  • – `fwrite`: Write data to a file
  • – `feof`: Check the end-of-file flag
  • – `fprintf`: Write formatted data to a file

7. Type conversion function

  • – `atoi`: Convert string to integer
  • – `atof`: Convert string to floating point number
  • – `itoa`: Convert integer to string
  • – `sprintf`: Output formatted data into a string

8. Assertion function

  • – `assert`: Insert assertions in the code to check for logical errors

9. Random number function

  • – `rand`: generate pseudo-random numbers
  • – `srand`: Set the seed of random numbers

10. Command line parameter function

  • – `argc`: the number of command line arguments
  • – `argv`: String array of command line arguments

These functions are part of the C standard library. In addition to standard library functions, C language also supports operating system-related system call functions, mathematical library functions, and custom functions. You can consult the official documentation or related resources of C language according to your own needs to obtain more instructions and examples for the use of standard library functions and other related functions.

5. Formatted input and output and file format processing

Formatted I/O is a common operation that can be used to read and write data in a specific format. The C language provides functions in the standard library for formatted input and output, mainly through the `printf` and `scanf` functions for formatting output and input.

1.printf function

The `printf` function is used to output data in a specified format to standard output (screen) or a specified file. It can convert data into corresponding strings for output according to the format control characters in the format string. Some commonly used format control characters include:

  • – `%d`: decimal integer
  • – `%f`: floating point number
  • – `%s`: string
  • – `%c`: character
  • – `%x`: hexadecimal integer
  • – `%o`: octal integer

int num = 10;

printf(“%d\\
“, num);//The integer num can be output to the screen in decimal form.

2. scanf function

The `scanf` function is used to read data in a specified format from standard input (keyboard) or a file and store it in a variable. Similar to `printf`, `scanf` also uses format control characters to specify the type of data to be read. Some commonly used format control characters include:

  • – `%d`: decimal integer—–corresponds to int type
  • – `%f`: floating point number—–corresponds to float type
  • – `%s`: string—–corresponds to string type
  • – `%c`: character —– corresponds to char type

For example:

scanf(“%d”, & amp;num);//You can read an integer from the keyboard and store it in the variable `num`.

3. File format processing

File operation functions in C language can be used to read and write different file formats. Common file formats include text files and binary files.

– **Text file processing**: Text files store data in the form of plain text and can be read and written using the file operation functions in the standard library, such as `fopen`, `fclose`, `fprintf` and `fscanf`. You can use the `fprintf` function to write data to a text file in a specified format, and use the `fscanf` function to read data from a text file in a specified format.

– **Binary file processing**: Binary files store data in binary form and can be read and written using the `fread` and `fwrite` functions. The `fread` function is used to read data from a binary file by a specified number of bytes, and the `fwrite` function is used to write data into a binary file by a specified number of bytes.

These are some common functions used in C language for formatted input and output and file format processing. You can use appropriate functions to implement formatted reading and writing of data and file format processing according to specific needs. At the same time, please handle file opening and closing, exception handling and other operations according to the actual situation to ensure the correctness and robustness of the program.

11. Advanced use of math library functions

Math library functions provide various basic and advanced mathematical operation functions in C language. Here are some common math library functions and their advanced uses:

1. Trigonometric functions

  • – `sin`, `cos`, `tan`: Calculate the sine, cosine and tangent of a given angle
  • – `asin`, `acos`, `atan`: calculate arcsine, arccosine and arctangent of a given value
  • – `sinf`, `cosf`, `tanf`: single precision floating point versions of trigonometric functions
  • – `sinh`, `cosh`, `tanh`: calculate hyperbolic sine, hyperbolic cosine and hyperbolic tangent values

2. Exponential and logarithmic functions

  • – `exp`, `log`: Calculate the exponential and logarithmic value of a given value
  • – `pow`: Calculate the power value of the given base and exponent
  • – `sqrt`: Calculates the square root of a given value
  • – `cbrt`: Compute the cube root of a given value
  • – `log10`: Calculates the base 10 logarithm of a given value

3. Rounding function

  • – `floor`, `ceil`: functions of rounding down and rounding up
  • – `round`: rounding
  • – `trunc`: truncate the decimal part and round it up

4. Other functions

  • `fabs`: Calculates the absolute value of a given value
  • – `fmod`: Computes the remainder of the division of floating point numbers between two given values.
  • – `isnan`, `isinf`: Check whether the given value is NaN (not a number) or infinity

When using these math library functions, you need to include the C language math library header file “. When writing code, you can select the appropriate mathematical function according to specific needs and pass in the corresponding parameters to call it. When dealing with floating point numbers, if you need to use a mathematical function with single-precision floating point numbers (float type), you need to add the suffix “f” after the function name.

Note that the use of mathematical library functions needs to follow the corresponding mathematical rules and value ranges. Be sure to perform appropriate checks and verifications when using them to ensure the correctness and robustness of the program.

7. Application of time and date processing functions

Time and date processing functions are often used in programs to obtain, process, and format time and date information. C language provides some standard library functions to support time and date operations. The following are some commonly used time and date processing functions and their applications:

1. time function

The `time` function is used to obtain the current system time and return the number of seconds from 00:00:00 UTC on January 1, 1970 to the current time. Its prototype is as follows:

time_t time(time_t *timer);

Application examples:

time_t currentTime;

time(¤tTime);

printf(“Current time: %s”, ctime(¤tTime));

2.gmtime function and localtime function

The `gmtime` function and the `localtime` function are used to convert a timestamp into a time represented by a structure, and to convert a time represented by a structure into a timestamp. Among them, the `gmtime` function converts the timestamp to Coordinated Universal Time (UTC), while the `localtime` function converts the timestamp to local time. The prototypes of these two functions are as follows:

struct tm *gmtime(const time_t *timer);

struct tm *localtime(const time_t *timer);

Application examples:

time_t currentTime;
   time(& amp;currentTime);
   struct tm *timeinfo = localtime( & amp;currentTime);
   printf("Current time: d: d: d", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);

3. strftime function

The strftime` function is used to convert a time structure into a formatted string. By providing a specified format string, time information can be output in a custom format. Its prototype is as follows:

size_t strftime(char *str, size_t maxsize, const char *format, const struct tm *timeptr);

Application examples:

 time_t currentTime;
   time(& amp;currentTime);
   struct tm *timeinfo = localtime( & amp;currentTime);
   char buffer[80];
   strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", timeinfo);
   printf("Current time: %s", buffer);

4.difftime function

The `difftime` function is used to calculate the difference between two times, in seconds. Its prototype is as follows:

 double difftime(time_t time1, time_t time0);

Application examples:

 time_t currentTime;
   time(& amp;currentTime);
   time_t pastTime = ...; // past time
   double seconds = difftime(currentTime, pastTime);
   printf("Two time intervals: %f seconds", seconds);

These functions provide basic time and date processing functions. You can choose the appropriate function for application based on specific needs and scenarios. There are other more advanced time and date processing functions, you can get more details by consulting the official documentation of the C language or related resources.