wad wad - 1 year ago 73
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);

Answer Source

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download