scanf problem

Discussion in 'C Programming' started by Pete, Oct 16, 2004.

  1. Pete

    Pete Guest

    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*/
     
    Pete, Oct 16, 2004
    #1
    1. Advertising

  2. Pete wrote:

    > 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**
     
    Jaroslav Sykora, Oct 16, 2004
    #2
    1. Advertising

  3. 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 wrote:
    >
    >> 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"?
     
    Peter Shaggy Haywood, Oct 19, 2004
    #3
  4. 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"?
     
    Peter Shaggy Haywood, Oct 19, 2004
    #4
  5. Pete

    pete Guest

    Peter Shaggy Haywood wrote:

    > >> char Ptext[8], Key1[8];


    > &Ptext is (char *)[].


    &Ptext is char (*)[]

    --
    pete
     
    pete, Oct 19, 2004
    #5
  6. Pete

    pete Guest

    pete wrote:
    >
    > Peter Shaggy Haywood wrote:
    >
    > > >> char Ptext[8], Key1[8];

    >
    > > &Ptext is (char *)[].

    >
    > &Ptext is char (*)[]


    &Ptext is char (*)[8]

    --
    pete
     
    pete, Oct 19, 2004
    #6
  7. 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 Shaggy Haywood wrote:
    >
    >> >> 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"?
     
    Peter Shaggy Haywood, Oct 21, 2004
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    21
    Views:
    1,212
    Richard Herring
    Apr 20, 2005
  2. code_wrong

    Re: printf scanf problem

    code_wrong, Aug 2, 2003, in forum: C Programming
    Replies:
    1
    Views:
    417
    /dev/null
    Aug 2, 2003
  3. Dan Pop

    Re: printf scanf problem

    Dan Pop, Aug 4, 2003, in forum: C Programming
    Replies:
    0
    Views:
    647
    Dan Pop
    Aug 4, 2003
  4. =?ISO-8859-1?Q?Martin_J=F8rgensen?=

    scanf (yes/no) - doesn't work + deprecation errors scanf, fopen etc.

    =?ISO-8859-1?Q?Martin_J=F8rgensen?=, Feb 16, 2006, in forum: C Programming
    Replies:
    185
    Views:
    3,410
    those who know me have no need of my name
    Apr 3, 2006
  5. =?ISO-8859-1?Q?Martin_J=F8rgensen?=

    difference between scanf("%i") and scanf("%d") ??? perhaps bug inVS2005?

    =?ISO-8859-1?Q?Martin_J=F8rgensen?=, Apr 26, 2006, in forum: C Programming
    Replies:
    18
    Views:
    684
    Richard Bos
    May 2, 2006
Loading...

Share This Page