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

scanf("%d",&j);

and

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

;?

Recommended Answers

All 6 Replies

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

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)

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;
}

thank you for the anwser

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.