I have been working on Caesar for some time now and tried to use different loops but I am getting Segmentation Errors.

#include <stdio.h>
#include <cs50.h>
#include <stdlib.h>
#include <ctype.h>

int main (int argc, string argv[])
    if (argc != 2 || !isdigit(argv[1]))
        printf("Usage: ./caesar key");
        return 1;
        string plaintext = get_string("plaintext: ");
        return 0;

I think this code should work but when I input something like ./caesar 2 I get a segmentation error. Can someone pin point what exactly is wrong?

1 Answer 1


The isdigit() function can only take a single character as input, but argv[1] is a string, complete with end of string marker. If you pass a string to isdigit() or any of it's cousins like isalpha, etc., it will always generate a seg fault.

If you want to check a string to see if it's really all digits, you need to use a for loop in combination with a correctly formatted call that only references one char of the string at a time, as in isdigit(argv[1][i]).

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .