خرید بک لینک

Vote count: 0

I'm writing a simple program in C, trying to teach myself the language for the first time and also leaing to use my (Mac) Terminal for the first time. However, I keep getting a segmentation fault error when I try to enter a variable (ssn) into scanf() to be saved. I changed the variable from an int to a long hoping to resolve the problem (which I looked up and thought had to do with memory availability/access) but to no avail. I would really appreciate some guidance, thank you! My code is below:

/* A short example program from cs449 C Programming Text */
/* Section 4.13 */
/* Exercise 4-1 */

/**********************************************************
*                                                         *
*     Write a program to print a name, SSN, and DOB       *
*                                                         *
**********************************************************/

#include <stdio.h>
int main()
{
    char name[20];      /* an array of char used to hold a name */
    long ssn;           /* an integer for holding a 9 dig ssn */
    long dob;           /* an integer for holding a date of birth */

    /* for the name */
    printf("Please enter your name: ");
    scanf("%s", name);

    /* for the SSN */
    printf("Please enter your ssn: ");
    scanf("%ld", ssn);

    /* for the date of birth */
    printf("Please enter your date of birth:n");
    printf("Ex. monthdayyear or 041293n");
    scanf("%ld", dob);

    /* final print of user-entered information */
    printf("You are %s bo on %d and your SSN is %d", name, dob, ssn); 

    /* remember to always retu 0 at the end of a main funct! */
    retu(0);
}

asked 2 mins ago

1 Answer

Vote count: 0

You need the following:

scanf("%ld", &ssn);

And then

scanf("%ld", &dob);

answered 19 secs ago

برچسب: نویسنده: استخدام کار تاريخ: يکشنبه 2 خرداد 1395 ساعت: 2:42

صفحه بندی