LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Help with simple C string function (https://www.linuxquestions.org/questions/programming-9/help-with-simple-c-string-function-4175418175/)

fahad.anwar 07-23-2012 01:26 AM

Help with simple C string function
 
Hi all

I have my main function as below :
I have included a couple of printfs. I need to know why the program never prints printf("1")even when the value of S is given as 0.

Thanks in advance


Code:

int main()
{
        int U, L, S, fahad;
        char in[20], *pEnd;
        node * root = (node *)calloc(1,sizeof(node));
        fgets(in, 20, stdin);
        U = strtol (in,&pEnd,10);
        L = strtol (pEnd,NULL,10);
        minSize = 1<<L;
        root->size = 1<<U;
        while(fgets(in, 20, stdin)>=0)
        {
                S = strtol (in,NULL,10);
                printf("\n S = %d\n",S);
                if(S == 0)
                {
                        printf("1");
                        deallocate(root, *in);
                        printf("2");
                }
                else
                        allocate(root, *in, S);
        }
        print(root);
        return 0;
}


NevemTeve 07-23-2012 03:24 AM

Your program is incomplete; the first compilation error is:

Code:

bamba1.c:7: error: 'node' undeclared (first use in this function)

Wim Sturkenboom 07-23-2012 05:36 AM

Add a '\n' at the end of the printf or (better?) force a flush of stdout.

I suspect there to be a crash in your deallocate() and therefore stdout does not get flushed and hence no output; might be wrong though.

pan64 07-23-2012 06:12 AM

if you want to see the complete source, see last post here: http://www.linuxquestions.org/questi...-c-4175417104/, probably up to date.
here is a small discussion about fflush: http://cboard.cprogramming.com/c-pro...sh-stdout.html

Wim Sturkenboom 07-24-2012 03:40 AM

So it's indeed a crash in which case the problem is solved and the thread can be marked as such :)


All times are GMT -5. The time now is 08:15 PM.