Home > Backend Development > C++ > body text

The function of void keyword in C language

PHPz
Release: 2024-02-19 23:33:06
Original
1214 people have browsed it

The function of void keyword in C language

void in C is a special keyword used to represent an empty type, which means data without a specific type. In C language, void is usually used in the following three aspects.

  1. The function return type is void
    In C language, functions can have different return types, such as int, float, char, etc. However, if the function does not return any value, the return type can be set to void. This means that after the function is executed, it does not return a specific value. For example:
void helloWorld() {
    printf("Hello, World!
");
}
Copy after login

In the above code, the return type of the helloWorld() function is void. It simply prints out the string "Hello, World!" without returning any value.

  1. The function parameter type is void
    In C language, functions can have parameters, and the parameters can have different types. However, sometimes we don't need the function to receive any parameters, in which case the parameter type can be set to void. For example:
void printMessage(void) {
    printf("This is a message.
");
}
Copy after login

In the above code, the printMessage() function does not receive any parameters, it simply prints out the string "This is a message."

  1. The pointer type is void
    In C language, we can declare various types of pointers, such as int pointers, float pointers, etc. The void pointer is a general pointer type that can point to any type of data, but it cannot directly access and operate the data pointed to. For example:
void* ptr;
int num = 10;
float f = 3.14;
char c = 'A';

ptr = # // void指针指向int类型的变量
ptr = &f;   // void指针指向float类型的变量
ptr = &c;   // void指针指向char类型的变量
Copy after login

In the above code, we declare a void type pointer ptr and let it point to the int type variable num, the float type variable f and the char type variable c.

It should be noted that since the void pointer cannot directly access and operate the data pointed to, when it is actually used, it usually needs to be converted to a specific type before operation can be performed. For example:

int* intPtr = (int*)ptr; // 将void指针转换为int指针
*intPtr = 20;            // 通过int指针修改所指向的int类型的变量
printf("%d
", *intPtr); // 输出20
Copy after login

In the above code, we convert the void pointer ptr into the int pointer intPtr, and modify the pointed int type variable through the int pointer.

To sum up, void in C is a keyword used to represent empty types. It is widely used in function return types, function parameter types and pointer types.

The above is the detailed content of The function of void keyword in C language. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!