1

I am trying to create a simple notepad-like program to type in command prompt. But can't exactly get to the next line by pressing "Enter" using scanf() function.

#include<stdio.h>
void main ()
{
char c;
for(;;){
        scanf("%c", &c);
   if(c == "\n"){
        printf("\n");
        }
   else{
        printf("%c", c);
        }
}
}

(I know scanf() leaves the new char in buffer. I have tried my best to get rid of the problem. Used getch() function after than scanf() but I can't make it work. I have to understand this issue with the scanf() function so I would like to avoid alternatives like using string or something else)

xenteros
  • 15,586
  • 12
  • 56
  • 91
Farhan Fuad
  • 189
  • 2
  • 12
  • 4
    `"\n"` is a string (containing one character), `'\n'` is a character – user253751 Aug 12 '16 at 11:21
  • 3
    You mean to say your compiler din't warn you, or you chose to ignore that? Tell me something, showing up in SO, writing up a question, posting it, waiting for _answers_, is it really _too much fun_? – Sourav Ghosh Aug 12 '16 at 11:25
  • You seem to be misunderstanding what `scanf` does. It does *not* leave the characters it extracts in the buffer. What would be the purpose of that? – Some programmer dude Aug 12 '16 at 11:27
  • Yes it did show a warning "warning: comparison between pointer and integer" but sorry I am a newbie in programming. I couldn't understand what it meant so I just ignored it. Would you please explain the warning? What is pointer? @SouravGhosh – Farhan Fuad Aug 12 '16 at 11:27
  • 1
    @FarhanFuad _"What is pointer"_ if that is your question, **Immediately** go and read a C book. don't try to write code straightaway. – Sourav Ghosh Aug 12 '16 at 11:28
  • If you are suggesting me to use ''\n'' instead of '"\n"' then it's not working @immibis – Farhan Fuad Aug 12 '16 at 11:31
  • 1
    Also for performance reasons, don't use `scanf` to read characters. Declare `c` as an `int` and just `fgetc` instead. Remember to check for `EOF` as well (where declaring the variable as `int` comes into play). – Some programmer dude Aug 12 '16 at 11:35
  • 1
    Lastly, if all you want is to do is print a newline when you have read a newline, you don't need the `if` statement. Printing `c` will print a newline if `c` is a newline. – Some programmer dude Aug 12 '16 at 11:36

2 Answers2

5

Well, you're a little bit unfortunate. This is a popular issue at the beggining of C programming. In C there is a huge difference between '\n' and "n". The first one is single character, so the type of '\n' is char. The other one is a string, and strings in C are char[]s. It means that actually "\n" = ['\n', '\0'] as strings are terminated with '\0' in C.

Another thing is, that scanf ignores leading white signs and a new line is a white sign. If you want to get more characters use fgets instead. For reading single characted, scanf is still ok.

This was widely described in this stack overflow topic.

#include<stdio.h>
void main () {
    char c;
    for(;;){
        scanf("%c", &c);
       if(c == '\n'){
           printf("\n");
       } else{
             printf("%c", c);
       }
    }
}
Community
  • 1
  • 1
xenteros
  • 15,586
  • 12
  • 56
  • 91
0

I think it will be better to use getch() to achieve whatever your trying to achieve.

while(1){
  int i = getch();
  if(i==13) printf("\n");
  // [Maybe an else if ladder for different shortcuts/keys...]
  else printf("%c",i);
}

I know I am late, but I hope this this answer benefits anyone who reads it in the future.