Values in C

C has various value types including strings, integers, floats, booleans, etc. Here are a few basic examples.

#include <stdio.h>
#include <stdbool.h>

int main() {
    // Strings in C are character arrays
    // We can't directly add strings with +, so we use puts() to print them
    printf("C");
    printf("lang\n");

    // Integers and floats
    printf("1+1 = %d\n", 1 + 1);
    printf("7.0/3.0 = %f\n", 7.0 / 3.0);

    // Booleans in C are represented by integers
    // 0 is false, any non-zero value is true
    printf("%d\n", 1 && 0);  // Equivalent to true && false
    printf("%d\n", 1 || 0);  // Equivalent to true || false
    printf("%d\n", !1);      // Equivalent to !true

    return 0;
}

To compile and run this C program:

$ gcc values.c -o values
$ ./values
Clang
1+1 = 2
7.0/3.0 = 2.333333
0
1
0

In this C version:

  1. We use printf instead of fmt.Println for output.
  2. Strings in C are character arrays, so we can’t directly concatenate them with +. Instead, we print them separately.
  3. C doesn’t have a built-in boolean type. We use integers (0 for false, non-zero for true) and include <stdbool.h> for boolean operations.
  4. The %d format specifier is used for integers, and %f for floats in printf.
  5. In C, the main function typically returns an integer, so we include return 0; at the end.

This example demonstrates basic value types and operations in C, including string output, integer and float arithmetic, and logical operations.