wad wad - 2 months ago 6
C Question

Read an integer from stdin (in C)

I want to write a C program that takes as input an integer, and outputs its square. Here is what I have tried.

However,


  • ./a.out < 3
    outputs 32768, and

  • ./a.out < 4
    also outputs 32768.



Where am I wrong? Thanks.

#include <stdio.h>

int main(){
int myInt;
scanf("%d", &myInt);
printf("%d\n",myInt*myInt);
}

Answer

It looks like what you're trying to do is

echo 4 | ./a.out

the syntax for < is

program < input_file

whereas | is

command_with_output | program