473,387 Members | 1,374 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

doubts on scanf("%100[^\n]%*[^\n]", buff);

I've read some post on sent by pete and other experts, and have doubts
on following lines of code.

1.
I think the xstr macro isn't needed, is it safe to just use the str
macro in the following scanf statement instead of xstr?

#define STRINGLENGTH 100
#define str(x) # x
#define xstr(x) str(x)

scanf("%" xstr(STRINGLENGTH) "[^\n]%*[^\n]", string);
getchar();

2.
The following scanf is the equivalent of the above scanf. If the users
input 200 characters at terminal, the chars 101~200 remains in stdin
and the next getchar call just read and discard one char, and there
are 99 characters still remains in stdin. Am I right? Masters, can you
teach me.

scanf("%100[^\n]%*[^\n]", buff);
getchar();

Mar 29 '07 #1
1 4717
lovecreatesbea...@gmail.com wrote On 03/29/07 12:03,:
I've read some post on sent by pete and other experts, and have doubts
on following lines of code.

1.
I think the xstr macro isn't needed, is it safe to just use the str
macro in the following scanf statement instead of xstr?

#define STRINGLENGTH 100
#define str(x) # x
#define xstr(x) str(x)

scanf("%" xstr(STRINGLENGTH) "[^\n]%*[^\n]", string);
getchar();
No, changing the final xstr to str is not "safe." To
see why, try printing the string to see what it looks like:

puts("%" xstr(STRINGLENGTH) "[^\n]%*[^\n]");

.... and again with the change you think is "safe." See
a difference?
2.
The following scanf is the equivalent of the above scanf. If the users
input 200 characters at terminal, the chars 101~200 remains in stdin
and the next getchar call just read and discard one char, and there
are 99 characters still remains in stdin. Am I right? Masters, can you
teach me.

scanf("%100[^\n]%*[^\n]", buff);
getchar();
You're ignoring the effect of the "%*[^\n]" part.

--
Er*********@sun.com
Mar 29 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: David | last post by:
Comments on a program called PHP Page Generator? ( http://www.phppagegenerator.com) I received a email offer of $100 and the product appears to allow quick generation of code for SQL web...
26
by: Chris Potter | last post by:
Hello everyone. I am taking my first course in C and in one of my assignments i need to print out an array that could have anywhere from 0 to 100 positive integers in it (a negative integer is...
7
by: Przemo Drochomirecki | last post by:
hi, SCANF is not well described in my books, i'd like to do this stuff: 1) read all numbers from a line, separated by #32 2) read all numbers from a line, separaed by ',' 3) read whole line ...
60
by: K. G. Suarez | last post by:
Hello everyone. I am new to programming and my uncle gave me a copy of "C For Dummies 2nd Edition". I am up to chapter 9 right now. He probably saw me struggling with "The C Programming...
24
by: Apotheosis | last post by:
The problem professor gave us is: Write a program which reads two integer values. If the first is less than the second, print the message "up". If the second is less than the first, print the...
32
by: Fresh Air Rider | last post by:
Hi I understand that ASP.net 2.0 (Whidbey) is going to reduce coding by 70%. Surely this is going to de-skill or dumb down the developer's task and open up the task of web development to less...
19
by: gk245 | last post by:
I have something like this: printf("Enter numbers: "); scanf ("%i", &number); If the user put in a bunch of numbers (like 4568), instead of just one number, i know that you can extract the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.