ccompiler.inOnline C Compiler & Docs
C Example Code2026-03-152 min read

Check if a number or string is a palindrome in C with complete tested code, step-by-step arithmetic trace, and string pointer examples.

What is a Palindrome?

A palindrome is a number, word, or sequence that reads the same backward as forward. Examples include:

  • Numbers: 121, 1331, 12321
  • Strings: "radar", "level", "kayak"

1. Checking an Integer Palindrome

To check if an integer is a palindrome, reverse the digits mathematically and compare against the original value:

c (ISO Standard)
#include <stdio.h>
#include <stdbool.h>

bool isNumberPalindrome(int num) {
    if (num < 0) {
        return false; /* Negative numbers are not palindromes */
    }

    int original = num;
    long long reversed = 0;

    while (num > 0) {
        int remainder = num % 10;
        reversed = (reversed * 10) + remainder;
        num /= 10;
    }

    return (original == reversed);
}

int main() {
    int testValues[] = {121, 12321, 1234, 4554};
    int count = 4;

    for (int i = 0; i < count; i++) {
        int val = testValues[i];
        if (isNumberPalindrome(val)) {
            printf("%d is a Palindrome.\n", val);
        } else {
            printf("%d is NOT a Palindrome.\n", val);
        }
    }

    return 0;
}

Expected Output

text (ISO Standard)
121 is a Palindrome.
12321 is a Palindrome.
1234 is NOT a Palindrome.
4554 is a Palindrome.

2. Checking a String Palindrome (Two-Pointer Method)

c (ISO Standard)
#include <stdio.h>
#include <string.h>
#include <stdbool.h>

bool isStringPalindrome(const char *str) {
    int left = 0;
    int right = strlen(str) - 1;

    while (left < right) {
        if (str[left] != str[right]) {
            return false;
        }
        left++;
        right--;
    }
    return true;
}

int main() {
    const char *words[] = {"racecar", "hello", "madam", "compiler"};

    for (int i = 0; i < 4; i++) {
        if (isStringPalindrome(words[i])) {
            printf("\"%s\" is a Palindrome.\n", words[i]);
        } else {
            printf("\"%s\" is NOT a Palindrome.\n", words[i]);
        }
    }

    return 0;
}

Related C Examples & Tutorials