Example 1: VariablesDemo.c
Demonstrate Data Types: int, char, float

/*
VariablesDemo.c
Demonstrate Data Types: int, char, float
*/
#include <stdio.h>

int main(void)
{
char x = 'X';
char bell = '\a';
char sn = '\xA5';
char accent = '\xA0';
int x1 = 21;
int x2 = 021;
int x3 = 0x21;
printf("\nExample: %c", x);
//printf("\nA Bell %c", bell);
printf("\nExample: %c", sn);
printf("\nExample: %c", accent);
printf("\n%c %c %c", 'A', 'B', 'C');
printf("\nI am Peter, I am %d years old and my weight is %f kg", 20, 48.5);
printf("\nbase 10: %d, Octal: %d, Hexadecimal: %d", x1, x2, x3);
printf("\nx1=%d, x2(octal)=%o, x3(hexadecimal)=%x", x1, x2, x3);
printf("\n%d %d %d", 456, 456, 456);
printf("\n%5d %5d %5d", 456, 456, 456);
printf("\n%-5d %-5d %-5d", 456, 456, 456);
printf("\n%f", 134.568767);
printf("\n%6.2f", 134.568767);

printf("\n123456789123456789");
printf("\n%+8.2f,%+8.2f", 12.345,12.345);
printf("\n%-8.2f,%-8.2f", 12.345,12.345);
return 0;
}

Output:

  • In C, char values are stored in 1 byte, and are encoded as numbers using the ASCII encoding.
    Sometimes, there are some characters that are not represented by normal forms, such as new line characters. These can be coded by escape characters, such as \x, \n, \t... More on escape characters see 04 examples page.
  • int x1 = 21; this is in base 8, x1 = 21 in base 10
    int x2 = 021; this is in base 8, hence, x2 = 17 in base 10
    int x3 = 0x21; this is in base 16, x3 = 33 in base 10
  • printf("\nbase 10: %d, Octal: %d, Hexadecimal: %d", x1, x2, x3); print 3 varibles all in base 10; %d = decimal = base 10
  • printf("\nx1=%d, x2(octal)=%o, x3(hexadecimal)=%x", x1, x2, x3); print 3 varibles all in 3 different bases 10, 8 then 16;
    %d = decimal = base 10
    %o = octal = base 8
    %x = hex = base 16
  • %5d: print decimal number with minimum 5 spaces per number including empty spaces, so number 234 will be shown as __234 but number 123456 still shows as 123456
  • 6.2f: print float number with minimum 6 spaces per number including empty spaces and the dot, so number 34.1123 will be shown as _34.11 but number 1123.456 still shows as 1123.45
  • +8.2f, -8.2f
    (-): Left-justify within the given field width; Right justification is the default (see width sub-specifier).
    (+): Forces to precede the result with a plus or minus sign (+ or -) even for positive numbers. By default, only negative numbers are preceded with a - sign.

Example 2: DataTypes.c
Demonstrate on Demonstrate sizeof


/*
DataTypes.c
Demonstrate sizeof
*/
#include <stdio.h>
#include <limits.h>

int main(void)
{
printf("\nCharacter Types\n");
printf("Size of character types is %d byte\n",(int)sizeof(char));
printf("Signed char min: %d max: %d\n", SCHAR_MIN, SCHAR_MAX);
printf("Unsigned char min: 0 max: %u\n",(unsigned int)UCHAR_MAX);

printf("\nShort Int Types\n");
printf("Size of short int types is %d bytes\n",(int)sizeof(short int));
printf("Signed short min: %d max: %d\n",SHRT_MIN, SHRT_MAX);
printf("Unsigned short min: 0 max: %u\n",(unsigned int)USHRT_MAX);

printf("\nInt Types\n");
printf("Size of int types is %d bytes\n",(int)sizeof(int));
printf("Signed int min: %d max: %d\n",INT_MIN, INT_MAX);
printf("Unsigned int min: 0 max: %u\n",(unsigned int)UINT_MAX);

printf("\nLong Int Types\n");
printf("Size of long int types is %d bytes\n",(int)sizeof(long int));
printf("Signed long min: %ld max: %ld\n",LONG_MIN, LONG_MAX);
printf("Unsigned long min: 0 max: %lu\n",ULONG_MAX);

printf("\nSize of long int types is %d bytes\n",(int)sizeof(float));
printf("Size of long int types is %d bytes\n",(int)sizeof(double));
printf("Size of long int types is %d bytes\n",(int)sizeof(long double));
return 0;
}

