#include <stdio.h>
int main(void)
{
int* p;
enum vowel{ a, e, i, o, u, y } vow;
printf("_Bool : %zu bits\n", 8 * sizeof(_Bool));
printf("char : %zu bits\n", 8 * sizeof(char));
printf("unsigned char : %zu bits\n", 8 * sizeof(unsigned char));
printf("\n");
printf("short (int) : %zu bits\n", 8 * sizeof(short int));
printf("short : %zu bits\n", 8 * sizeof(short));
printf("unsigned short: %zu bits\n", 8 * sizeof(unsigned short));
printf("\n");
printf("int : %zu bits\n", 8 * sizeof(int));
printf("unsigned int : %zu bits\n", 8 * sizeof(unsigned int));
printf("\n");
printf("long : %zu bits\n", 8 * sizeof(long));
printf("unsigned long : %zu bits\n", 8 * sizeof(unsigned long));
printf("\n");
printf("float : %zu bits\n", 8 * sizeof(float));
printf("double : %zu bits\n", 8 * sizeof(double));
printf("long double : %zu bits\n", 8 * sizeof(long double));
printf("\n");
printf("enum : %zu bits\n", 8 * sizeof(vow));
printf("pointer : %zu bits\n", 8 * sizeof(p));
printf("\n");
return 0;
}
#include <stdio.h>
int main(void)
{
int a = 10;
int b = 5;
int temp;
// Before swapping variables values
printf("BEFORE swap\n");
printf("Variable a: %d \n", a); // Variable a: 10
printf("Variable b: %d \n", b); // Variable b: 5
/// Add here the instructions to SWAP the variables values
/// Hint: 2 instructions are executed sequentially (one after the other)
temp = a;
a = b;
b = temp;
// After swapping varialbes values
printf("\nAFTER swap\n");
printf("Variable a: %d \n", a); // Variable a: 5
printf("Variable b: %d \n", b); // Variable b: 10
return 0;
}
#include <stdio.h>
// Assumes little endian
void printBits(int size, void *ptr)
{
unsigned char *b = (unsigned char*)ptr;
for (int i = size - 1; i >= 0; --i)
{
for (int j = 7; j >= 0; --j)
{
// Take the i-th byte
unsigned char byte = b[i];
// Use a mask to keep the (j+1)-th bit.
// 10000000 to keep 8th bit (j == 7)
// 01000000 to keep 7th bit
// ...
// 00000001 to keep 1st bit (j == 0)
unsigned char mask = 1 << j;
byte = byte & mask;
// Then offset byte so that the (j+1)-th bit is in the first position.
// So the char will always be 0 or 1 depending on the value of the (j+1)-th bit
byte >>= j;
printf("%u", byte);
}
}
puts("");
}
int main(void)
{
char c = 1;
int i = 1;
float f = 1.f;
double d = 1.;
printf("char c = 1 : "); printBits(sizeof(c), &c);
printf("int i = 1 : "); printBits(sizeof(i), &i);
printf("float f = 1.f: "); printBits(sizeof(f), &f);
printf("double d = 1. : "); printBits(sizeof(d), &d);
c = -1;
i = -1;
f = -1.f;
d = -1.;
printf("char c = -1 : "); printBits(sizeof(c), &c);
printf("int i = -1 : "); printBits(sizeof(i), &i);
printf("float f = -1.f: "); printBits(sizeof(f), &f);
printf("double d = -1. : "); printBits(sizeof(d), &d);
c = '1';
printf("char c = '1' : "); printBits(sizeof(c), &c);
c = 49;
printf("char c = 49 : "); printBits(sizeof(c), &c);
printf("\nPrint values 1, 2 and 4 (float):\n");
f = 1.f;
printBits(sizeof(f), &f); // 0 0111 1111 00000000000000000000000 exp=127 (-127) => e=0 1*2^0 = 1
f = 2.f;
printBits(sizeof(f), &f); // 0 1000 0000 00000000000000000000000 exp=128 (-127) => e=1 1*2^1 = 2
f = 4.f;
printBits(sizeof(f), &f); // 0 1000 0001 00000000000000000000000 exp=129 (-127) => e=2 1*2^2 = 4
return 0;
}
#include <stdio.h>
int main(void)
{
// BIIIP using a char
char c = '\7';
printf("%c", c);
c = '\a';
printf("%c", c);
// BIIIP using a string
const char* s = "\7";
printf("%s", s);
return 0;
}
#include <stdio.h>
// To use bool instead of _Bool
// Added in C99: https://en.wikipedia.org/wiki/C99
#include <stdbool.h>
int main(void)
{
int oldBool = 42;
if (oldBool == 0)
{
printf("%s", "oldBool is FALSE\n");
}
// Any other value is considered true
else
{
printf("%s", "oldBool is TRUE\n");
}
// _Bool is a keyword of the language
_Bool newBool = 42;
if (newBool == 0)
{
printf("%s", "newBool is FALSE\n");
}
// Any other value is considered true
else
{
printf("%s", "newBool is TRUE\n");
}
bool betterBool = true;
if (betterBool == false)
{
printf("%s", "betterBool is FALSE\n");
}
else
{
printf("%s", "betterBool is TRUE\n");
}
// Test output
{
_Bool test = 3; //!=0 so considered true
printf("%d\n", test);
test = (2 * 3) < 7;
printf("%d\n", test);
}
{
bool test = true;
printf("%d\n", test);
test = (2 * 3) < 7;
printf("%d\n", test);
}
_Bool x = true;
bool y = true;
char z = true;
printf("x = %d\n", x);
printf("y = %d\n", y);
printf("z = %d\n", z);
printf("(x == true) = %d\n", x == true);
printf("Memory space: \n");
printf("x: %d\n", (int) sizeof(x));
printf("y: %d\n", (int) sizeof(y));
printf("z: %d\n", (int) sizeof(z));
printf("(x == true): %d\n", (int) sizeof(x == true));
return 0;
}