arrayscprintfstdio

I can't printf() or write() my array after using a read() on stdin to check for '\n' (enter)


I am writing a small program that is supposed to print out sin(counter), while counter is incremented by 0.01 every time I put in '\n' (Enter) in the console.

Both printf("%lf", sin_output[0]); and write(1, &sin_output, sizeof(sin_output));do not print anything to the console. Through debugging I found out that the rest of my code is working as intended, right before sin_output[0] is printed, it is filled with the correct value of sin(counter).

I chose an array, because I want the program to write into a socket or pipe later on, for which a printf() is useless to my knowledge.

I tried reproducing this error in a small program that goes as follows:

#include <stdio.h>
#include <unistd.h>
#include <string.h>

int main() {
  double array[1];
  array[0] = 1;
  printf("%lf", array[0]);

  return 0;
}

This prints out the correct value. Output: 1.000000

Substituting printf("%lf", array[0]); with write(1, array, sizeof(array)); gives out some random characters, which is due to write() formatting my output to a string maybe?

Using write(1, &array, sizeof(array)); prints the same gibberish.

Now I tried array[0] = sin(x);:

#include <stdio.h>
#include <unistd.h>
#include <string.h>

int main() {
  double array[1];
  double x = 0.01
  array[0] = sin(x);
  printf("%lf", array[0]);

  return 0;
}

Which prints 0.010000 for some reason (rounding?).

Using double x = 0.1 prints out the right value.

So I tried every option I know of in a minimal reproducible system. All of the programs output at least something, be it a wrong or unwanted value.

Using the (almost) exact same structure, of array[1] = sin(enter_counter) in my code however prints no value whatsoever. None.

I suspect it to be thanks to me using read(0, &buf, sizeof(buf)) and the program producing some unwanted behavior with the stdin/stdout buffer, which leads to stdout not printing properly both for printf and write().

My code for the actual program is:

/* includes*/
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

/* main function*/
int main() {
  /* declare variables*/
  char buf[1];
  double enter_counter = 0;
  double sin_output[1];

  /* reset buf*/
  memset(&buf, 0, 1);

  /* master loop*/
  while (1) {
    /* read stdin*/
    read(0, &buf, sizeof(buf));

    /* if stdin == enter key, progress sin by 0.01rad*/
    if (buf[0] == 10) {
      enter_counter += 0.01;
    }

    /* reset buffer*/
    memset(&buf, 0, 1);

    /* calculate sin for enter_counter*/
    sin_output[0] = sin(enter_counter);
    
    /* printf sin(enter_value)*/
    printf("%lf", sin_output[0]); 
    //write(1, sin_output, sizeof(sin_output));
  }
}

Any help is appreciated.


Solution

  • You need to add a newline to your print statement as such:

    printf("%lf\n", sin_output[0]); 
    

    This yields the following for me:

    $ ./a.out 
    
    0.010000
    
    0.019999
    
    0.029996
    
    0.039989
    
    0.049979
    
    0.059964
    ^C
    

    Not sure exactly what's going on, but likely that your output is being buffered, and then overwritten by your own newline. Look into output buffering to get a better understanding.