This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Scanf function not works in C

 
Prock Kumar
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IN C-programming: I am trying to take input (from console) multilines of text. But it does not works . I use this code:



In this code , scanf function works only for single iteration and for rest iteration it is neglected. Please tell me how it works and suggest me the way to take multiline input.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I have added code tags and got rid of the red writing from your post, and you can see how much better it looks.
I tried your code, and had to get rid of the conio include and the getch call, but they didn’t seem to be essential.
What does the ^\n bit mean? Is that some sort of regular expression? When I deleted that, so I had scanf("%s", name[i]); the whole thing seemed to work.
 
Prock Kumar
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Thanks . I knew what you did in reply. My question was: (scanf("%[^\n]s",name[i]);) this code is used for taking input all characters of keyboard except return key. When it find return key at console , it stores all the characters including space in a character array of predefined size. I want to take loop and write this code inside that . As I want, it should take line of text as encounter return key and store in a new character array in each loop. But it does work only at once . Loop works well and iterate as according to the given condition but reads this line in the first turn of iteration and in second or next turn it escapes this line. Would you clear me why?
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don’t know. Sorry. I didn’t know you could use regular expressions in scanf like that.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found a few links via Google; this one says it’s not a regex (obviously it only looks like a regex), but they are using sscanf.
 
Anand Hariharan
Rancher
Posts: 272
C++ Debian VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Methinks this belongs to the forum devoted to C and C++. I do not see posts made here all to often.]

Some suggestions to the OP:

  • Avoid doing I/O in C. If you are learning the language, you could simply hard-code your inputs. If you are in a professional environment, you will very likely be using a good library for I/O.
  • Avoid using scanf. If you have to read input in C, use getchar instead.
  • Absolutely do NOT give a finite sized stack allocated array to be assigned a value from scanf. Lookup BUFFER OVERFLOW on Google. Think of what could happen if the user were to provide a line as input that is longer than 49 characters in length. (Yes, you could specify in your format string to scanf, a length of your input -- but before you respond like so, see above point.)
  • Get into habit of examining a function's return value. In your particular example, scanf's return value would have provided you with very valuable information.


  • To answer your question --

    The scanf's format specifier says "Keep reading all input until you encounter a newline, and store the result in the ith element of name." When scanf reads the input, it encounters the newline the first time. It leaves this newline in the input stream. As a result the newline is not consumed, and hence it is encountered in the second and the third time of your loop. The other lines of input is simply waiting to be read.

    hope this helps,
    - Anand
     
    Consider Paul's rocket mass heater.
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic