473,545 Members | 2,009 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C code

Richard can you or anyone else tell me what's wrong with this code. It's
just a simple error I'm sure.

#include <stdio.h>

int main(int argc,char *argv[]){

char name[]="Enter Code -";
printf("%s",&na me);
fflush(stdout);
char input[40];
scanf("%s",&inp ut);
printf("%s",&in put);}

Now if I enter something like niceday it is printed but nice day then only
nice is printed. The space is the question. Is it scanf or printf's string
literal in those functions?

Bill
Nov 11 '07 #1
11 1387
Bill Cunningham wrote:
Richard can you or anyone else tell me what's wrong with this code.
It's just a simple error I'm sure.
Which "Richard"? There are least four regulars I can think of.
#include <stdio.h>

int main(int argc,char *argv[]){

char name[]="Enter Code -";
printf("%s",&na me);
Why do you have an & before name?
fflush(stdout);
char input[40];
scanf("%s",&inp ut);
scanf() stops reading a string at the first whitespace. It's also
unsafe to use in this situation because its input isn't bounded.
printf("%s",&in put);}

Now if I enter something like niceday it is printed but nice day then
only nice is printed. The space is the question.
See above. Use the standard fgets() or one of the fine third-party
"getline" type functions posted here occasionally.


Brian
Nov 12 '07 #2

"Default User" <de***********@ yahoo.comwrote in message
news:5p******** ****@mid.indivi dual.net...
Bill Cunningham wrote:
> Richard can you or anyone else tell me what's wrong with this code.
It's just a simple error I'm sure.

Which "Richard"? There are least four regulars I can think of.
>#include <stdio.h>

int main(int argc,char *argv[]){

char name[]="Enter Code -";
printf("%s",&na me);

Why do you have an & before name?
> fflush(stdout);
char input[40];
scanf("%s",&inp ut);

scanf() stops reading a string at the first whitespace. It's also
unsafe to use in this situation because its input isn't bounded.
> printf("%s",&in put);}

Now if I enter something like niceday it is printed but nice day then
only nice is printed. The space is the question.

See above. Use the standard fgets() or one of the fine third-party
"getline" type functions posted here occasionally.


Brian
OK I see. I was referring to richard heathfield.

Bill
Nov 12 '07 #3
Bill Cunningham wrote, On 11/11/07 23:51:
Richard can you or anyone else tell me what's wrong with this code. It's
just a simple error I'm sure.
I see no reason for you to address this specifically to Richard,
especially as you don't specify which Richard.
#include <stdio.h>

int main(int argc,char *argv[]){

char name[]="Enter Code -";
name is an array of char.
printf("%s",&na me);
So &name is a pointer to an array of char, NOT a pointer to char. Yes,
it points at the same memory location, but it is a *very* important
difference.
fflush(stdout);
char input[40];
scanf("%s",&inp ut);
Same mistake here. Other mistakes include:
1) NEVER use %s as a scanf format specifier. What happens if the name is
too long?
2) ALWAYS check the value returned by the input functions in stdio.h,
otherwise you will not know if anything was obtained. This is especially
important for scanf.
3) Until you know C a lot better don't use scanf. If you want to read a
line use fgets (not gets) and *then* parse it using whatever tool you
want, possibly including sscanf.
printf("%s",&in put);}

Now if I enter something like niceday it is printed but nice day then only
nice is printed. The space is the question. Is it scanf or printf's string
literal in those functions?
It is scanf doing what it is meant to do, i.e. stopping at the first
white space since it was not told to do anything different.

If you don't have a *good* text book I recommend K&R2, if you check the
bibliography of the comp.lang.c FAQ at http://c-faq.com/ you will find
the full details. Also checking the FAQ would be a good idea as it has
answers to a *lot* of simple questions like this.
--
Flash Gordon
Nov 12 '07 #4
"Default User" <de***********@ yahoo.comwrites :
Bill Cunningham wrote:
> Richard can you or anyone else tell me what's wrong with this code.
It's just a simple error I'm sure.

Which "Richard"? There are least four regulars I can think of.
>#include <stdio.h>

int main(int argc,char *argv[]){

char name[]="Enter Code -";
printf("%s",&na me);

Why do you have an & before name?
> fflush(stdout);
char input[40];
scanf("%s",&inp ut);

scanf() stops reading a string at the first whitespace. It's also
Slight correction/clarification : scanf() reads upto the first white
space in THIS instance because of the format qualifier passed in which
is "%s".
unsafe to use in this situation because its input isn't bounded.
> printf("%s",&in put);}

