C Language Data Types II

1. void Type void is a special type in C language, representing “no type” or “empty type”. The void type is mainly used in the following three situations:

  • Function return type is void: indicates that the function does not return any value.
  • Function parameter is void: indicates that the function does not accept any parameters.
  • void pointer: can point to data of any type, but cannot be dereferenced directly.

1) Function return type is void

#include <stdio.h>// Function with no return valuevoid printf_message() {    printf("This is no return function\n");}// Function with return value, and return type is intint add(int a, int b) {    int ret = 0;    ret = a + b;    return ret;}int main(int argc, char *argv[]) {    int val = 0;    printf_message();    val = add(2, 3);    printf("2 plus 3, the result is :%d\n", val);    return 0;}

Output:

This is no return function

2 plus 3, the result is :5

2) Function parameter is void In C language, there is a difference between using <span>void</span> for function parameters and an empty parameter list. In C language, using<span><span>void</span></span> in the function parameter list explicitly indicates no parameters, while an empty parameter list in the function declaration indicates unknown parameters. If a function does not require parameters, please explicitly use void

#include <stdio.h>// Explicitly declare that the function does not accept any parametersint get_value(void) {    return 42;}// Empty parameter list, can accept any parametersint get_value2() {    return 100;}int main(int argc, char *argv[]) {    int value = 0;    int value2 = 0;    int value3 = 0;    // Cannot pass any parameters when calling, otherwise compilation fails    value = get_value();    value2 = get_value2();    // Compilation will produce a warning, but still compiles to an executable    value3 = get_value2(20);    printf("value is :%d\n", value);    printf("value2 is :%d\n", value2);    printf("value3 is :%d\n", value3);    return 0;}

If line 18 is changed to value = get_value(2); it will produce a compilation error.

error: too many arguments to function call, expected 0, have 1

18 | value = get_value(2);

Line 22 only produces a compilation warning, but the program can still execute.

warning: too many arguments in call to ‘get_value2’

22 | value3 = get_value2(20);

Output:

value is :42

value2 is :100

value3 is :100

3) void pointer Can point to data of any type, further details will be explained when discussing pointers.2. Array Type Arrays are one of the most important composite data types in C language, used to store multiple elements of the same type. Array characteristics:

  • A collection of elements of the same data type
  • Stored contiguously in memory
  • Accessed via index
  • Index starts from 0
#include <stdio.h>int main(int argc, char *argv[]) {    int size = 0;    // Array definition    // Array of 5 integers, but values are not specified    int num_array[5];    // Array of 5 integers, with specified values    int num_array2[5] = {1, 2, 3, 4, 5};    // Number of integers in the array is inferred from the specified values, resulting in 3 integers    int num_array3[] = {10, 20, 30};    // Array of 5 integers, first two specified, others are random    // Depends on the values in that segment of memory, so data must be initialized properly    // To avoid inconsistent results on each execution, which would require a long time to analyze issues    int num_array4[5] = {100, 200};    // Accessing array elements, index starts from 0, indicating the first element    // The element at index 4 is the fifth element, which is the last element of num_array    num_array[0] = 1000;    num_array[4] = 5000;    printf("num array first value is %d\n", num_array[0]);    printf("num array2 second value is %d\n", num_array2[1]);        // Calculate array size, using the total size of the array divided by the size of one element    size = sizeof(num_array3) / sizeof(num_array3[0]);    printf("num_array3‘s size is %d\n", size);        printf("num array4 third value is %d\n", num_array[2]);        return 0;}

Output: You can see that the value of the third element of num_array4 is a random value.

num array first value is 1000

num array2 second value is 2

num_array3‘s size is 3

num array4 third value is 114812688

The above example is a one-dimensional array. A two-dimensional array is similar.For example

int matrix2[2][3]={

{1,2,3},

{4,5,6}

};

3. Structure Type Structures are a user-defined data type that allows combining multiple different types of data items into a single logical unit.

// Basic syntax for defining a structure

struct StructureName {

DataType Member1;

DataType Member2;

// …

DataType MemberN;

};

#include <stdio.h>#include <string.h>#define MAX_NAME_LENGTH 50struct Student {    char name[MAX_NAME_LENGTH];    int age;    int id;};int main(int argc, char *argv[]) {    // Different initialization methods    struct Student people;    struct Student people2 = {        .name = "student2",        .age = 15,        .id = 1002    };    struct Student people3 = {"student3", 17, 1003};    // strcpy is the string copy function    strcpy(people.name, "student1");    people.age = 16;    people.id = 1001;    // name is a string, use %s to output, age and id are integers, use %d    printf("first student name:%s, age:%d, id:%d\n", people.name,                                                     people.age,                                                     people.id);    printf("second student name:%s, age:%d, id:%d\n", people2.name,                                                      people2.age,                                                      people2.id);    printf("third student name:%s, age:%d, id:%d\n", people3.name,                                                     people3.age,                                                     people3.id);    return 0;}

Output:

first student name:student1, age:16, id:1001

second student name:student2, age:15, id:1002

third student name:student3, age:17, id:1003

Leave a Comment