You forgot to allocate memory to the str variable. As soon as you try to access it, for instance in the strcpy function, you get a segmentation fault, because you are accessing address NULL, which is 0, which is illegal. The OS catches it and terminates the program with the error you are seeing.
You need to allocate memory for your string. You can do it dinamically, as the code below:
Security risk
Please note that I am using a 100 characters array, if you write more than 100 characters you will encounter more errors, similar to the segfault but sometimes trickier to debug, as you may overwrite legal parts of the memory and corrupt your data without knowing it).
There are ways to mitigate this, for instance using strncpy(str, "Success!",100); which will write exactly 100 bytes, and stop even if there is no string terminator.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define BUFLEN 100
int main(void)
{
int i = 0, num = 0;
double j = 0;
char *str = NULL;
str = malloc(BUFLEN); // Allocate for 100chars
memset(str,0,BUFLEN); // Init with zeros, for NULL terminated string
printf("Enter the number: ");
scanf("%d", &num);
for (i = 0; i < num; i++) {
j = i / 3 + i;
printf("j = %lf \n", j);
}
strncpy(str, "Success!",BUFLEN);
printf("%s\n", str);
return 0;
}
Or statically
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define BUFLEN 100
int main(void)
{
int i = 0, num = 0;
double j = 0;
char str[BUFLEN] = {0};
printf("Enter the number: ");
scanf("%d", &num);
for (i = 0; i < num; i++) {
j = i / 3 + i;
printf("j = %lf \n", j);
}
strncpy(str, "Success!",BUFLEN);
printf("%s\n", str);
return 0;
}
The malloc approach gives more flexibility and it is something you will need to learn at some point, but it is very risky if you don't know what is happening. So I can suggest first to understand deeply why your code was not working, and secondly to learn the difference between the stack and the heap.