Now if I enter something like niceday it is printed but nice day then
only nice is printed. The space is the question.
Your format string is not upto what you what. Google up some scanf
examples. (The safety part isn't really relevant to your question).
See above. Use the standard fgets() or one of the fine third-party
"getline" type functions posted here occasionally.
Try and stick with the standard functions at this stage IMO. You learn
more.
Brian
Nov 12 '07 #5
On Mon, 12 Nov 2007 00:11:45 +0000, Flash Gordon
<sp**@flash-gordon.me.ukwro te:
>Bill Cunningham wrote, On 11/11/07 23:51:
> Richard can you or anyone else tell me what's wrong with this code. It's
just a simple error I'm sure.

I see no reason for you to address this specifically to Richard,
especially as you don't specify which Richard.
>#include <stdio.h>

int main(int argc,char *argv[]){

char name[]="Enter Code -";

name is an array of char.
> printf("%s",&na me);

So &name is a pointer to an array of char, NOT a pointer to char. Yes,
it points at the same memory location, but it is a *very* important
difference.
> fflush(stdout);
char input[40];
scanf("%s",&inp ut);

Same mistake here. Other mistakes include:
1) NEVER use %s as a scanf format specifier. What happens if the name is
too long?
In other words, the Standard C function scanf is broken? And if broken
has no shades of gray, then scanf is as broken as the Standard C
function gets?

How should a poor programmer like the OP know what is and what is not
broken in the C Standard? And what should the poor programmer use as
alternatives in place of what is broken in the C Standard? In
specific, exactly what should the poor programmer use instead of
scanf?

Regards
--
jay
Nov 12 '07 #6
In article <ji************ *************** *****@4ax.com>, jaysome
<ja*****@hotmai l.comwrote on Monday 12 Nov 2007 11:50 am:
On Mon, 12 Nov 2007 00:11:45 +0000, Flash Gordon
<sp**@flash-gordon.me.ukwro te:
<snip>
>>1) NEVER use %s as a scanf format specifier. What happens if the name
is too long?

In other words, the Standard C function scanf is broken? And if broken
has no shades of gray, then scanf is as broken as the Standard C
function gets?
No. Some uses of scanf() are safer. All possible uses of gets() are
equally unsafe.
How should a poor programmer like the OP know what is and what is not
broken in the C Standard?
Lurk here?
And what should the poor programmer use as
alternatives in place of what is broken in the C Standard?
Depends. Many of the broken or fragile functions have more robust widely
available alternatives.
In specific, exactly what should the poor programmer use instead of
scanf?
fgets() + strto*() or
fgets() + sscanf() or
fgets() + custom_function ()

are some alternative methods that come to mind immediately.

Nov 12 '07 #7
>On Mon, 12 Nov 2007 00:11:45 +0000, Flash Gordon
><sp**@flash-gordon.me.ukwro te:
>>1) NEVER use %s as a scanf format specifier. ...
In article <ji************ *************** *****@4ax.com>
jaysome <ja*****@hotmai l.comwrote:
>In other words, the Standard C function scanf is broken?
Essentially, yes -- but "broken" here has a number of shades of
grey:
>And if broken has no shades of gray, then scanf is as broken as
the Standard C function gets?
The scanf() function *can* be used correctly; it just takes a lot
of work. For instance:

char buf[8], stopc;
int ret;

/*
* Read up to 7 non-white-space characters and a "stop character".
*/
ret = scanf("%7s%c", buf, &stopc);
if (ret == 2) {
/*
* That worked; now inspect the "stop character" to see
* if there were characters after the 7 that would have
* been part of the "non-white-space" characters that
* %s would have scanned, to see if overlong input was
* truncated.
*/
if (isspace(stopc) )
... the non-white-space input was followed by white-space ...
else
... the non-white-space input was overlong ...
} else if (ret == 1) {
/*
* We were able to get up to 7 non-white-space characters,
* after which input must have ended due to EOF or an input
* failure, since the %c directive cannot have a matching
* failure. Decide what to do here.
*/
...
} else {
/*
* We must have ret==EOF, which means we had EOF or an
* input failure. Decide what to do here.
*/
}

