Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6659519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:02:39+00:00 2026-05-26T02:02:39+00:00

I’m relatively new to C (and completely new to StackOverflow – hey guys!), and

  • 0

I’m relatively new to C (and completely new to StackOverflow – hey guys!), and this segfault has been giving me no surcease of sorrow for the past few hours (DevC++ on a windows machine). It’s just a simple palindrome prime program, but it’s really giving me a hard time. I’m not generally a novice programmer like it seems here, but… Good god. Now I remember why I wanted to get away from C++ and to Python so quickly.

#include <stdio.h>                                           
#include <stdlib.h>                                          
#include <errno.h>                                           
#include <string.h>                                         

FILE *outputFile;                                           
char buffer[81];
char* strrev();                                            
int bytesWritten;                                           
char* strI = 0;   


char *strrev(char str[])
{
   char *p1 =NULL;
   char *p2 =NULL;  

  if (! str || ! *str)
        return str;
  for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2)
  {
        *p1 ^= *p2;
        *p2 ^= *p1;
        *p1 ^= *p2;
  }
  return str;
}

main()                                                       
{ 
    int isPrime(int);

    int i,j;                                                                     
    outputFile = fopen("DD:OUTPUT", "w");                       
    if (outputFile == NULL)                                     
    {                                                           
       printf("open error:   %d/%s\n", errno, strerror(errno));   
       exit(99);                                                  
    }                                                                                                                          
for (i=1; i<15000; i++)                                  
{   

if (isPrime(i)==1)                                    
{                                                      
 bytesWritten = sprintf(buffer,"%d is primepal!\n",i);    
 fwrite(buffer, 1, bytesWritten, outputFile);          
}                                                      
}                                                                         
fclose(outputFile);                                       

return 0;                                                
}    

int isPrime(int myInt)                              
{                                                    

int loop;                                           

for (loop = 2; loop < myInt/2+1; loop++)
  sprintf(strI, "%s%d", 10, myInt);           
  {                                                   
      if (myInt%loop==0 && (atoi(strrev(strI))-myInt)==0)
      {                              
      return 0;                                       
  }                                                   
  return 1;                                         
  }
}                                                    

I apologize ahead of time if this is a dumb question, and the answer is very obvious — but I’ve officially hit the limit where no matter how logical an answer, I’ve been coding the same problem for too long for it to make any sense. And also, segfaults are horrible beasts. Thank you ahead of time for anything you have to offer!

~ Jordan

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T02:02:40+00:00Added an answer on May 26, 2026 at 2:02 am

    The line sprintf(strI, "%s%d", 10, myInt); is likely crashing.

    • You have not allocated any space for strI, it’s defined as char* strI = 0; Make it a char[64] , or a suitable size.
    • You’re giving the wrong arguments to sprintf, “%s%d” says the first parameter should be a string (“%s”) , but you give it an int. Change %s to %d

    Some other issues:

    • Don’t use *p1 ^= *p2; hack to to swap variables, there’s many cases where this does not work. Do it properly with a temp variable.
    • main() calls isPrime(), but there’s no prototype for isPrime at that time. Place int isPrime(int myInt); somewhere before main().
    • The prototype for your strrev function should be char *strrev(char str[]); and not char *strrev()
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.