scanf produces segfault on the line of scanf
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
printf(n);
return 0;
}
This is the code I have, and I'm so dumbed out probably missing out on a very basic mistake. It segfaults on the line of scanf.
How would I resolve this problem?
c scanf
|
show 1 more comment
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
printf(n);
return 0;
}
This is the code I have, and I'm so dumbed out probably missing out on a very basic mistake. It segfaults on the line of scanf.
How would I resolve this problem?
c scanf
2
printf needs a string first. Then the parameters. likeprintf("%d",n);
– KaeptnNemo
Nov 19 '18 at 21:42
7
No it doesn't. It segfaults onprintf(n)
. The first argument toprintf()
must be a pointer to a format string. You're passing in the numeric value ofn
as a pointer (and your compiler would almost certainly warn about this if compiled with a sufficiently sensitive warning level such as-Wall
withgcc
).
– TypeIA
Nov 19 '18 at 21:42
Thanks! @TypeIA
– Joseph Seung Jae Dollar
Nov 19 '18 at 21:46
3
The text-book reason for downvotes on questions is "This question does not show any research effort ...". Could you demonstrate your research effort, by explaining how even while only reading the printf spec you could miss the need for more than one parameter?
– Yunnosch
Nov 19 '18 at 21:50
1
@stark True, i.e. not strictly. But for any attempt to printn
it does.
– Yunnosch
Nov 20 '18 at 18:35
|
show 1 more comment
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
printf(n);
return 0;
}
This is the code I have, and I'm so dumbed out probably missing out on a very basic mistake. It segfaults on the line of scanf.
How would I resolve this problem?
c scanf
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
printf(n);
return 0;
}
This is the code I have, and I'm so dumbed out probably missing out on a very basic mistake. It segfaults on the line of scanf.
How would I resolve this problem?
c scanf
c scanf
asked Nov 19 '18 at 21:40
Joseph Seung Jae DollarJoseph Seung Jae Dollar
3822621
3822621
2
printf needs a string first. Then the parameters. likeprintf("%d",n);
– KaeptnNemo
Nov 19 '18 at 21:42
7
No it doesn't. It segfaults onprintf(n)
. The first argument toprintf()
must be a pointer to a format string. You're passing in the numeric value ofn
as a pointer (and your compiler would almost certainly warn about this if compiled with a sufficiently sensitive warning level such as-Wall
withgcc
).
– TypeIA
Nov 19 '18 at 21:42
Thanks! @TypeIA
– Joseph Seung Jae Dollar
Nov 19 '18 at 21:46
3
The text-book reason for downvotes on questions is "This question does not show any research effort ...". Could you demonstrate your research effort, by explaining how even while only reading the printf spec you could miss the need for more than one parameter?
– Yunnosch
Nov 19 '18 at 21:50
1
@stark True, i.e. not strictly. But for any attempt to printn
it does.
– Yunnosch
Nov 20 '18 at 18:35
|
show 1 more comment
2
printf needs a string first. Then the parameters. likeprintf("%d",n);
– KaeptnNemo
Nov 19 '18 at 21:42
7
No it doesn't. It segfaults onprintf(n)
. The first argument toprintf()
must be a pointer to a format string. You're passing in the numeric value ofn
as a pointer (and your compiler would almost certainly warn about this if compiled with a sufficiently sensitive warning level such as-Wall
withgcc
).
– TypeIA
Nov 19 '18 at 21:42
Thanks! @TypeIA
– Joseph Seung Jae Dollar
Nov 19 '18 at 21:46
3
The text-book reason for downvotes on questions is "This question does not show any research effort ...". Could you demonstrate your research effort, by explaining how even while only reading the printf spec you could miss the need for more than one parameter?
– Yunnosch
Nov 19 '18 at 21:50
1
@stark True, i.e. not strictly. But for any attempt to printn
it does.
– Yunnosch
Nov 20 '18 at 18:35
2
2
printf needs a string first. Then the parameters. like
printf("%d",n);
– KaeptnNemo
Nov 19 '18 at 21:42
printf needs a string first. Then the parameters. like
printf("%d",n);
– KaeptnNemo
Nov 19 '18 at 21:42
7
7
No it doesn't. It segfaults on
printf(n)
. The first argument to printf()
must be a pointer to a format string. You're passing in the numeric value of n
as a pointer (and your compiler would almost certainly warn about this if compiled with a sufficiently sensitive warning level such as -Wall
with gcc
).– TypeIA
Nov 19 '18 at 21:42
No it doesn't. It segfaults on
printf(n)
. The first argument to printf()
must be a pointer to a format string. You're passing in the numeric value of n
as a pointer (and your compiler would almost certainly warn about this if compiled with a sufficiently sensitive warning level such as -Wall
with gcc
).– TypeIA
Nov 19 '18 at 21:42
Thanks! @TypeIA
– Joseph Seung Jae Dollar
Nov 19 '18 at 21:46
Thanks! @TypeIA
– Joseph Seung Jae Dollar
Nov 19 '18 at 21:46
3
3
The text-book reason for downvotes on questions is "This question does not show any research effort ...". Could you demonstrate your research effort, by explaining how even while only reading the printf spec you could miss the need for more than one parameter?
– Yunnosch
Nov 19 '18 at 21:50
The text-book reason for downvotes on questions is "This question does not show any research effort ...". Could you demonstrate your research effort, by explaining how even while only reading the printf spec you could miss the need for more than one parameter?
– Yunnosch
Nov 19 '18 at 21:50
1
1
@stark True, i.e. not strictly. But for any attempt to print
n
it does.– Yunnosch
Nov 20 '18 at 18:35
@stark True, i.e. not strictly. But for any attempt to print
n
it does.– Yunnosch
Nov 20 '18 at 18:35
|
show 1 more comment
2 Answers
2
active
oldest
votes
Use of printf()
like bellow :
printf("%d", n);
Probably you did a mistake in your code
before using scanf
. For example it may be occurred if you copy a string in a uninitialized array that pointing nowhere in memory.
This answer lacks explanation for the segfault. A good one is already described on this page.
– Yunnosch
Nov 20 '18 at 18:37
add a comment |
The segmentation fault is not at the scanf
, but at the printf
. As @TypeIA has mentioned in the comments section, this is because the printf
expects to have a pointer to the format string and not the integer itself. To do this, just how you use the format specifier %d
while accepting an integer as input through scanf
, you need to use the same while printing using printf
., i.e,
printf("%d",n);
Notice the %d
before n
.
UPDATED CODE:
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
printf("%d",n);
return 0;
}
1
This answer lacks explanation for the segfault. A good one is already described on this page.
– Yunnosch
Nov 20 '18 at 18:37
Thanks@Yunnosch, I have updated the answer with a line of explanation.
– Rai
Nov 21 '18 at 4:19
You could probably improve the answer by using the better, more detailed explanation in the comments, giving credits of course and maybe even asking for permission.
– Yunnosch
Nov 21 '18 at 11:49
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53383029%2fscanf-produces-segfault-on-the-line-of-scanf%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Use of printf()
like bellow :
printf("%d", n);
Probably you did a mistake in your code
before using scanf
. For example it may be occurred if you copy a string in a uninitialized array that pointing nowhere in memory.
This answer lacks explanation for the segfault. A good one is already described on this page.
– Yunnosch
Nov 20 '18 at 18:37
add a comment |
Use of printf()
like bellow :
printf("%d", n);
Probably you did a mistake in your code
before using scanf
. For example it may be occurred if you copy a string in a uninitialized array that pointing nowhere in memory.
This answer lacks explanation for the segfault. A good one is already described on this page.
– Yunnosch
Nov 20 '18 at 18:37
add a comment |
Use of printf()
like bellow :
printf("%d", n);
Probably you did a mistake in your code
before using scanf
. For example it may be occurred if you copy a string in a uninitialized array that pointing nowhere in memory.
Use of printf()
like bellow :
printf("%d", n);
Probably you did a mistake in your code
before using scanf
. For example it may be occurred if you copy a string in a uninitialized array that pointing nowhere in memory.
edited Nov 20 '18 at 5:40
answered Nov 20 '18 at 5:33


