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 theC/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 */
elseif ((choice1 == 'h') || (choice1 == 'H'))
{
scanf("%x", &numtest);
getchar();
}
/* If octal number */
elseif ((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 */
elseif ((choice2 == 'h') || (choice2 == 'H'))
hexadecimal (&choice1, &numtest);
/* If octal number */
elseif ((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 */
elseif ((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);
elseif ((ch == 'h') || (ch == 'H'))
printf("\nThe number \"%X\" in hex is equivalent to \"%d\" in decimal.\n", ans, ans);
elseif ((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);
elseif ((ch == 'h') || (ch == 'H'))
printf("\nThe number \"%X\" in hex is equivalent to \"%X\" in hexadecimal.\n", ans, ans);
elseif ((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);
elseif ((ch == 'h') || (ch == 'H'))
printf("\nThe number \"%X\" in hex is equivalent to \"%o\" in octal. \n", ans, ans);
elseif ((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;
case 6:{printf("hex 6 is ACK-acknowledge ascii");}break;
case 7:{printf("hex 7 is BEL-bell ascii");}break;
case 8:{printf("hex 8 is BS-backspace ascii");}break;
case 9:{printf("hex 9 is TAB-horizontal tab ascii");}break;
case 10:{printf("hex A is LF-NL line feed, new line ascii");}break;
case 11:{printf("hex B is VT-vertical tab ascii");}break;
case 12:{printf("hex C is FF-NP form feed, new page ascii");}break;
case 13:{printf("hex D is CR-carriage return ascii");}break;
case 14:{printf("hex E is SO-shift out ascii");}break;
case 15:{printf("hex F is SI-shift in ascii");}break;
case 16:{printf("hex 10 is DLE-data link escape ascii");}break;
case 17:{printf("hex 11 is DC1-device control 1 ascii");}break;
case 18:{printf("hex 12 is DC2-device control 2 ascii");}break;
case 19:{printf("hex 13 is DC3-device control 3 ascii");}break;
case 20:{printf("hex 14 is DC4-device control 4 ascii");}break;
case 21:{printf("hex 15 is NAK-negative acknowledge ascii");}break;
case 22:{printf("hex 16 is SYN-synchronous idle ascii");}break;
case 23:{printf("hex 17 is ETB-end of trans. block ascii");}break;
case 24:{printf("hex 18 is CAN-cancel ascii");}break;
case 25:{printf("hex 19 is EM-end of medium ascii");}break;
case 26:{printf("hex 1A is SUB-substitute ascii");}break;
case 27:{printf("hex 1B is ESC-escape ascii");}break;
case 28:{printf("hex 1C is FS-file separator ascii");}break;
case 29:{printf("hex 1D is GS-group separator ascii");}break;
case 30:{printf("hex 1E is RS-record separator ascii");}break;
case 31:{printf("hex 1F is US-unit separator ascii");}break;
}
}
else
printf ("\nThe number \"%X\" in hex is equivalent to \"%c\" ascii character.\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:
// using C code and header in C++ (C++ wrapper)
#include <cstdio>
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;
}
-----------------------------------------------------------------------------
Program examples compiled usinggcc.
/* another data type program example */
#include <stdio.h>
/* main function */
int main()
{
int p = 2000; /* positive integer data type */
short int q = -120; /* variation */
unsigned short int r = 121; /* variation */
float s = 21.566578; /* float data type */
char t = 'r'; /* char data type */
long u = 5678; /* long positive integer data type */
unsigned long v = 5678; /* variation */
long w = -5678; /* -ve long integer data type */
int x = -171; /* -ve integer data type */
short y = -71; /* short -ve integer data type */
unsigned short z = 99; /* variation */
double a = 88.12345; /* double float data type */
float b = -3.245823; /* float data type */
printf("\t--Data type again--\n");
printf("\t-------------------\n");
printf("\n1. \"int\" sample: \t\t %d, the data size: %d bytes", p, sizeof(p));
printf("\n2. \"short\" int sample: \t %d, the data size: %d bytes", q, sizeof(q));
printf("\n3. \"unsigned short int\" sample: %d, the data size: %d bytes", r, sizeof(r));
printf("\n4. \"float\" sample: \t\t %.7f, the data size: %d bytes", s, sizeof(s));
printf("\n5. \"char\" sample: \t\t %c, the data size: %d byte", t, sizeof(t));
printf("\n6. \"long\" sample: \t\t %d, the data size: %d bytes", u, sizeof(u));
printf("\n7. \"unsigned long\" sample: \t %d, the data size: %d bytes", v, sizeof(v));
printf("\n8. negative \"long\" sample: \t %d, the data size: %d bytes", w, sizeof(w));
printf("\n9. negative \"int\" sample: \t %d, the data size: %d bytes", x, sizeof(x));
printf("\n10. negative \"short\" sample: \t %d, the data size: %d bytes", y, sizeof(y));
printf("\n11. unsigned \"short\" sample: \t %d, the data size: %d bytes", z, sizeof(z));
printf("\n12. \"double\" sample: \t\t %.4f, the data size: %d bytes", a, sizeof(a));
printf("\n13. negative \"float\" sample: \t %.5f, the data size: %d bytes\n", b, sizeof(b));
return 0;
}
[bodo@bakawali ~]$ gcc datatype.c -o datatype
[bodo@bakawali ~]$ ./datatype
--Data type again--
-------------------
1. "int" sample: 2000, the data size: 4 bytes
2. "short" int sample: -120, the data size: 2 bytes
3. "unsigned short int" sample: 121, the data size: 2 bytes
4. "float" sample: 21.5665779, the data size: 4 bytes
5. "char" sample: r, the data size: 1 byte
6. "long" sample: 5678, the data size: 4 bytes
7. "unsigned long" sample: 5678, the data size: 4 bytes
8. negative "long" sample: -5678, the data size: 4 bytes
9. negative "int" sample: -171, the data size: 4 bytes
10. negative "short" sample: -71, the data size: 2 bytes
11. unsigned "short" sample: 99, the data size: 2 bytes
12. "double" sample: 88.1235, the data size: 8 bytes
13. negative "float" sample: -3.24582, the data size: 4 bytes
#include <stdio.h>
#include <stdlib.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;
/* buffer for 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");
}
[bodo@bakawali ~]$ gcc binary.c -o binary
[bodo@bakawali ~]$ ./binary
Enter decimal number: 64
The binary representation is: 1000000
Again? Y, others to exit: Y
Enter decimal number: 128
The binary representation is: 10000000
Again? Y, others to exit: Y
Enter decimal number: 32
The binary representation is: 100000
Again? Y, others to exit: Y
Enter decimal number: 100
The binary representation is: 1100100
Again? Y, others to exit: N
[bodo@bakawali ~]$ cat binary.c