In other words, it takes a big-block-o-code just to figure out what
scanf() did and recover from it in case it was not quite what you
meant for it to do. (This also still has the problem that scanf()
is quite bad for "interactiv e input", in that if the user enters
nothing but white-space -- such as a blank line -- the program may
appear to have quit running, since scanf() continues to wait for
input without printing any new prompt to the user.)
>How should a poor programmer like the OP know what is and what is not
broken in the C Standard?
Either read it very carefully, or -- much easier -- read comp.lang.c
for a long time. :-)
>And what should the poor programmer use as
alternatives in place of what is broken in the C Standard?
This depends on what one is using. For instance, as Jacob Navia
is fond of pointing out, ctime() and asctime() both behave badly
for dates in which the year needs more than four digits. To avoid
that, use strftime() instead.
>In specific, exactly what should the poor programmer use instead
of scanf?
Usually the best approach is something like fgets() (including
ggets() and similar) followed by some degree of "manual" picking
apart of the input line, using strtol(), strtod(), and/or sscanf().
The fundamental difference between using scanf() directly and using
sscanf() is that by getting an "input line" first, and only then
throwing it at the "wild and wooly" *scanf() family, you create a
buffer zone -- with a literal buffer, on which you can apply strlen()
first to verify its size -- that insulates your code from most of
the damage "plain scanf()" inflicts in practice (which is to say,
overrunning arrays; leaving behind time bombs; and/or chewing up
scads of input text that you wanted to keep, not throw away).
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 12 '07 #8
On Sun, 11 Nov 2007 22:20:03 -0800, jaysome <ja*****@hotmai l.com>
wrote:
>How should a poor programmer like the OP know what is and what is not
broken in the C Standard?
By reading what the function does and understanding the consequences.
Though I don't know how you define "poor programmer." One who doesn't
read and understand the description of a function?
And what should the poor programmer use as
alternatives in place of what is broken in the C Standard? In
specific, exactly what should the poor programmer use instead of
scanf?
I almost never use scanf. I usually find it easier and clearer to
input as a character string, then take it apart some other way.

--
Al Balmer
Sun City, AZ
Nov 12 '07 #9

"user923005 " <dc*****@connx. comwrote in message
news:11******** **************@ d55g2000hsg.goo glegroups.com.. .
>
>If you just have a string to output with no formatting and you want a
newline at the end of the string, then puts is better than printf()
because you are not going to have to invoke a scanner to analyze a
format.

puts("Hello, world!");

is the equivalent of:

/* Using printf() with a format string with specifier to describe
the arg {note the newline}: */
printf("%s", "Hello, world!\n");

or even:

/* Using printf with only a format string containing no specifiers:
*/
printf("Hello, world!\n"); /* Format string still gets parsed. */

In any case, it's a tempest in a teapot to quibble over which one to
use.
OK thanks. That's probably why some compilers I noticed have optimized
printf to puts.

THanks
Nov 14 '07 #10

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

Similar topics

51
5215
by: Mudge | last post by:
Please, someone, tell me why OO in PHP is better than procedural.
9
3848
by: bigoxygen | last post by:
Hi. I'm using a 3 tier FrontController Design for my web application right now. The problem is that I'm finding to have to duplicate a lot of code for similar functions; for example, listing users, and listing assignments use similar type commands. Is there a "better" way I can organize my code?
4
2419
by: jason | last post by:
Hello. Newbie on SQL and suffering through this. I have two tables created as such: drop table table1; go drop table table2; go
16
3088
by: Dario de Judicibus | last post by:
I'm getting crazy. Look at this code: #include <string.h> #include <stdio.h> #include <iostream.h> using namespace std ; char ini_code = {0xFF, 0xFE} ; char line_sep = {0x20, 0x28} ;
109
5751
by: Andrew Thompson | last post by:
It seems most people get there JS off web sites, which is entirely logical. But it is also a great pity since most of that code is of such poor quality. I was looking through the JS FAQ for any question that identifies the warning signs to look out for, the things that most easily and clearly identify the author of code as something less...
5
4038
by: ED | last post by:
I currently have vba code that ranks employees based on their average job time ordered by their region, zone, and job code. I currently have vba code that will cycle through a query and ranks each employee based on their region, zone, job code and avg job time. (See code below). My problem is that I do not know how to rank the ties. Right...
0
2079
by: Namratha Shah \(Nasha\) | last post by:
Hey Guys, Today we are going to look at Code Access Security. Code access security is a feature of .NET that manages code depending on its trust level. If the CLS trusts the code enough to allow it ro run then it will execute, the code execution depends on the permission provided to the assembly. If the code is not trusted wnough to run...
18
3142
by: Joe Fallon | last post by:
I have some complex logic which is fairly simply to build up into a string. I needed a way to Eval this string and return a Boolean result. This code works fine to achieve that goal. My question is what happens to the dynamically created assembly when the method is done running? Does GC take care of it? Or is it stuck in RAM until the...
37
5922
by: Alan Silver | last post by:
Hello, Newbie here, so please forgive what is probably a basic question ... I see a lot of discussion about "code behind", which if I have understood correctly, means that the script code goes in a separate file from the HTML. Apart from the obvious advantage if you have a separate designer and programmer, are there any other advantages...
171
7601
by: tshad | last post by:
I am just trying to decide whether to split my code and uses code behind. I did it with one of my pages and found it was quite a bit of trouble. I know that most people (and books and articles) like it because you can split the code from the design. That is logical. But if you are the only one working on the code, it seem a little...
0
7664
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. ...
0
7921
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...
1
7437
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...
1
5343
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4958
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3465
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...
1
1900
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1023
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
720
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.