【发布时间】:2016-05-21 19:26:45
【问题描述】:
我正在用 C 编写一个简单的程序,尝试第一次自学该语言,并第一次学习使用我的 (Mac) 终端。 但是,当我尝试将变量 (ssn) 输入 scanf() 以进行保存时,我不断收到分段错误错误。我将变量从 int 更改为 long 希望解决问题(我查找并认为与内存可用性/访问有关)但无济于事。 我真的很感激一些指导,谢谢! 我的代码如下:
/* 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 041293\n");
scanf("%ld", dob);
/* final print of user-entered information */
printf("You are %s born on %d and your SSN is %d", name, dob, ssn);
/* remember to always return 0 at the end of a main funct! */
return(0);
}
【问题讨论】:
-
@BLUEPIXY 感谢您发现这个错误!
-
@BLUEPIXY,把我的答案复制到 cmets :-)
-
@BLUEPIXY 非常感谢!
-
@BLUEPIXY,我只是在开玩笑,伙计,因为你说的是完全我在回答中所说的即使是在相同的顺序:)
-
回滚。您不得编辑使现有答案无用的问题!
标签: c terminal segmentation-fault