Comment
Author: Admin | 2025-04-28
To a double when passing to printf), you should use the %f format specifier. The %s format specifier is for strings ('\0'-terminated character arrays). answered Mar 1, 2019 at 11:44 PHP Worm...PHP Worm...4,2241 gold badge27 silver badges48 bronze badges Simple meaning of Segmentation fault is that you are trying to access some memory which doesn't belong to you. Segmentation fault occurs when we attempt to read and/or write tasks in a read only memory location or try to freed memory. In other words, we can explain this as some sort of memory corruption. Below I mention common mistakes done by programmers that lead to Segmentation fault.Use scanf() in wrong way(forgot to put &).int num;scanf("%d", num);// must use # instead of numUse pointers in wrong way.int *num; printf("%d",*num); //*num should be correct as num only//Unless You can use *num but you have to point this pointer to valid memory address before accessing it.Modifying a string literal(pointer try to write or modify a read only memory.)char *str; //Stored in read only part of data segmentstr = "GfG"; //Problem: trying to modify read only memory*(str+1) = 'n';Try to reach through an address which is already freed.// allocating memory to num int* num = malloc(8); *num = 100; // de-allocated the space allocated to num free(num); // num is already freed there for it cause segmentation fault*num = 110; Stack Overflow -: Running out of memory on the stackAccessing an array out of bounds'Use wrong format specifiers when using printf() and scanf()' answered Jan
Add Comment