Re: C code
- From: Keith Thompson <kst-u@xxxxxxx>
- Date: Sun, 11 Nov 2007 18:17:39 -0800
"Bill Cunningham" <nospam@xxxxxxxxx> writes:
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 are you addressing (there are several here), and why?
Just ask the question; somebody will answer it. (In this case it
happens to be somebody not named Richard.)
#include <stdio.h>
int main(int argc,char *argv[]){
char name[]="Enter Code -> ";
printf("%s",&name);
``name'' has type ``char[15]'' (array of 15 char), so ``&name'' has
type ``char (*)[15]'' (pointer to array of 15 char). printf() with a
"%s" format requires an argument of type ``char*''.
It's very likely that your code will work anyway, since a pointer to
an array and a pointer to the first element of an array have, in a
sense, the same value, though they're of different types. But the
correct way to write the above is simply:
printf("%s", name);
But since you don't use the variable ``name'' except in this one
place, you might as well drop the variable and use the value directly:
printf("%s", "Enter code -> ");
or even
printf("Enter code -> ");
(The latter is ok only because the string happens not to contain any
'%' characters.)
fflush(stdout);
char input[40];
Note that the ability to mix statements and declarations within a
block is new in C99; C90 compilers don't allow it, unless they provide
it as an extension. For maximum portability, place all your
declarations first within a block, followed by all your statements.
scanf("%s",&input);
Again, this should be ``input'', not ``&input''.
printf("%s",&input);}
And again.
I thought this was just a code fragment until I noticed the "}" at the
end of the line. More on that below.
main() returns an int, so you should return an int. (C99 makes
falling off the end of main() equivalent to ``return 0;'', but I don't
recommend depending on that.) Add ``return 0;'' before the closing
brace.
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?
The "%s" format for scanf doesn't read a line. It reads a
space-delimited word. Consult your documentation for details.
Also, scanf's "%s" format is extremely dangerous. Your input array is
40 bytes long, but there's nothing to prevent a user of your program
from typing, say, a 100-character word. scanf() will blindly attempt
to store the additional characters past the end of your array, with
arbitrarily bad results.
Since you apparently are trying to read a line rather than a word,
fgets() is probably your best bet. It has some limitations (reading
lines of text in C is surprisingly difficult), but it should be find
for your purposes.
The comp.lang.c FAQ is at <http://www.c-faq.com/>. Section 12 deals
with input/output, but I suggest reading the whole thing.
Now for style issues. Judicious use of whitespace can make your code
much easier to read, and costs practically nothing.
Here's your original code:
#include <stdio.h>
int main(int argc,char *argv[]){
char name[]="Enter Code -> ";
printf("%s",&name);
fflush(stdout);
char input[40];
scanf("%s",&input);
printf("%s",&input);}
And here's how I'd write it, changing nothing but the layout:
#include <stdio.h>
int main(int argc, char *argv[])
{
char name[] = "Enter Code -> ";
printf("%s", &name);
fflush(stdout);
char input[40];
scanf("%s", &input);
printf("%s", &input);
}
Most binary operators should usually be surrounded with blanks.
Commas should always be followed by either a blank or the end of a
line. A closing braces should be on a line by itself. And so on.
The compiler doesn't care about any of this, but anyone reading your
code will. And there are a plethora of different styles for C code
(and many a flame war has been waged on the subject), but I think most
programmers would agree that your program could benefit from some more
spaces.
--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
Looking for software development work in the San Diego area.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
.
- References:
- C code
- From: Bill Cunningham
- C code
- Prev by Date: Re: fseek/fsetpos moving stream position incorrectly
- Next by Date: Re: What's the deal with size_t?
- Previous by thread: Re: C code
- Next by thread: Keith Thompson's messages dont come through
- Index(es):
Relevant Pages
|