hope I can get some help here. I'm only at week 3 in CS50 and wanted to play around with command-line arguments a bit before moving on. I got this working (diagonalizing input), but couldn't figure out a way to error-check it. When I enter a ' symbol for instance, it'll just freeze. I tried checking if each char was on the ASCII table (between 31 and 127), but that didn't help any (guess ' should've worked in the first place then anyways).

How would I go about checking that all the inputs are valid, or making the array accept ' as a char? Sorry if this is covered in a later lecture, but I was curious since I couldn't figure out how to error-check it. If anyone has any ideas for a more elegant way to achieving the result, that'd be great too, I'm still pretty new to all of this.


#include <cs50.h>
#include <stdio.h>
#include <string.h>

int main(int argc, string argv[])

// Check string sizes and set size of largest string (ignoring command)
int max_length = 0;
int string_length[argc];

    for (int i = 1; i < argc; i++)
        string_length[i] = strlen(argv[i]);

        if (string_length[i] > max_length)
            max_length = string_length[i];

// Start looping through i'th letter of each string
    for (int i = 0; i < max_length; i++)

// Print appropriate number of spaces
        for (int k = 0; k < i; k++)
            printf(" ");

// Print i'th char of string j or blank if no chars left
        for (int j = 1; j < argc; j++)
            if (string_length[j] > i)
                printf("%c  ",argv[j][i]);
                printf("   ");

2 Answers 2


Your program is actually working as designed. What you're probably overlooking is how the OS shell is handling the inputs to the program. Single quotes, double quotes and the single back quote (the ` and ~ key below the escape key on non-apple keyboards) are treated differently. They would be used, for instance, to identify a complete string. As an example, let's assume your program name is sample. If you were to run it like this,

./sample "aa bb" cc dd

It would treat aa bb as one string and put it all in argv[1], while cc would be treated as the second argument, and dd as the third.

When you said that a single ' symbol was hanging, the system was just waiting for more input. If you were to then type something else in, it would continue to concatenate input to the current argv, until it saw the next '.

If you wanted to actually enter one of these special characters that the shell normally intercepts, you need to type a backslash just before it, \' and you need to do that for each and every occurrence of one of these special characters.

Try doing some of the things I noted above with your existing program and see what happens.

If you want to learn more, try and google escape characters and similar descriptions.

If you like this answer, please mark the question as answered. Let's keep up on the forum housekeeping. ;-)


If you're trying to printf an apostrophe, or other special characters in C, you need to enter the proper escape sequence. You've already seen an example of this...the \n that you have to type in order to enter a newline character. For an apostrophe, the character is \'

To do a check on an apostrophe, you would want to do something like the following:

if ( text[i] == '\'' ) { // do something }



You must log in to answer this question.

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