473,385 Members | 1,753 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,385 software developers and data experts.

advanced printf() and scanf()

I read somewhere that printf and scanf had "advanced features" and they
point to:

scanf("%[^\n]",line); // line is a string

as an example, where scanf() acts like gets()

I try to look of more of this "advanced features" in documentation but
couldn't found any. What "advance features" do you of these functions
(printf and scanf)?

Thanks a lot!

Apr 25 '06 #1
4 11539

Gaijinco wrote:
I read somewhere that printf and scanf had "advanced features" and they
point to:

scanf("%[^\n]",line); // line is a string
This tells `scanf()` to read everything until the newline. Newline is
left in the input buffer, and may be read by subsequent calls.
as an example, where scanf() acts like gets()
Making anything behave like `gets()` must be EVIL!

Think of what happens when there's more charaters in the input buffer
than you allowed for in your string buffer.

You're better off using `fgets()` and `sscanf()`.
I try to look of more of this "advanced features" in documentation but
couldn't found any.
Try the C Standard. You should be able to download the current version
of C99 plus TCs as N1124.pdf. Google for it.
What "advance features" do you of these functions (printf and scanf)?


In my line of work, exactly none (don't even use the functions, let
alone their "advanced" features).

Apr 25 '06 #2
In article <11**********************@j33g2000cwa.googlegroups .com>,
Gaijinco <ga******@gmail.com> wrote:
I try to look of more of this "advanced features" in documentation but
couldn't found any. What "advance features" do you of these functions
(printf and scanf)?


I don't know if you'd call it an "advanced feature", but I have seen
examples that would have been a lot simpler if someone had known about
the use of "*" as the precision modifier.

Suppose that "s" points to an array of characters whose length is
"len" - a representation of a string that does not use null-termination.
This can be printed with

printf("%.*s", len, s);

but I have seen programs where the author malloced a buffer, copied
the string, and null-terminated it so that he could use "%s".

-- Richard
Apr 25 '06 #3
In article <11**********************@t31g2000cwb.googlegroups .com>,
Vladimir Oka <no****@btopenworld.com> wrote:
Gaijinco wrote:
I read somewhere that printf and scanf had "advanced features" and they
point to: scanf("%[^\n]",line); // line is a string
This tells `scanf()` to read everything until the newline. Newline is
left in the input buffer, and may be read by subsequent calls.

as an example, where scanf() acts like gets()


As the scanf() form leaves the newline in the input stream, but
the gets() form does not, the two are not equivilent.

You could use:

scanf("%[^\n]\n",line);

but you should be sure you understand the result you would get
if end of file occurs before you encounter a \n .
--
There are some ideas so wrong that only a very intelligent person
could believe in them. -- George Orwell
Apr 25 '06 #4

In article <e2**********@canopus.cc.umanitoba.ca>, ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
Gaijinco wrote:
as an example, where scanf() acts like gets()


As the scanf() form leaves the newline in the input stream, but
the gets() form does not, the two are not equivilent.

You could use:

scanf("%[^\n]\n",line);

but you should be sure you understand the result you would get
if end of file occurs before you encounter a \n .


Also, since \n is a whitespace directive for the fscanf family, it
will consume not only the newline (if it exists) but any following
whitespace. If this scanf is used in a loop, for example, it will
remove leading whitespace from all lines after the first. So again,
this is not equivalent to gets().

(It *is* similar to gets() in that, as presented, it can overflow
the line object. There should be a maximum size specifier on that
conversion directive.)

--
Michael Wojcik mi************@microfocus.com

Even though there may be some misguided critics of what we're trying
to do, I think we're on the wrong path. -- Reagan
Apr 27 '06 #5

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

Similar topics

4
by: sushant | last post by:
hi why do we use '&' operator in scanf like scanf("%d", &x); but why not in printf() like printf("%d" , x); thnx in advance sushant
12
by: drM | last post by:
I have looked at the faq and queried the archives, but cannot seem to be able to get this to work. It's the usual factorial recursive function, but that is not the problem. It hangs after the user...
6
by: Dawn Minnis | last post by:
Hi (running Win xp and developing using Miracle C. Running applications in windows command prompt) I'm new to the group so be gentle with me. I am currently writing a C program to perform...
8
by: Gregc. | last post by:
Hi Sorry to bother you gusy with such a basic question, but I am working on a conversion program. I've got most of it down, but I am having trouble with the output: *Dollar Conversion...
25
by: Podrzut_z_Laweczki | last post by:
Hello, I have question (or 2 :)). Is that true that for a large data using scanf/printf instead of cin/cout makes that the program runs faster? And if it is, is it legal to mix scanf/printf with...
34
by: Old Wolf | last post by:
Is there any possible situation for printf where %hd causes a different result to %d, and the corresponding argument was of type 'short int' ?
1
by: linq936 | last post by:
Hi, I read in many places that the string to be outputted by printf() must be ending with newline, for example, it should be printf("Hello World.\n"); instead of printf("Hello World.");
15
by: pereges | last post by:
have i written this program correctly ? it is giving me correct output but i am little suspicious in the following two statements - scanf("%d", &ptr->data) and printf("%d\n", ptr->data). ...
11
by: sunnyalways4u2000 | last post by:
hello sir, Sir will please tell me the exact difference between C and advanced C...what are the extra features or funcions...etc added in this advanced one.
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...

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.