I am very new to coding. Prior to starting CS50, I have had no experience in coding at all, so pardon me if my questions are trivial.
I am trying to write some code for 'Caesar' in Problem Set 2 and this is what I have come up with so far:
#include <cs50.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, string argv[])
{
if (argc == 2 && isdigit(argv[1]))
{
printf("Success\n");
return 0;
}
else
{
printf("Usage: ./caesar key\n");
return 1;
}
}
When I try to execute this code, it gives me a segmentation error as follows:
~/pset1/caesar/ $ make caesar
clang -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wshadow caesar.c -lcrypt -lcs50 -lm -o caesar
~/pset1/caesar/ $ ./caesar 12
Segmentation fault
Below are a few questions I have regarding my code:
Firstly, what is a segmentation error? I believe it was not covered in lecture.
Secondly, may I know if it is "correct" to use the isdigit function in this way? I am trying to figure out if the 2nd string in the argv array is a digit. I also have a hunch that this is what is causing the so-called "segmentation error".
Thirdly, in lecture, the professor's source code stated that we need to return 0s and 1s, but I do not understand this concept clearly. Why, exactly, do we need to return values? Or rather, would there be any difference to my code if I deleted the "return 0" and "return 1" lines?
Thank you all in advance.
EDIT
Since posting this question, I have played around with my code a little and I have gotten rid of the segmentation error. However, my new code accepts alphanumeric arguments and I cannot figure out why.
My updated code is as follows:
#include <cs50.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, string argv[])
{
if (argc == 2)
{
for (int i = 0, n = strlen(argv[1]); i < n; i++)
{
if (isdigit(argv[1][i]))
{
printf("Success\n");
return 0;
}
else
{
printf("Usage: ./caesar key\n");
return 1;
}
}
}
else
{
printf("Usage: ./caesar key\n");
return 1;
}
}
The terminal window shows the following:
~/pset1/caesar/ $ make caesar
clang -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wshadow caesar.c -lcrypt -lcs50 -lm -o caesar
~/pset1/caesar/ $ ./caesar 1hey
Success
In addition to the three questions above which I still wish to be answered, I have three more:
Fourthly, why does my code overlook alphanumeric arguments? I thought that my for loop already checks for whether each character in the 2nd string of argv is a digit? How are letters still accepted then?
Fifthly, in my updated code, I am now trying to use the isdigit function and a for loop to check each character in argv[1]. May I know if my code is written correctly/logically? In particular, does "argv[1][i]" make sense, as in, does the program know that the [1] belongs to the name argv and that the [i] is a variable?
Lastly, how did I get rid of the segmentation error with the changes in my code?
Thank you all in advance again.
if(!isdigit(...) ) error msg and return