Mohammadreza PanahiMohammadreza Panahi
2,56721432
2,56721432
This answer lacks explanation for the segfault. A good one is already described on this page.
– Yunnosch
Nov 20 '18 at 18:37
add a comment |
This answer lacks explanation for the segfault. A good one is already described on this page.
– Yunnosch
Nov 20 '18 at 18:37
This answer lacks explanation for the segfault. A good one is already described on this page.
– Yunnosch
Nov 20 '18 at 18:37
This answer lacks explanation for the segfault. A good one is already described on this page.
– Yunnosch
Nov 20 '18 at 18:37
add a comment |
The segmentation fault is not at the scanf
, but at the printf
. As @TypeIA has mentioned in the comments section, this is because the printf
expects to have a pointer to the format string and not the integer itself. To do this, just how you use the format specifier %d
while accepting an integer as input through scanf
, you need to use the same while printing using printf
., i.e,
printf("%d",n);
Notice the %d
before n
.
UPDATED CODE:
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
printf("%d",n);
return 0;
}
1
This answer lacks explanation for the segfault. A good one is already described on this page.
– Yunnosch
Nov 20 '18 at 18:37
Thanks@Yunnosch, I have updated the answer with a line of explanation.
– Rai
Nov 21 '18 at 4:19
You could probably improve the answer by using the better, more detailed explanation in the comments, giving credits of course and maybe even asking for permission.
– Yunnosch
Nov 21 '18 at 11:49
add a comment |
The segmentation fault is not at the scanf
, but at the printf
. As @TypeIA has mentioned in the comments section, this is because the printf
expects to have a pointer to the format string and not the integer itself. To do this, just how you use the format specifier %d
while accepting an integer as input through scanf
, you need to use the same while printing using printf
., i.e,
printf("%d",n);
Notice the %d
before n
.
UPDATED CODE:
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
printf("%d",n);
return 0;
}
1
This answer lacks explanation for the segfault. A good one is already described on this page.
– Yunnosch
Nov 20 '18 at 18:37
Thanks@Yunnosch, I have updated the answer with a line of explanation.
– Rai
Nov 21 '18 at 4:19
You could probably improve the answer by using the better, more detailed explanation in the comments, giving credits of course and maybe even asking for permission.
– Yunnosch
Nov 21 '18 at 11:49
add a comment |
The segmentation fault is not at the scanf
, but at the printf
. As @TypeIA has mentioned in the comments section, this is because the printf
expects to have a pointer to the format string and not the integer itself. To do this, just how you use the format specifier %d
while accepting an integer as input through scanf
, you need to use the same while printing using printf
., i.e,
printf("%d",n);
Notice the %d
before n
.
UPDATED CODE:
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
printf("%d",n);
return 0;
}
The segmentation fault is not at the scanf
, but at the printf
. As @TypeIA has mentioned in the comments section, this is because the printf
expects to have a pointer to the format string and not the integer itself. To do this, just how you use the format specifier %d
while accepting an integer as input through scanf
, you need to use the same while printing using printf
., i.e,
printf("%d",n);
Notice the %d
before n
.
UPDATED CODE:
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
printf("%d",n);
return 0;
}
edited Nov 23 '18 at 4:26
answered Nov 20 '18 at 5:29


