What is a segmentation error (kernel is reset)?

I am trying to write a C program in linux with sqrt argument, here is 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 entering my input at the shell> prompt, gcc gives me the following error:

 Segmentation fault (core dumped) 
+53
c
Oct 28 '13 at 17:46
source share
1 answer

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

The first problem is with your main arguments. The main function should be int main(int argc, char *argv[]) , and you should check that argc at least 2 before accessing argv[1] .

In addition, since you pass from float to printf (which, by the way, converts to double when passed to printf ), you must use the %f format specifier. The %s format specifier is for strings ( '\0' -terminated character arrays).

+80
Oct 28 '13 at 17:49
source share



All Articles