scanf problem

P

Pete

Following is a fragment from a program I'm writing. I am trying to enter an
8 bit binary representation from keyboard which is then scanfed into &Ptext
and then again into &Key1. This is not binary just a representation, when I
print each character in the two char arrays, Key1 displays ok but Ptext is
missing the first element. I'm stumped can someone please help. The
following compiles and runs to show my problem.

Thanks in advance.

#include <stdio.h>
#include <string.h>

main() {

char Ptext[8], Key1[8];
int i;

printf("Enter 8 bit binary string Plaintext: ");
scanf("%s", &Ptext);

printf("\n");

printf("Enter 8 bit binary string Key1 test key: ");
scanf("%s", &Key1);

printf("Plaintext is: ");
for(i=0; i<8; i++){
printf("%c", Ptext);
}
printf("\n");

printf("Key No1 is: ");
for(i=0; i<8; i++){
printf("%c", Key1);
}
}/*end main*/
 
J

Jaroslav Sykora

Pete said:
Following is a fragment from a program I'm writing. I am trying to enter
an 8 bit binary representation from keyboard which is then scanfed into
&Ptext and then again into &Key1. This is not binary just a
representation, when I print each character in the two char arrays, Key1
displays ok but Ptext is missing the first element. I'm stumped can
someone please help. The following compiles and runs to show my problem.

Thanks in advance.

#include <stdio.h>
#include <string.h>

main() {

char Ptext[8], Key1[8];
int i;

printf("Enter 8 bit binary string Plaintext: ");
scanf("%s", &Ptext);

printf("\n");

printf("Enter 8 bit binary string Key1 test key: ");
scanf("%s", &Key1);

printf("Plaintext is: ");
for(i=0; i<8; i++){
printf("%c", Ptext);
}
printf("\n");

printf("Key No1 is: ");
for(i=0; i<8; i++){
printf("%c", Key1);
}
}/*end main*/


two errors:
1) you read in 8 characters so you need 9 bytes to store them:
char Ptext[9], Key1[9];
(Key1[9] will overwrite Ptest[0] by \0 in your version)

2) scanf is scanf("%s", char*) so there should be
scanf("%s", Ptext);
scanf("%s", Key1);
because Ptext[int] is char, Ptext is char* and &Ptext is char**
 
P

Peter Shaggy Haywood

Groovy hepcat Jaroslav Sykora was jivin' on Sat, 16 Oct 2004 15:29:17
+0200 in comp.lang.c.
Re: scanf problem's a cool scene! Dig it!
Pete said:
char Ptext[8], Key1[8];

2) scanf is scanf("%s", char*) so there should be
scanf("%s", Ptext);
scanf("%s", Key1);
because Ptext[int] is char, Ptext is char* and &Ptext is char**

Not quite. Ptext is char []. It is converted to char *, but not when
it is the operand of the & operator. &Ptext is (char *)[].

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 
P

Peter Shaggy Haywood

Groovy hepcat Pete was jivin' on Sat, 16 Oct 2004 20:19:25 +1000 in
comp.lang.c.
scanf problem's a cool scene! Dig it!
Following is a fragment from a program I'm writing. I am trying to enter an
8 bit binary representation from keyboard which is then scanfed into &Ptext
and then again into &Key1. This is not binary just a representation, when I
print each character in the two char arrays, Key1 displays ok but Ptext is
missing the first element. I'm stumped can someone please help. The
following compiles and runs to show my problem.

Thanks in advance.

#include <stdio.h>
#include <string.h>

main() {

int main(void) {

It was always a good idea to explicitly specify the return type. But
in C99 it is now mondatory.
char Ptext[8], Key1[8];

As Jaroslav Sykora said, you need 9 elements for each of these
arrays, not 8. Remember, a char array needs (at least) one more
element than the length of the string you intend to store in it. Thus,
a string of 8 characters needs an array of 9 (or more).
int i;

printf("Enter 8 bit binary string Plaintext: ");

There is no guarantee that this prompt will be displayed right away.
So, you should do something that will increase the chances. You can
either a) terminate the output with a newline character or b) flush
stdout with fflush().

fflush(stdout);

Actually, you could also turn off buffering for stdout. But that's
probably a little drastic just to get a prompt to display.
scanf("%s", &Ptext);

As Jaroslav said, this is not right. Since Ptext is an array, and an
array is converted to a pointer to its first element (except when it
is the operand of sizeof or &), what you need here is this:

scanf("%s", Ptext);

However, scanf() isn't a very good function to use for user input. A
better way is to use fgets().
printf("\n");

printf("Enter 8 bit binary string Key1 test key: ");
scanf("%s", &Key1);

Since you have basically duplicated some functionality here
(displaying a prompt and then reading some input), you could put this
functionality in its own function. You might call this function
getbin8(), or something similar. It could take, as arguments, a const
char * for the prompt, and a char * for the input; display the prompt,
making sure to flush the output with fflush(), and read in the input
using fgets(). You could do error checking too, to guard against I/O
errors and to validate the input. And you might use this function like
so:

getbin8("Enter 8 bit binary string Plaintext: ", Ptext));
getbin8("Enter 8 bit binary string Key1 test key: ", Key1);
printf("Plaintext is: ");
for(i=0; i<8; i++){
printf("%c", Ptext);


Why waste cycles interpreting a printf() conversion string? Using
putchar(), fputc() or putc() is easier.

putchar(Ptext);
}
printf("\n");

Again, you could use putchar(), fputc() or putc() here.
printf("Key No1 is: ");
for(i=0; i<8; i++){
printf("%c", Key1);
}


Again you've duplicated functionality. You could easily put this in
its own function too.
And you should make sure the last line of output has a newline at
the end. It is implementation defined whether the last line requires a
newline, so make sure it has one, to be on the safe side. You can do
this by issuing another printf("\n") call (or by using putchar(),
fputc() or putc() as suggested).
Also, you do not have a return statement. This is bad. In C99 main()
is allowed to have no return statement, but it's still probably not a
good idea. And besides, if you had a C99 compiler it would have
complained about your definition of main(), since you didn't
explicitly state the return type.

return 0;
}/*end main*/

You wouldn't need this pointless comment if you had indented your
code properly. Comments that do not add to one's understanding of the
code are useless. And poor formating makes the code ugly and hard to
read. Here's your code reformatted (and with a few of the above
mentioned problems fixed). I'm sure you'll agree that it is much
easier to read:

#include <stdio.h>
#include <string.h>

int main(void)
{
char Ptext[8], Key1[8];
int i;

printf("Enter 8 bit binary string Plaintext: ");
fflush(stdout);
scanf("%s", &Ptext);

printf("\n");

printf("Enter 8 bit binary string Key1 test key: ");
fflush(stdout);
scanf("%s", &Key1);

printf("Plaintext is: ");
for(i=0; i<8; i++)
{
printf("%c", Ptext);
}
printf("\n");

printf("Key No1 is: ");
for(i=0; i<8; i++)
{
printf("%c", Key1);
}
printf("\n");

return 0;
}

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 
P

Peter Shaggy Haywood

Groovy hepcat pete was jivin' on Tue, 19 Oct 2004 04:01:01 GMT in
comp.lang.c.
Re: scanf problem's a cool scene! Dig it!
Peter said:
char Ptext[8], Key1[8];
&Ptext is (char *)[].

&Ptext is char (*)[]

Yep. That's what I meant. Dunno what happened there. Typo.

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top