473,796 Members | 2,625 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 11577

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************ **********@j33g 2000cwa.googleg roups.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************ **********@t31g 2000cwb.googleg roups.com>,
Vladimir Oka <no****@btopenw orld.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**********@c anopus.cc.umani toba.ca>, ro******@ibd.nr c-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
17207
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
1822
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 enters a number. However, as I indicate, if one adds something else after the number, the function proceeds and finishes successfully. I would appreciate some helpful hints. thanks in advance. >>>>>>>>>
6
2824
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 matrix by matrix (mxm) and matrix by vector (mxv) multiplication, so obviously one of my first considerations is to ask the user if they want an mxm or mxv multiplication performed. I have written the code below (this is a working snippet of the...
8
4973
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 Problem*/ /*--------------------------------------------------------------------------------------------------------------------*/ /*Head Files*/...
25
9734
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 C++ code? Program should execute below 1 sec and the hint is to use scanf/printf.
34
15999
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
8518
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
7070
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). /********** LINK LIST **********/ #include <stdio.h>
11
5155
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
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10459
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10237
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10187
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10018
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9055
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5446
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5578
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3735
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.