Search code examples
cbooleanqueueimplicit-conversion

Function is returning 1 instead of a value?


I am writing a Queue data structure and I am not able to retain the value of the integer in the array once the value is returned in the stack. The pop function is doing exactly what it needs to do but why doesn't main get that information? What am I missing? malloc?

#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>

int QUE[20];
const int EMPTY = -1;
int position = -1;
int retrieve = 0;

//push, append to front of array
bool push(int num) {
    if (position >= 20) return false;
    position += 1;
    QUE[position] = num;
    return true;
}

//pop from top of array
bool pop() {

    if(QUE[retrieve] == 0) return false;
    int hold = QUE[retrieve];
    printf("%d",hold);
    retrieve ++;
    return hold;

}

// PEEK

// First in first out

int main() {
    push(12);
    push(90);
    push(22);

    int t;
    //why does pop equal 1
    while ((t = pop()) != 0) {
        printf("t = %d\n",t);

    }

}

Solution

  • It is because any non zero value is being converted to bool true and then to integer. The integer value of the bool true is 1