Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize generic programming in C language

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Today Xiaobian to share with you how to achieve generic programming in the C language related knowledge points, detailed content, logic clear, I believe most people are still too familiar with this knowledge, so share this article for your reference, I hope you read this article after some gains, let's take a look at it.

Generic programming is a style or paradigm of programming languages. Generics allow programmers to write code in a strongly typed programming language with types specified later, indicating these types as parameters at instantiation time. C++ supports generic programming, i.e. templates, such as:

Run Results:

1 + 2 = 3 1.2 + 2.3 = 3.5

As you can see from the results above, for calls to add, integer addition is performed if the input is an integer, and floating-point addition is performed if the input is a floating-point. That is, the add function does not target a specific type (generic).

You can do the same with overloading, but there is a lot of duplication.

Does C support generic programming?

Unfortunately, the C language itself does not support generic programming in the true sense, but it can "implement generic programming" to a certain extent.

_Generic Keyword

_Generic is a keyword of C11, by which a generic expression can be made:

_Generic((value). int:"int", float:"float",char*:"char*",default:"other type")

What do you mean? If value is of type int, then the value of the expression is "int", and so on. Does it look like a switch statement?

Based on this example, let's implement a function that prints exactly what type a variable or constant is:

Here, for convenience, we simplify generic expressions by defining keywords.

Run Results:

1 + 2 type: int 1/3 type: int 2/3 type: float xxx type: char*

You can see that you can get the result type of the expression through TYPE, which is really a boon for beginners.

generic algorithms

Since C has the_Generic keyword, let's try to implement the addition in the C++ example code at the beginning. After reading the example above, I believe you already know:

Looking at the code above, we note:

Here, we need to define two types of addition (actually, through C++ templates, by the compiler to do this for us), because C language does not support overloading, so the two addition function names are different.

Because there are two parameters involved, if the two parameters are inconsistent when making type judgment, there may still be compilation problems.

Callers do not need to distinguish what type of object is added, they can use ADD uniformly.

tgmath.h of C99

As mentioned earlier, the_Generic keyword is only available in C11, so what about C99? In fact, tgmath.h provides a generic type macro if float, double, and long double versions are defined in the function of math.h. The effect is the same as in the previous example, for example:

Compile Run Results:

2.000000 1.200000

However, it has to be said that the generic macros provided in tgmath are also limited.

void * pointer

As we all know, void * pointer in C language is a typeless pointer, from this point of view, it can also be regarded as generic pointer. Its use is very common in C, for example, the function declaration of the quicksort interface is like this:

#include void qsort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *));

The library function qsort is actually a generic sort algorithm that can sort any type of data. Of course, there is a premise that you need to implement a compar function to compare sizes according to its protocol.

The above is "C language how to achieve generic programming" all the content of this article, thank you for reading! I believe everyone has a great harvest after reading this article. Xiaobian will update different knowledge for everyone every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 203

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report