scanf - Why can't my C program complete the code after inputing a number? -


i started learning c have no idea why happening.

#include <stdio.h>  int square(int x);  int main(int argc, const char * argv[]) {     printf("enter number");     int usernum;     scanf("%d", &usernum);     int result = square(usernum);     printf("the result %d", result);  }  int square(int x){     int result = x*x;     return result; } 

it ask number nothing happen after input. if take scanf out , put square(10) or something, code run , finish.

it compiles , runs expected me using both gcc , clang... make more clear (as maybe other text getting in way of seeing answer) add new lines outputting stdout:

int main( void ) {     printf("enter number: ");      int usernum;      scanf("%d", &usernum);      int result = square(usernum);      printf("\nthe result is: %d\n", result);      return 0; } 

if testing in terminal (and not piping input) recall scanf (with %d placeholder) read integer until next character not numerical character. on keyboard need type 10 , return (or enter). otherwise, pipe program input file:

10 

... using following command:

./a.out < input.txt 

Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -