SolvedGot problems in scanf() program

Posts 1–5 of 5 · Page 1 of 1
Got problems in scanf() program
Hi, me agin.

Here is my program, my first name gets mixed up when the program prints it out, why?


Code:
/* This is a sample program that asks users for some basics data and
prints it on screen in order to show what was entered */

#include <stdio.h>

main()
{
    //Set up the variables that scanf will fill

    char firstInitial;
    char lastInitial;
    int age;
    int favorite_number;

    printf("What is your first name?\n");
    scanf(" %s", &firstInitial);

    printf("What is your last name?\n");
    scanf(" %s", &lastInitial);

    printf("How old are you?\n");
    scanf(" %d", &age);

    printf("What is your favorite number (integer only)?\n");
    scanf(" %d", &favorite_number);


    printf("\nYour intitials are %s.%s. and you are %d years old", &firstInitial, &lastInitial, age);
    printf("\nYour favorite number is %d.\n\n", favorite_number);

    return 0;
}

Also, the book said that you dont need to put & at variables when you use %s(strings) in scanf, but it looks like you do have to put & all the time.
You can not store a string in a single char. Also it's best to avoid scanf. Use streams instead.
Quote Originally Posted by Hell_Demon View Post
You can not store a string in a single char. Also it's best to avoid scanf. Use streams instead.
Yeah man, thanks i forgot about that.
Posts 1–5 of 5 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Tags for this Thread

None

Talk with us