sourcecode

what is Segmentation fault (core dumped)?

copyscript 2022. 8. 12. 23:33
반응형

what is Segmentation fault (core dumped)?

I am trying to write a C program in linux that having sqrt of the argument, Here's the code:

#include<stdlib.h>
#include<stdio.h>
#include<math.h>

int main(char *argv[]){
    float k;
    printf("this is consumer\n");
    k=(float)sqrt(atoi(argv[1]));
    printf("%s\n",k);
    return 0;
}

After I type in my input at the "shell> " prompt, gcc gives me the following error:

Segmentation fault (core dumped)

"Segmentation fault" means that you tried to access memory that you do not have access to.

첫 번째 문제는 당신의 주장입니다.main.그main함수는 다음과 같아야 한다.int main(int argc, char *argv[])를 체크해 주세요.argc액세스하기 전에 적어도2개argv[1].

그리고, 당신이 지나가고 있으니까float로.printf(그건 그렇고, 이 경우엔,double로 넘어가면printf)를 사용합니다.%f포맷 지정자.%sformat specificate는 문자열용입니다.'\0'- 종단 문자 배열).

ReferenceURL : https://stackoverflow.com/questions/19641597/what-is-segmentation-fault-core-dumped

반응형