RaiRai
820418
820418
1
This answer lacks explanation for the segfault. A good one is already described on this page.
– Yunnosch
Nov 20 '18 at 18:37
Thanks@Yunnosch, I have updated the answer with a line of explanation.
– Rai
Nov 21 '18 at 4:19
You could probably improve the answer by using the better, more detailed explanation in the comments, giving credits of course and maybe even asking for permission.
– Yunnosch
Nov 21 '18 at 11:49
add a comment |
1
This answer lacks explanation for the segfault. A good one is already described on this page.
– Yunnosch
Nov 20 '18 at 18:37
Thanks@Yunnosch, I have updated the answer with a line of explanation.
– Rai
Nov 21 '18 at 4:19
You could probably improve the answer by using the better, more detailed explanation in the comments, giving credits of course and maybe even asking for permission.
– Yunnosch
Nov 21 '18 at 11:49
1
1
This answer lacks explanation for the segfault. A good one is already described on this page.
– Yunnosch
Nov 20 '18 at 18:37
This answer lacks explanation for the segfault. A good one is already described on this page.
– Yunnosch
Nov 20 '18 at 18:37
Thanks@Yunnosch, I have updated the answer with a line of explanation.
– Rai
Nov 21 '18 at 4:19
Thanks@Yunnosch, I have updated the answer with a line of explanation.
– Rai
Nov 21 '18 at 4:19
You could probably improve the answer by using the better, more detailed explanation in the comments, giving credits of course and maybe even asking for permission.
– Yunnosch
Nov 21 '18 at 11:49
You could probably improve the answer by using the better, more detailed explanation in the comments, giving credits of course and maybe even asking for permission.
– Yunnosch
Nov 21 '18 at 11:49
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53383029%2fscanf-produces-segfault-on-the-line-of-scanf%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
printf needs a string first. Then the parameters. like
printf("%d",n);
– KaeptnNemo
Nov 19 '18 at 21:42
7
No it doesn't. It segfaults on
printf(n)
. The first argument toprintf()
must be a pointer to a format string. You're passing in the numeric value ofn
as a pointer (and your compiler would almost certainly warn about this if compiled with a sufficiently sensitive warning level such as-Wall
withgcc
).– TypeIA
Nov 19 '18 at 21:42
Thanks! @TypeIA
– Joseph Seung Jae Dollar
Nov 19 '18 at 21:46
3
The text-book reason for downvotes on questions is "This question does not show any research effort ...". Could you demonstrate your research effort, by explaining how even while only reading the printf spec you could miss the need for more than one parameter?
– Yunnosch
Nov 19 '18 at 21:50
1
@stark True, i.e. not strictly. But for any attempt to print
n
it does.– Yunnosch
Nov 20 '18 at 18:35