HaxxorSiD HaxxorSiD - 4 months ago 34
C Question

Declaration Syntax Error for Bool function Line 7

I have wrote this program in C language, to perform Right Shift & 1s,2s complement on one 4 bit binary number , perform AND,OR,EX OR gate operations on two 1 bit binary numbers.

#include <conio.h>
#include <stdio.h>

int x, y, z, choice, i, rem, binary, temp;
int a[10], b[10], c[10];

bool checkBinary(int bin)
{
bool status = true;
for (i = 0; i < 4; i++) {
rem = bin % 10;
if (rem > 1) {
status = false;
break;
}
else
bin = bin / 10;
}
return status;
}

void shift()
{

printf("\nEnter 4-bit number:");
scanf("%d", &binary);
if (checkBinary(binary) != true)
printf("Invalid 4-bit number");
else {
for (i = 0; i < 4; i++) {
rem = binary % 10;
a[i] = rem;
binary = binary / 10;
}

for (i = 0; i < 2; i++) {
temp = a[3 - i];
a[3 - i] = a[i];
a[i] = temp;
}
for (i = 0; i < 3; i++) {
b[i + 1] = a[i];
}
b[0] = 0;

printf("\nAfter right shifting no. becomes: ");
for (i = 0; i < 4; i++) {
printf("%d", b[i]);
}
}
}

void comple()
{

printf("\nEnter 4-bit number: ");
scanf("%d", &binary);
if (checkBinary(binary) != true)
printf("Invalid 4-bit number");
else {
for (i = 0; i < 4; i++) {
rem = binary % 10;
if (rem == 0) {
a[i] = 1;
}
else {
a[i] = 0;
}
binary = binary / 10;
}
for (i = 0; i < 4; i++) {
b[3 - i] = a[i];
}
printf("\n1's Complement of number is: ");
for (i = 0; i < 4; i++) {
printf("%d", b[i]);
}

if (b[3] == 0) {
for (i = 0; i < 3; i++) {
c[i] = b[i];
}
c[3] = 1;
}
else {
c[3] = 0;
for (i = 2; i > 0; i--) {
if (b[i] == 1 && b[i - 1] == 0) {
c[i] = 0;
c[i - 1] = 1;
c[i - 2] = b[i];
break;
}
else if (b[i] == 0) {
c[i] = 1;
c[i - 1] = b[i - 1];
c[i - 2] = b[i - 2];
break;
}
else if (b[i] == 1 && b[i - 1] == 1) {
c[i] = 0;
c[i - 1] = 0;
}
}
}
printf("\n2's complement of no. is: ");
for (i = 0; i < 4; i++) {
printf("%d", c[i]);
}
}
}

void basic()
{
printf("\nEnter two numbers (0,1): ");
scanf("%d", &x);
scanf("%d", &y);
if ((x != 0 && x != 1) || (y != 0 && y != 1))
printf("Incorrect binary digits!");
else {
if (x == 1 && y == x) {
z = 1;
}
else {
z = 0;
}
printf("\n%d AND %d = %d", x, y, z);
if (y == 1 || x == 1) {
z = 1;
}
else {
z = 0;
}
printf("\n%d OR %d = %d", x, y, z);
if (x != y) {
z = 1;
}
else
z = 0;
printf("\n%d EXOR %d = %d", x, y, z);
}
}
void main()
{
clrscr();
printf("1. Shift Operation\n2. 1's & 2's Complement\n3. Basic Operations "
"(OR, AND, EXOR)\n");
printf("\nEnter your choice:");
scanf("%d", &choice);
switch (choice) {
case 1:
shift();
break;
case 2:
comple();
break;
case 3:
basic();
break;
default:
printf("\nIncorrect choice try again!");
break;
}
getch();
}


I have made switch case for choosing among 3 options. Shift/Complement/Gates and made 3 functions: shift() for right shift ,comple() for 1s 2s complement,basic() for gates operation.

I get a error at line 7 Declaration Syntax error.
If I remove everything related to checkBinary() function, the program works well.
Something is wrong in declaring checkBinary()

Answer

bool datatype was added in C99 standard. You need to add #include <stdbool.h> to use it, and maybe also add compiler parameter to use C99 and not some earlier standard which did not have bool.

Of course you could just use e.g. int instead.