Help on getchar

I wanted to make a simple program that writes chracters in a file but i didnt want to press enter .So i found the getchar which doesnt need enter.If i pass (int) getchar to putc ,in the file it shows a P character.The (int) getchar says it is equal to1734747216 so i do (int) getchar-1734747216 and it gives -62259200 and i cant understand why doesnt it give 0 and what is the P symbol since i didnt press any button?So i found that P symbol is the value of (char*)getchar and i wrote this code

#include <stdio.h>

int main()
{
FILE* text;
char* compare=(char*)getchar;
int temp=0;
text=fopen("C:\\cat.txt","w+");
while(1)
  {
  if ((char*)( temp = (int)getchar )!= compare) 
    {
      putc(temp,text);
    }
  }
  return(0);
}

Also if i change the != with == then the txt has iside it a P symbol as expected but the one with the != still doesnt work as expected
by me :stuck_out_tongue:

  1. getchar is an function, not a variable. So, probably you have to use getchar() instead of getchar. This explains the weird value of getchar.

  2. getchar returns the integer _value_ of character entered. Casting variables compare and temp to (*char) is wrong. Just leave them as int.

  3. The comparison of compare and temp will not work because characters will be analysed by program on after flushing the buffer (<ENTER> was pressed).

The following code writes characters to the file. (You need to press <ENTER> once before entering EOF - <CTRL>D in UNIX <CTRL>Z in Windows)

#include <stdio.h>

int main() {
    FILE* text;
    int temp=0;

    text=fopen("cat.txt","w+");
    while((temp = getchar()) != EOF) {
      putc(temp,text);
    }
    fclose(text);
    return(0);
}

Also, you're wrong. getchar does wait for you to press enter.

I said "The following code writes characters to the file. (You need to press <ENTER> once before entering EOF - <CTRL>D in UNIX <CTRL>Z in Windows)"
Can you, please tell me in what I am wrong?

That was a crosspost, I was replying to the OP, who said: