0

who can tell me what the use of "\n" is? and what the difference between

scanf("%d",&j);

and

scanf("%d\n",&j)

;?

6
Contributors
6
Replies
7
Views
7 Years
Discussion Span
Last Post by Dave Sinkula
-1

In C, the '\n' character is a special type of character called a control character, these are characters that do something other then just display themselves, here is all the ones that I know:

\n = newline
\t = tab
\a = This one rocks, it makes the computer make a beeping sound :D

The difference between those follows:

Say you called the first one, then the user entered 'hello', j would now be 'hello', if you called the second one, j would be 'hello\n' and whenever you printed out j after the second one, it would print out hello followed by a newline

Edited by tomtetlaw: n/a

-1

if i use this in a program its keep on taking the return key
the program not getting terminated, whats the problem?

scanf("%d\n",&j)
0

It discards unwanted white space.
A white-space character in the control string causes scanf() to skip over one or more leading white-space characters in the input stream.

int main()
{
 char ch,ch1;

 printf("Enter two chars : ");
 scanf("%c\n%c",&ch,&ch1);
 printf("\n%c\n%c",ch,ch1);
 return 0;
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.