Note: ANSI C refers to ISO/IEC C. This is a continuation from the previous Module. The source code for this Module is: C/C++ source codes and the practice worksheets: C/C++ basic data types and the C/C++ standard input, scanf()/scanf_s().
Boolean, bool is a lateral true or false. Use bool and the literals false and true to make Boolean logic tests.
The bool keyword represents a type that can take only the value false or true. The keywords false and true are Boolean literals with predefined values. false is numerically zero and true is numerically one. These Boolean literals are rvalues (right value); you cannot make an assignment to them. Let play around with the following program examples.
// a sample Boolean tests with bool, true, and false.
#include <iostream>
using namespace std;
// non main function but just another function...
bool func()
{
// function returns a bool type
return NULL;
// NULL is converted to Boolean false, same
// as statement 'return false;'
}
int main()
{
bool val = false; // Boolean variable
int i = 1; // i is neither Boolean-true nor Boolean-false
int g = 5;
float j = 3.02; // j is neither Boolean-true nor Boolean-false
cout<<"Given the test value: "<<endl;
cout<<"bool val = false "<<endl;
cout<<"int i = 1 "<<endl;
cout<<"int g = 5 "<<endl;
cout<<"float j = 3.02 "<<endl;
cout<<"\nTESTING\n";
// tests on integers
if(i == true)
cout<<"True: value i is 1"<<endl;
if(i == false)
cout<<"False: value i is 0"<<endl;
if(g)
cout << "g is true."<<endl;
else
cout << "g is false."<<endl;
// to test j's truth value, cast it to bool type.
if(bool(j) == true)
cout<<"Boolean j is true."<<endl;
// test the Boolean function returns value
val = func();
if(val == false)
cout<<"func() returned false."<<endl;
if(val == true)
cout<<"func() returned true."<<endl;
// false is converted to 0
return false;
}

// testing the escape sequences
#include <stdio.h>
int main()
{
printf("Testing the escape sequences:\n");
printf("-----------------------------\n");
printf("The audible bell --->\'\\a\' \a\a\a\n");
printf("The backspace --->\'\\b\' \bTesting\n");
printf("The formfeed, printer --->\'\\f\' \fTest\n");
printf("The newline --->\'\\n\' \n\n");
printf("The carriage return --->\'\\r\' \rTesting\n");
printf("The horizontal tab --->\'\\t\' \tTesting\t\n");
printf("The vertical tab --->\'\v\' \vTesting\n");
printf("The backslash --->\'\\\\' \\Testing\\\n");
printf("The single quote --->\'\'\' \'Testing\'\'\'\n");
printf("The double quote --->\'\"\' \"Testing\"\"\n");
printf("The question mark --->\'\?\' \?Testing\?\n");
printf("Some might not working isn't it?\n");
return 0;
}

#include <stdio.h>
int main()
{
int num;
printf("Conversion...\n");
printf("Start with any character and\n");
printf("Press Enter, EOF to stop\n");
num = getchar();
printf("Character Integer Hexadecimal Octal\n");
while(getchar() != EOF)
{
printf(" %c %d %x %o\n", num, num, num, num);
++num;
}
return 0;
}

#include <stdio.h>
/* convert decimal to binary function */
void dectobin();
int main()
{
char chs = 'Y';
do
{
dectobin();
printf("Again? Y, others to exit: ");
chs = getchar();
scanf("%c", &chs);
}while ((chs == 'Y') || (chs == 'y'));
return 0;
}
void dectobin()
{
int input;
printf("Enter decimal number: ");
scanf("%d", &input);
if (input < 0)
printf("Enter unsigned decimal!\n");
/* for the mod result */
int i;
/* count the binary digits */
int count = 0;
/* storage */
int binbuff[64];
do
{
/* Modulus 2 to get the remainder of 1 or 0 */
i = input%2;
/* store the element into the array */
binbuff[count] = i;
/* Divide the input by 2 for binary decrement */
input = input/2;
/* Count the number of binary digit */
count++;
/* repeat */
}while (input > 0);
/* prints the binary digits */
printf ("The binary representation is: ");
do
{
printf("%d", binbuff[count - 1]);
count--;
if(count == 8)
printf(" ");
} while (count > 0);
printf ("\n");
}

#include <stdio.h>
/* for strlen() */
#include <string.h>
/* convert bin to decimal */
void bintodec()
{
char buffbin[100];
char *bin;
int i=0;
int dec = 0;
int bcount;
printf("Please enter the binary digits, 0 or/and 1.\n");
printf("Your binary digits: ");
bin = gets(buffbin);
i=strlen(bin);
for (bcount=0; bcount<i; ++bcount)
/* if bin[bcount] is equal to 1, then 1 else 0 */
dec=dec*2+(bin[bcount]=='1'? 1:0);
printf("\n");
printf("The decimal value of %s is %d\n", bin, dec);
}
int main(void)
{
bintodec();
return 0;
}

/* playing with binary, decimal, hexadecimal and octal conversion */
#include <stdio.h>
#include <stdlib.h>
/* for strlen() */
#include <string.h>
/* octal conversion function */
void octal(char *octa, int *octares);
/* hexadecimal conversion function */
void hexadecimal(char *hexa, int *hexares);
/* decimal conversion function */
void decimal(char *deci, int *decires);
/* convert binary to decimal */
void bintodec(void);
/* convert decimal to binary */
void decnumtobin (int *dec);
int main()
{
/* Yes or No value to continue with program */
char go;
/* Yes or No value to proceed to Binary to Decimal function */
char binY;
char choice1;
char choice2;
/* numtest, value to test with, and pass to functions */
int numtest;
/* value to convert to binary, and call decnumtobin function */
int bintest;
int flag;
flag = 0;
go = 'y';
do
{
printf("Enter the base of ur input(d=dec, h=hex, o=octal): ");
scanf("%c", &choice1);
getchar();
printf("\n");
printf("The entered Number: ");
/* If decimal number */
if ((choice1 == 'd') || (choice1 == 'D'))
{
scanf("%d", &numtest);
getchar();
}
/* If hexadecimal number */
else if ((choice1 == 'h') || (choice1 == 'H'))
{
scanf("%x", &numtest);
getchar();
}
/* If octal number */
else if ((choice1 == 'o') || (choice1 == 'O'))
{
scanf("%o", &numtest);
getchar();
}
/* If no match */
else
{
flag = 1;
printf("Only d, h or o options!\n");
printf("Program exit...\n");
exit(0);
}
/* Firstly convert the input 'number' to binary */
bintest = numtest;
decnumtobin(&bintest);
/* output the hex, decimal or octal */
printf("\n");
printf("Next, enter the base of ur output (d=dec, h=hex, o=octal): ");
scanf("%c", &choice2);
getchar();
/* If decimal number */
if ((choice2 == 'd') || (choice2 == 'D'))
decimal (&choice1, &numtest);
/* If hexadecimal number */
else if ((choice2 == 'h') || (choice2 == 'H'))
hexadecimal (&choice1, &numtest);
/* If octal number */
else if ((choice2 == 'o') || (choice2 == 'O'))
octal (&choice1, &numtest);
/* if nothing matched */
else
{
flag = 1;
system("cls");
printf("Only d, h or o options!");
printf("\nProgram exit...");
exit(0);
}
printf("\n\nAn OPTION\n");
printf("=========\n");
printf("Do you wish to do the binary to decimal conversion?");
printf("\n Y for Yes, and N for no : ");
scanf("%c", &binY);
getchar();
/* If Yes... */
if ((binY == 'Y') || (binY == 'y'))
/* Do the binary to decimal conversion */
bintodec();
/* If not, just exit */
else if ((binY != 'y') || (binY != 'Y'))
{
flag = 1;
printf("\nProgram exit...\n");
exit(0);
}
printf("\n\n");
printf("The program is ready to exit...\n");
printf("Start again? (Y for Yes) : ");
scanf("%c", &go);
getchar();
/* initialize to NULL */
numtest = '\0';
choice1 = '\0';
choice2 = '\0';
}
while ((go == 'y') || (go == 'Y'));
printf("-----FINISH-----\n");
return 0;
}
/*===================================================*/
void decimal(char *deci, int *decires)
{
int ans = *decires;
char ch = *deci;
if ((ch == 'd') || (ch == 'D'))
printf("\nThe number \"%d\" in decimal is equivalent to \"%d\" in decimal.\n", ans, ans);
else if ((ch == 'h') || (ch == 'H'))
printf("\nThe number \"%X\" in hex is equivalent to \"%d\" in decimal.\n", ans, ans);
else if ((ch == 'o') || (ch == 'O'))
printf("\nThe number \"%o\" in octal is equivalent to \"%d\" in decimal.\n", ans, ans);
}
/*======================================================*/
void hexadecimal(char *hexa, int *hexares)
{
int ans = *hexares;
char ch = *hexa;
if ((ch == 'd') || (ch == 'D'))
printf("\nThe number \"%d\" in decimal is equivalent to \"%X\" in hexadecimal.\n", ans, ans);
else if ((ch == 'h') || (ch == 'H'))
printf("\nThe number \"%X\" in hex is equivalent to \"%X\" in hexadecimal.\n", ans, ans);
else if ((ch == 'o') || (ch == 'O'))
printf("\nThe number \"%o\" in octal is equivalent to \"%X\" in hexadecimal.\n", ans, ans);
}
/*========================================================*/
void octal(char *octa, int *octares)
{
int ans = *octares;
char ch = *octa;
if ((ch == 'd') || (ch == 'D'))
printf ("\nThe number \"%d\" in decimal is equivalent to \"%o\" in octal.\n", ans, ans);
else if ((ch == 'h') || (ch == 'H'))
printf("\nThe number \"%X\" in hex is equivalent to \"%o\" in octal. \n", ans, ans);
else if ((ch == 'o') || (ch == 'O'))
printf("\nThe number \"%o\" in octal is equivalent to \"%o\" in octal.\n", ans, ans);
}
void bintodec(void)
{
char buffbin[1024];
char *binary;
int i=0;
int dec = 0;
int z;
printf("Please enter the binary digits, 0 or 1.\n");
printf("Your binary digits: ");
binary = gets(buffbin);
i=strlen(binary);
for(z=0; z<i; ++z)
/* if Binary[z] is equal to 1, then 1 else 0 */
dec=dec*2+(binary[z]=='1'? 1:0);
printf("\n");
printf("The decimal value of %s is %d", binary, dec);
printf("\n");
}
void decnumtobin (int *dec)
{
int input = *dec;
int i;
int count = 0;
int binary[64];
do
{
/* Modulus 2 to get 1 or a 0 */
i = input%2;
/* Load Elements into the Binary Array */
binary[count] = i;
/* Divide input by 2 for binary decrement */
input = input/2;
/* Count the binary digits */
count++;
}while (input > 0);
/* Reverse and output binary digits */
printf ("The binary representation is: ");
do
{
printf ("%d", binary[count - 1]);
count--;
} while (count > 0);
printf ("\n");
}
Output:

/* playing with binary, decimal, hexadecimal and octal conversion */
#include <stdio.h>
#include <stdlib.h>
/* for the strlen() */
#include <string.h>
/* decimal conversion function */
void decimal(char *deci, int *decires);
/* convert decimal to binary*/
void decnumtobin (int *dec);
int main()
{
/* Yes or No value to continue with program */
char go;
char choice1;
char choice2;
/* numtest, value to test with, and pass to functions */
int numtest;
/* value to convert to binary, and call decnumtobin function */
int bintest;
int flag;
flag = 0;
go = 'y';
do
{
printf ("Enter the h for hex input: ");
scanf("%c", &choice1);
getchar();
printf ("\n");
printf ("Enter your hex number lor!: ");
/*If hexadecimal number*/
if ((choice1 == 'h') || (choice1 == 'H'))
{
scanf ("%x", &numtest);
getchar();
}
else
{
flag = 1;
printf ("Only h!\n");
printf("Program exit...\n");
exit(0);
}
/* Firstly convert the input 'number' to binary */
bintest = numtest;
decnumtobin(&bintest);
/* output the hex, decimal or octal */
printf ("\n");
printf ("Enter the d for decimal output: ");
scanf ("%c", &choice2);
getchar();
/* If decimal number... */
if ((choice2 == 'd') || (choice2 == 'D'))
decimal(&choice1, &numtest);
/* else... */
else
{
flag = 1;
printf("Only d!");
printf("\nProgram exit...");
exit(0);
}
printf ("\n\n");
printf ("The program is ready to exit...\n");
printf ("Start again? (Y for Yes) : ");
scanf ("%c", &go);
getchar();
/* initialize to NULL*/
numtest = '\0';
choice1 = '\0';
choice2 = '\0';
} while ((go == 'y') || (go == 'Y'));
printf ("-----FINISH-----\n");
return 0;
}
/*===================================================*/
void decimal(char *deci, int *decires)
{
int ans = *decires;
char ch = *deci;
if ((ch == 'h') || (ch == 'H'))
printf ("\nThe number \"%X\" in hex is equivalent to \"%d\" in decimal.\n", ans, ans);
}
void decnumtobin (int *dec)
{
int input = *dec;
int i;
int count = 0;
int binary[128];
do
{
/* Modulus 2 to get 1 or a 0 */
i = input%2;
/* Load Elements into the Binary Array */
binary[count] = i;
/* Divide input by 2 for binary decrement */
input = input/2;
/* Count the binary digits*/
count++;
}while (input > 0);
/* Reverse and output binary digits */
printf ("The binary representation is: ");
do
{
printf ("%d", binary[count - 1]);
count--;
if(count == 4)
printf(" ");
} while (count > 0);
printf ("\n");
}
Output:

/* playing with hexadecimal and ASCII */
#include <stdio.h>
#include <stdlib.h>
/* strlen() */
#include <string.h>
/* decimal conversion function */
void decimal(int *decires);
/* convert decimal to binary */
void decnumtobin (int *dec);
int main()
{
/* program continuation... */
char go;
/* numtest, value to test with, and pass to functions */
int numtest;
/* value to convert to binary, and call decnumtobin function */
int bintest;
int flag = 0;
go = 'y';
do
{
printf("Playing with hex and ASCII\n");
printf("==========================\n");
printf("For hex, 0(0) - 1F(32) are non printable/control characters!\n");
printf("For hex > 7F(127) they are extended ASCII characters that are\n");
printf("platform dependent!\n\n");
printf("Enter the hex input: ");
scanf("%x", &numtest);
getchar();
/* Firstly convert the input 'number' to binary */
bintest = numtest;
decnumtobin(&bintest);
decimal (&numtest);
printf("\nStart again? (Y for Yes) : ");
scanf ("%c", &go);
getchar();
/ * initialize to NULL*/
numtest = '\0';
}while ((go == 'y') || (go == 'Y'));
printf("-----FINISH-----\n");
return 0;
}
/*===================================================*/
void decimal(int *decires)
{
int ans = *decires;
/* If < decimal 32... */
if(ans < 32)
{
printf("hex < 20(32) equivalent to non printable/control ascii characters\n");
switch(ans)
{
case 0:{printf("hex 0 is NULL ascii");}break;
case 1:{printf("hex 1 is SOH-start of heading ascii");}break;
case 2:{printf("hex 2 is STX-start of text ascii");}break;
case 3:{printf("hex 3 is ETX-end of text ascii");}break;
case 4:{printf("hex 4 is EOT-end of transmission ascii");}break;
case 5:{printf("hex 5 is ENQ-enquiry ascii");}break;