scanf produces segfault on the line of scanf












-2














#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?










share|improve this question


















  • 2




    printf needs a string first. Then the parameters. like printf("%d",n);
    – KaeptnNemo
    Nov 19 at 21:42






  • 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 at 21:42












  • Thanks! @TypeIA
    – Joseph Seung Jae Dollar
    Nov 19 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 at 21:50






  • 1




    @stark True, i.e. not strictly. But for any attempt to print n it does.
    – Yunnosch
    Nov 20 at 18:35
















-2














#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?










share|improve this question


















  • 2




    printf needs a string first. Then the parameters. like printf("%d",n);
    – KaeptnNemo
    Nov 19 at 21:42






  • 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 at 21:42












  • Thanks! @TypeIA
    – Joseph Seung Jae Dollar
    Nov 19 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 at 21:50






  • 1




    @stark True, i.e. not strictly. But for any attempt to print n it does.
    – Yunnosch
    Nov 20 at 18:35














-2












-2








-2







#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?










share|improve this question













#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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 at 21:40









Joseph Seung Jae Dollar

3822621




3822621








  • 2




    printf needs a string first. Then the parameters. like printf("%d",n);
    – KaeptnNemo
    Nov 19 at 21:42






  • 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 at 21:42












  • Thanks! @TypeIA
    – Joseph Seung Jae Dollar
    Nov 19 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 at 21:50






  • 1




    @stark True, i.e. not strictly. But for any attempt to print n it does.
    – Yunnosch
    Nov 20 at 18:35














  • 2




    printf needs a string first. Then the parameters. like printf("%d",n);
    – KaeptnNemo
    Nov 19 at 21:42






  • 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 at 21:42












  • Thanks! @TypeIA
    – Joseph Seung Jae Dollar
    Nov 19 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 at 21:50






  • 1




    @stark True, i.e. not strictly. But for any attempt to print n it does.
    – Yunnosch
    Nov 20 at 18:35








2




2




printf needs a string first. Then the parameters. like printf("%d",n);
– KaeptnNemo
Nov 19 at 21:42




printf needs a string first. Then the parameters. like printf("%d",n);
– KaeptnNemo
Nov 19 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 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 at 21:42














Thanks! @TypeIA
– Joseph Seung Jae Dollar
Nov 19 at 21:46




Thanks! @TypeIA
– Joseph Seung Jae Dollar
Nov 19 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 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 at 21:50




1




1




@stark True, i.e. not strictly. But for any attempt to print n it does.
– Yunnosch
Nov 20 at 18:35




@stark True, i.e. not strictly. But for any attempt to print n it does.
– Yunnosch
Nov 20 at 18:35












2 Answers
2






active

oldest

votes


















0














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.






share|improve this answer























  • This answer lacks explanation for the segfault. A good one is already described on this page.
    – Yunnosch
    Nov 20 at 18:37



















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;
}





share|improve this answer



















  • 1




    This answer lacks explanation for the segfault. A good one is already described on this page.
    – Yunnosch
    Nov 20 at 18:37










  • Thanks@Yunnosch, I have updated the answer with a line of explanation.
    – Rai
    Nov 21 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 at 11:49











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
});


}
});














draft saved

draft discarded


















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









0














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.






share|improve this answer























  • This answer lacks explanation for the segfault. A good one is already described on this page.
    – Yunnosch
    Nov 20 at 18:37
















0














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.






share|improve this answer























  • This answer lacks explanation for the segfault. A good one is already described on this page.
    – Yunnosch
    Nov 20 at 18:37














0












0








0






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.






share|improve this answer














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.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 at 5:40

























answered Nov 20 at 5:33









Mohammadreza Panahi

2,52221432




2,52221432












  • This answer lacks explanation for the segfault. A good one is already described on this page.
    – Yunnosch
    Nov 20 at 18:37


















  • This answer lacks explanation for the segfault. A good one is already described on this page.
    – Yunnosch
    Nov 20 at 18:37
















This answer lacks explanation for the segfault. A good one is already described on this page.
– Yunnosch
Nov 20 at 18:37




This answer lacks explanation for the segfault. A good one is already described on this page.
– Yunnosch
Nov 20 at 18:37













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;
}





share|improve this answer



















  • 1




    This answer lacks explanation for the segfault. A good one is already described on this page.
    – Yunnosch
    Nov 20 at 18:37










  • Thanks@Yunnosch, I have updated the answer with a line of explanation.
    – Rai
    Nov 21 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 at 11:49
















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;
}





share|improve this answer



















  • 1




    This answer lacks explanation for the segfault. A good one is already described on this page.
    – Yunnosch
    Nov 20 at 18:37










  • Thanks@Yunnosch, I have updated the answer with a line of explanation.
    – Rai
    Nov 21 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 at 11:49














0












0








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;
}





share|improve this answer














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;
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 23 at 4:26

























answered Nov 20 at 5:29









Rai

776218




776218








  • 1




    This answer lacks explanation for the segfault. A good one is already described on this page.
    – Yunnosch
    Nov 20 at 18:37










  • Thanks@Yunnosch, I have updated the answer with a line of explanation.
    – Rai
    Nov 21 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 at 11:49














  • 1




    This answer lacks explanation for the segfault. A good one is already described on this page.
    – Yunnosch
    Nov 20 at 18:37










  • Thanks@Yunnosch, I have updated the answer with a line of explanation.
    – Rai
    Nov 21 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 at 11:49








1




1




This answer lacks explanation for the segfault. A good one is already described on this page.
– Yunnosch
Nov 20 at 18:37




This answer lacks explanation for the segfault. A good one is already described on this page.
– Yunnosch
Nov 20 at 18:37












Thanks@Yunnosch, I have updated the answer with a line of explanation.
– Rai
Nov 21 at 4:19




Thanks@Yunnosch, I have updated the answer with a line of explanation.
– Rai
Nov 21 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 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 at 11:49


















draft saved

draft discarded




















































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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

RAC Tourist Trophy