1

Lately I've been trying a few exercises and examples a tutorial on YouTube gave its viewers. It all worked fine until I decided to put a scanf function to get user input.

After that everything appeared to stall leaving the terminal blank and only when i terminated the application the program's output appeared. What might be going wrong? I'm using Eclipse as IDE and mingw-64 as compiler

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


int sum(int* iterator, int* bound) {
   int sum = 0;

   while(iterator <= bound){
       sum += *iterator;
       iterator++;
   }
   return sum;


}
int main() {

   int at[] = { 1,2,4,6,7,-5,-3 };
   int* bound = &at[sizeof(at)/sizeof(int)-1];

   printf( "Sum of array is : %d \n" ,sum(at, bound));

   int matrix[3][3] ={{1,2,3},{4,5,6},{7,8,9}};

   for( bound = &matrix[0][0]; bound <= &matrix[2][2]; bound++){
       printf(" %d ", *bound);
   }

And the piece of code after adding the scanf function


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


int sum(int* iterator, int* bound) {
   int sum = 0;

   while(iterator <= bound){
       sum += *iterator;
       iterator++;
   }
   return sum;


}
int main() {

   int at[] = { 1,2,4,6,7,-5,-3 };
   int* bound = &at[sizeof(at)/sizeof(int)-1];

   printf( "Sum of array is : %d \n" ,sum(at, bound));

   int matrix[3][3] ={{1,2,3},{4,5,6},{7,8,9}};

   for( bound = &matrix[0][0]; bound <= &matrix[2][2]; bound++){
       printf(" %d ", *bound);
   }
   int s=3;
   printf("Give a number: ");
   scanf("%d",&s);
   printf("%d", s);
  return 0;



}


X HOxha
  • 143
  • 1
  • 2
  • 10
  • 1
    Please try to create a [mcve] to show us, with emphasis on the minimal part. There's a lot of code in your question that isn't really relevant to your problem. Also please take some time to refresh [ask] as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude May 05 '20 at 02:14
  • 2
    Suggest `printf("%d", s);` --> `printf("%d\n", s);` or `fflsuh(stdout);` I suspect an idiosyncrasy of Eclipse. See if that helps.. – chux - Reinstate Monica May 05 '20 at 02:14
  • 2
    As for your problem, it could be that Eclipse runs your program with a pipe for its `stdout`. That makes output to `stdout` (where `printf` writes) *fully* buffered. Which in turn means output won't be written until the program terminates or you explicitly call [`fflush(stdout`](https://en.cppreference.com/w/c/io/fflush). – Some programmer dude May 05 '20 at 02:15
  • 2
    Otherwise, if Eclipse create a (pseudo) terminal for the output, then output to `stdout` will be *line* buffered, which means output is written when you print a newline. To solve this print a trailing newline in your input prompt, or (again) explicitly flush using `fflush`. – Some programmer dude May 05 '20 at 02:17
  • 1
    @chux-ReinstateMonica adding ' fflush(stdout); ' right before scanf solved it ! thank you – X HOxha May 05 '20 at 02:23

0 Answers0