wad wad - 9 months ago 44
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.


  • ./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);


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