Quick actions

cmd+k|ctrl+k

Navigation

Languages

Comp120 example: Data Types and Values

Snippet info

Language

C

Visibility

public

Author

jfall

Created

2017-01-16T20:17:38Z

Updated

2017-01-16T20:21:50Z

/*
 * Experiments and demos for Data Types and Values
 *
 * Author: J. Fall
 * Date: Jan. 2017

**************************************************************************/

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

int main()
{  
    // Initialization
    int i = 42;
    char c = 'A';
    float f = 123.987;
    char* s = "Hello World";
    bool b = true;
    
    printf("Data values: %d, %c, %f, %s, %s \n",i, c, f, s, b?"true":"false");
    
    // type casting to char
    printf("char values: '%c', '%c', '%c', '%c', '%c' \n",(char)i, (char)c, (char)f, (char)s, (char)b);

    // type casting to int
    printf("int values: %i, %i, %i, %i, %i \n",(int)i, (int)c, (int)f, (int)s, (int)b);
    
    // type casting to bool
    printf("bool values: %s, %s, %s, %s, %s \n",(bool)i?"true":"false", (bool)c?"true":"false", (bool)f?"true":"false", (bool)s?"true":"false", (bool)b?"true":"false");

    return 0;
}
INFO