Segmentation fault when casting int to char in C -
i have simple c program. in main, have operation:
int main(){ int thenumber = 9009; printf("%s", (char *)thenumber); } and when run it, gives me seg fault. idea why? doing wrong?
expected output
this code should convert thenumber string , print it. output be:
9009 actual output
a segmentation fault.
this trying print whatever found @ address 9009. seeing operating system not giving program access address (it being used else entirely) operating system raises segmentation fault. read more here: what segmentation fault?
if wanted print value of integer use correct printf command:
int main(){ int thenumber = 9009; printf("%d", thenumber); } note don't need introduce string here achieve this.
Comments
Post a Comment