Thread: Printf "skipping" in CodeBlocks

  1. #1
    Registered User
    Join Date
    Mar 2016
    Posts
    110

    Printf "skipping" in CodeBlocks

    Code:
    #include "arithmeticEvaluator.h"
    #include <stdio.h>
    #include <stdlib.h>
    
    
    #define CLOSING_PARENTHESIS ')'
    
    
    void convertToPostfix(char *infix){
        int a = 1;
        printf("%d", a);
        //printCharacterStack(operatorStack);
        char token;
        while((token = getToken(&infix)) != '\0'){
                if(isdigit(token))
                {
                    pushNum(&operandStack, token);
                }
                else if(token == CLOSING_PARENTHESIS)
                {
                  /*flushOperatorStackToNextParenthesis(&operandStack, &operatorStack);*/
                }
                else if(!isdigit(token))
                {
                    pushChar(&operatorStack, token);
                }
        }
    
    
    }
    Its not that it hangs. Its that the program goes to the next statements without printing anything.

    CodeBlocks 16.01
    GNUGCC compiler
    Windows

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Try
    printf("%d\n", a);

    or
    printf("%d", a);
    fflush(stdout);


    stdout is by default line-buffered, so you only see output
    - when the stream is closed
    - when the internal buffer is full
    - when you output a \n
    - when you specifically call fflush.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Additionally, on most implementations (it is implementation-defined, but all rational implementations that can do so will do so) you will see the output on an interactive device if you call an input function afterwards on the same device.
    Code:
    #include <stdio.h>
    int main() {
        putchar('x'); // won't automatically appear
        getchar();    // will cause the x to appear
        return 0;
    }
    C99: 17.19.3
    3. When a stream is unbuffered, characters are intended to appear from the source or at the destination as soon as possible. Otherwise characters may be accumulated and transmitted to or from the host environment as a block. When a stream is fully buffered, characters are intended to be transmitted to or from the host environment as a block when a buffer is filled. When a stream is line buffered, characters are intended to be transmitted to or from the host environment as a block when a new-line character is encountered. Furthermore, characters are intended to be transmitted as a block to the host environment when a buffer is filled, when input is requested on an unbuffered stream, or when input is requested on a line buffered stream that requires the transmission of characters from the host environment. Support for these characteristics is implementation-defined, and may be affected via the setbuf and setvbuf functions.
    Last edited by algorism; 01-29-2017 at 01:39 PM.

  4. #4
    Registered User
    Join Date
    Mar 2016
    Posts
    110
    Quote Originally Posted by Salem View Post
    Try
    printf("%d\n", a);

    or
    printf("%d", a);
    fflush(stdout);


    stdout is by default line-buffered, so you only see output
    - when the stream is closed
    - when the internal buffer is full
    - when you output a \n
    - when you specifically call fflush.
    Tried it, no luck.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Perhaps you should post a complete program which replicates the problem. Talk about vague and unhelpful.

    Works for me.
    Code:
    //#include "arithmeticEvaluator.h"
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    #define CLOSING_PARENTHESIS ')'
    
    char getToken( char **p ) {
      char result = **p;
      (*p)++;
      return result;
    }
    
    void convertToPostfix(char *infix){
        int a = 1;
        printf("%d", a);
        fflush(stdout);
        //printCharacterStack(operatorStack);
        char token;
        while((token = getToken(&infix)) != '\0'){
          putchar(token);
          sleep(1);
    //       if(isdigit(token))
    //       {
    //           pushNum(&operandStack, token);
    //       }
    //       else if(token == CLOSING_PARENTHESIS)
    //       {
    //         /*flushOperatorStackToNextParenthesis(&operandStack, &operatorStack);*/
    //       }
    //       else if(!isdigit(token))
    //       {
    //           pushChar(&operatorStack, token);
    //       }
        }
    }
    
    int main(void)
    {
      char a[] = "ja;ogtujw3";
      convertToPostfix(a);
      return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Mar 2016
    Posts
    110
    Quote Originally Posted by Salem View Post
    Perhaps you should post a complete program which replicates the problem. Talk about vague and unhelpful.

    Works for me.
    Code:
    //#include "arithmeticEvaluator.h"
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    #define CLOSING_PARENTHESIS ')'
    
    char getToken( char **p ) {
      char result = **p;
      (*p)++;
      return result;
    }
    
    void convertToPostfix(char *infix){
        int a = 1;
        printf("%d", a);
        fflush(stdout);
        //printCharacterStack(operatorStack);
        char token;
        while((token = getToken(&infix)) != '\0'){
          putchar(token);
          sleep(1);
    //       if(isdigit(token))
    //       {
    //           pushNum(&operandStack, token);
    //       }
    //       else if(token == CLOSING_PARENTHESIS)
    //       {
    //         /*flushOperatorStackToNextParenthesis(&operandStack, &operatorStack);*/
    //       }
    //       else if(!isdigit(token))
    //       {
    //           pushChar(&operatorStack, token);
    //       }
        }
    }
    
    int main(void)
    {
      char a[] = "ja;ogtujw3";
      convertToPostfix(a);
      return 0;
    }
    main.c
    Code:
    #include "arithmeticEvaluator.h"#include <stdio.h>
    
    
    int main(){
        char* postfix;
        char* infixedUserInput = getKeyboardInput();
    
    
        void convertToPostfix(infixedUserInput);
        //printCharacterStack(operatorStack);
    
    
        return 0;
    }
    inputAndPrintinUtilities.c
    Code:
    #include <stdio.h>#include "arithmeticEvaluator.h"
    #define MEMORY_ALLOCATE 20
    
    
    const char* withinArraySeperator = "   ";
    
    
    void printCharacterArray(char* arrayToBePrinted){
        for(int i = 0; arrayToBePrinted[i] != '\0'; i++)
        {
            printf("%c%s", arrayToBePrinted[i], withinArraySeperator);
        }
    }
    
    
    void printCharacterStack(struct CharStack stackToBePrinted){
        printf("Hello!");
    //    int sizeOfStack = stackToBePrinted.stackPointer;
    //    char characterArrayToBePrinted[MAX_STACK_SIZE];
    //    int i = 0;
    //    for(i; i < sizeOfStack; i++)
    //    {
    //        characterArrayToBePrinted[i] = stackToBePrinted.stackValue[i];
    //    }
    //    characterArrayToBePrinted[i] = '\0';
    //    printCharacterArray(characterArrayToBePrinted);
    
    
    }
    
    
    char* getKeyboardInput(void){
        char* input = malloc(sizeof(char) * MEMORY_ALLOCATE); //Still have to free memory
        if (input == NULL) {
            fprintf(stderr, "Failed to allocate memory.\n");
            exit(-1);
        }
        int i;
        char inputCharacter;
        for(i = 0; i < MEMORY_ALLOCATE; i++){
            inputCharacter = getchar();
            if(inputCharacter == '\n'){
                break;
            }
            input[i] = inputCharacter;
        }
        input[i] = '\0';
        return input;
    }
    
    
    char getToken(char **inputString){
        char returnCharacter = **inputString;
        *inputString = *inputString + 1;    //Still have to free memory
        return returnCharacter;
    }
    stackUtilities.c
    Code:
    #include <stdio.h>
    #include "arithmeticEvaluator.h"
    
    
    #define MAX_STACK_SIZE 100
    
    
    struct CharStack operatorStack = {.stackPointer = 5, .stackValue = {'7','2','3','4','5'}};
    struct NumStack operandStack = {.stackPointer = 0};
    
    
    void pushChar(struct CharStack *stack, char pushedValue){
        if (stack -> stackPointer < MAX_STACK_SIZE) {
            stack -> stackValue[stack -> stackPointer++] = pushedValue;
        }
        else {
            fprintf(stderr, "Character Stack full, can't push %g\n", pushedValue);
            exit(-1);
        }
    }
    
    
    char popChar(struct CharStack stack){
        if (stack.stackPointer > 0){
            return stack.stackValue[--stack.stackPointer];
        }
        else {
            fprintf(stderr, "Character Stack empty\n");
            exit(-1);
        }
    }
    
    
    void pushNum(struct NumStack *stack, double pushedValue){
        if (stack -> stackPointer < MAX_STACK_SIZE) {
            stack -> stackValue[stack -> stackPointer++] = pushedValue;
        }
        else {
            fprintf(stderr, "Number Stack full, can't push %g\n", pushedValue);
            exit(-1);
        }
    }
    
    
    double popNum(struct NumStack stack){
        if (stack.stackPointer > 0){
            return stack.stackValue[--stack.stackPointer];
        }
        else {
            fprintf(stderr, "Number Stack empty\n");
            exit(-1);
        }
    }
    convertToPostfix.c
    Code:
    #include "arithmeticEvaluator.h"#include <stdio.h>
    #include <stdlib.h>
    
    
    #define CLOSING_PARENTHESIS ')'
    
    
    void convertToPostfix(char *infix){
        int a = 1;
           fflush(stdout);
        printf("%d", a);
    
    
        //printCharacterStack(operatorStack);
        char token;
        while((token = getToken(&infix)) != '\0'){
                if(isdigit(token))
                {
                    pushNum(&operandStack, token);
                }
                else if(token == CLOSING_PARENTHESIS)
                {
                  /*flushOperatorStackToNextParenthesis(&operandStack, &operatorStack);*/
                }
                else if(!isdigit(token))
                {
                    pushChar(&operatorStack, token);
                }
        }
    
    
    }
    arithmeticEvaluaator.h
    Code:
    #ifndef ARITHMETICEVALUATOR_H#define ARITHMETICEVALUATOR_H
    
    
    #define MAX_STACK_SIZE 100
    
    
    extern struct CharStack operatorStack;
    extern struct NumStack operandStack;
    
    
    struct CharStack{
        char stackValue[MAX_STACK_SIZE];
        int stackPointer;
    };
    
    
    struct NumStack{
        double stackValue[MAX_STACK_SIZE];
        int stackPointer;
    };
    
    
    void pushChar(struct CharStack *, char pushedValue);
    char popChar(struct CharStack);
    
    
    void printCharacterArray(char*);
    void printCharacterStack(struct CharStack);
    char* getKeyboardInput(void);
    char getToken(char **);
    
    
    void convertToPostfix(char *);
    
    
    #endif

  7. #7
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    In main.c:
    This isn't how you call a function. Delete the "void". Isn't your compiler complaining about this? Try compiling with warnings enabled( -Wall -Wextra ).
    Devoted my life to programming...

  8. #8
    Registered User
    Join Date
    Mar 2016
    Posts
    110
    Unbelievable.

    I would've never have figured that out because I was never looking at main. I enabled all warnings and got this.

    warning: parameter names (without types) in function declaration
    I'm not sure that this is the best explanation... but it works!

    Thanks man!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 02-21-2016, 12:35 AM
  2. Visual "C" alternatives? ...codeBlocks ...wxWidgets
    By vmars316 in forum C Programming
    Replies: 1
    Last Post: 04-06-2012, 10:38 PM
  3. skipping "else" statement is faster?
    By carlorfeo in forum C++ Programming
    Replies: 13
    Last Post: 03-03-2008, 04:41 AM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM

Tags for this Thread