Output:

  • sizeof(char): give you size of any type in number of byte, so sizeof(char) = 1 byte = 8 bits, sizeof(int) = 4 bytes = 32 bits.
  • SCHAR_MIN, SCHAR_MAX: min and max value of a type, which are constants.
    click here to see more details on those limits.
  • (unsigned int): turn a sign value in to an unsign value

Example 3: BooleanExample.c
This program demonstrate Boolean type.

/*
BooleanExample.c
Demonstrate Boolean
*/
#include <stdio.h>

#define FALSE 0
#define TRUE 1

int main(void)
{
int my_variable = 1;
if (my_variable) {
printf("True!\n");
} else {
printf("False!\n");
}

if (my_variable != 0) {
printf("True!\n");
} else {
printf("False!\n");
}

if (my_variable == TRUE) {
printf("True!\n");
} else {
printf("False!\n");
}
return 0;
}

Output:

  • In C, true and false are coded to be 1 and 0, what if you change my_variable = 15?
    If a number is different from 0 then it's equivalent to true, so (13-2) === true, (13-13) === false
    if(5){ printf('true'); always print true out.
  • Within if, we can use >, >=, <, <=, ==

Example 4: operatorExample.c
This program demonstrate operators in C.

/*
operatorExample.c
Demonstrate operators in C
*/
#include <stdio.h>
int main()
{
int int1 = 7, int2 = 15, int3=1;
float float1 = 3, float2=1;
char cv;
int a=10, b=5;
int i=2, j=9;

//arithmetic
printf("Value of int2 mod int1 = %d\n", int2%int1);
printf("Value of int2 div int1 = %d\n", int2/int1);

//increment/decrement
printf("Value of int1 = %d\n", ++int1);
int1 = 7;
printf("Value of int1 = %d\n", int1++);

//assignment
int3 += int2;
printf("Value of int3 = %d\n", int3);

float2 *= 2;
printf("Value of float2 = %f\n", float2);

/* Lost precision: char -> int */
int1 = 321;
cv = int1;
printf("Integer assigned to character: %d -> %d %c \n", int1, cv, cv);

/* Show loss of fractional component when numbers are involved in integer-only */
int3 = int1/50;
printf("Value of int1/5 = %d\n", int3);

float2 = int1/50;
printf("Value of int1/5 = %f\n", float2);

float2 = int1/50.0;
printf("Value of int1/5 = %f\n", float2);

return 0;
}

 

Output:

  • int2%int1: Modulo (remainder)
  • int2/int1: Division (quotient)
  • ++int1 and int1++: what is the different?
    ++int1: add 1 first then do the printing
    int1++: do the printing first then do the addition by 1
  • int3 += int2; same as int3 = int3 + int2;
  • float2 *= 2; same as float2 = float2 * 2;

Example 5: operatorExample.c
This program demonstrate operators in C.

/*
operatorExample.c
Demonstrate operators in C
*/
#include <stdio.h>
int main()
{
int a=10, b=5;
int i=2, j=9;
int bits1 = '\x2A';
int bits2 = '\x0F';
unsigned int x = ~0;
int y = ~0;

/* relational */
printf("a > b----> %d > %d (1=true, 0=false) = %d\n", a, b, a > b);
printf("a>0?a:b---->returns %d\n", a, b, a>0?a:b);

/* logical */
printf("a>b && i<j = %d\n", (a>b && i<j));
printf("a<b || i>j = %d\n", (a<b || i>j));
printf("a>b ^ i<j = %d\n", (a>b ^ i<j));

/* Bitwise */
printf("(bits1 & bits2)=%d\n", (bits1 & bits2));
printf("(bits1 | bits2)=%d\n", (bits1 | bits2));
printf("(bits1 ^ bits2)=%d\n", (bits1 ^ bits2));
printf("(~bits2)=%d (%x)\n", (~ bits2), (~ bits2));

/* Shifting */
printf("bits1 << 2=%d\n",( bits1 << 2));
printf("bits1 >> 2=%d\n",( bits1 >> 2));

//right shift
printf("%x\n", (x));
printf("x>>10=%d\n", (x >> 10));
printf("%y\n", (y));
printf("y>>10=%d\n", (y >> 10));

/* sizeof */
printf("Size of character types is %d byte\n",(int)sizeof(char));
printf("Size of int types is %d bytes\n",(int)sizeof(int));

return 0;
}

 

Output:

  • int bits1 = '\x2A';
    int bits2 = '\x0F';
    Declare variables by entering hexadecimal values.
  • unsigned int x = ~0; similar to NOT in bitwise functions, so declare x = 0 then flip all bits of x so in hex x = ffffffff
  • Try to figure out yourselves why the print out is like that by looking up all other C operators, click here.