#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
if (argc < 2) {
printf("Incorrect command line arguments.\n");
return 0;
}
for (int i = 0; i < strlen(argv[1]);i++){
printf("%c",argv[1][i]);
}
printf("\n");
}
I have this simple code, however after compiling this is my output:
./a.out 123
123
./a.out (1+2+3)
bash: syntax error near unexpected token `1+2+3'
What is the reason this is happening, and how can I fix it? it seems like its the parenthesis which is messing this up. Thanks
The () characters have special meaning to bash. Use quotes to force bash to treat them as ordinary characters:
./a.out '(1+2+3)'
Related
This segment of code is meant to check if a user has entered only one numeric command-line argument, and return an error code of "1" if this is not the case. I have the code set up so that it first checks if argc is anything other than 2. Unfortunately, I am still receiving Segmentation Faults if no command line argument is entered, and I'm not sure why this code doesn't catch a null amount of command line arguments.
I tried moving the "if (argc !=2)" formula above the entire "for" statement to try and catch the command line argument issue right from the beginning, but I received the same result.
My question is, why am I receiving a Segmentation Fault when no command line argument is provided, and what am I missing to ensure the program doesn't Seg Fault with no command line argument?
Due to course policy, I will only be providing the segment of code in question.
#include <unistd.h>
#include <cs50.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, string argv[])
{
// add'l variables //
int k = atoi(argv[1]);
for (int i = 0; i < strlen(argv[1]); i++)
{
if (argc != 2)
{
printf("Please enter only 1 command-line argument.\n");
return 1;
}
else if (!isdigit(argv[1][i]))
{
printf("Usage: ./caesar key\n");
return 1;
}
}
// add'l code //
Error as shown in Terminal
You must first check the argc before using the argv[1], because argv[1] may not have a valid pointer if argc < 2. A corrected version of your code could be like that:
#include <stdio.h>
#include <ctype.h>
int main(int argc, char *argv[])
{
int i = 0;
if (argc != 2) {
printf("Please enter only 1 command-line argument.\n");
return 1;
}
while (isdigit(argv[1][i]))
++i;
if (argv[1][i] != '\0') {
printf("Usage: ./caesar key\n");
return 1;
}
return 0;
}
I am trying to construct a simple programme to acquaint myself with command line functionality, I seem to have formatted something incorrectly, but I find it very difficult to understand precisely what the resulting error message means. My intenion is to create a programme which checks that all the characters in the command line are digits. Here is my code:
#include <stdio.h>
#include <cs50.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main(int argc, char *argv[])
{
for (int i = 0; i < strlen(argv); i++)
{
if (!isalnum(argv[i]))
{
printf("Please provide letters or numbers only");
}
else
{
printf("Success!");
}
}
}
The error message I receive when I attempt to compile is: c:9:32: error: incompatible pointer types passing 'char **' to parameter of type 'const char *'; dereference with * [-Werror,-Wincompatible-pointer-types].
argv is an array (pointer) of pointers to strings.
isalnum is checking a character, not a whole string, so you will need two loops: to check each strings and to check each characters in the strings.
This code won't get the warning:
#include <stdio.h>
#include <cs50.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main(int argc, char *argv[])
{
for (int c = 1; c < argc; c++) // loop for checking each strings
{
for (int i = 0; i < strlen(argv[c]); i++) // loop for checking each characters in the strings
{
if (!isalnum(argv[c][i]))
{
printf("Please provide letters or numbers only");
}
else
{
printf("Success!");
}
}
}
}
But you may want this code with implovements:
Print the message only once instead of for each characters.
Use size_t for looping until the length of the string.
Call strlen() once before the loop instead of calling in each iteration.
Use puts instead of printf to print newline character at the end of output.
Add return 0; to clarify that the code returns 0.
Remove unnecessary #includes, including non-standard one.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(int argc, char *argv[])
{
int all_ok = 1;
for (int c = 1; c < argc; c++)
{
size_t len = strlen(argv[c]);
for (size_t i = 0; i < len; i++)
{
all_ok = all_ok && isalnum(argv[c][i]);
}
}
if (!all_ok)
{
puts("Please provide letters or numbers only");
}
else
{
puts("Success!");
}
return 0;
}
I am trying to create a cipher coder in C, so far I have the following code. The if statement doesn't work properly and pops up segmentation fault whenever something is inputted whether it be a digit or just a letter. The else statement which is supposed to print out how to use the cipher text only prints it out when there is a space.
#include <stdio.h>
#include <cs50.h>
#include <ctype.h>
#include <string.h>
#include <math.h>
int main(int argc, string argv[]) //im certain the problem is on this line but I dont know where exactly nor do I know how to fix it
{
if (isdigit(argv[1]))
{
string s = get_string("Cipher Text: ");
}
else
{
printf("Usage: ./caesar key\n");
}
}
I'm certain the error has to deal with the string in the command line argument but I'm not to sure on what steps to take to fix it.
This is a very small program to show how you can access arguments of a C program.
It only uses standard C features (not CS50 or special string type).
#include <stdio.h>
int main(int argc, char **argv)
{
int i;
printf("argc=%d\n", argc);
for (i=0; i < argc; i++)
printf("argv[%d]=%s\n", i, argv[i]);
return 0;
}
I'm finding that my very basic code to lowercase a string is sometimes outputting an extra character. If I run vigenere with some inputs, it works correctly:
~/workspace/pset2/vigenere/ $ ./vigenere tweedDLed
tweeddled
But for other inputs, it inserts an extra character at the end:
~/workspace/pset2/vigenere/ $ ./vigenere tweedDLedf
tweeddledfB
or...
~/workspace/pset2/vigenere/ $ ./vigenere bkls33bf
bkls33bfW
What is going on here? I am not finding anything with the debugger as the character array is not displayed. This is my code:
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <cs50.h>
int main(int argc, string argv[]){
if (argc!=2){
return 1;
}
else{
int n = strlen(argv[1]);
char cipherKey[n];
for (int i=0;i<n;i++){
cipherKey[i]=tolower(argv[1][i]);
}
printf("%s\n",cipherKey);
}
}
You need to allocate space for the string termination character, and you need to terminate your string. Otherwise, you leave your string unterminated, printf may read out of the string's bounds yielding undefined behaviour (e.g. in form of "weired" output). You could correct this as follows:
int n = strlen(argv[1]);
char cipherKey[n+1];
for (int i=0;i<n;i++){
cipherKey[i]=tolower((unsigned char)argv[1][i]);
}
cipherKey[n]='\0';
I'm making a very simple program code.
First, it has the option "-num" as 2nd argc. If you input anything in the 3rd argc, the program will simply say that the 3rd argc is entered.
Here are the examples of the inputs and outputs.
Input command line 1:
./test -num
Output 1
-num
Input command line 2:
./test -num AnythingHere
Output 2
-num 3rdArgcEntered
I also want the following command line with sticked argc (-num and AnythingHere are sticked together) to give the same output as Output 2:
./test -numAnythingHere
The output I wish to get is:
-num 3rdArgcEntered
But I obtained:
None
This is the source code I'm currently working on:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (int argc, char *argv[])
{
if (!strcmp(argv[1], "-num"))
{
printf("-num ");
if(argc==3){
printf("3rdArgcEntered");
}
}
else
{
printf("None");
}
printf("\n");
return 0;
}
a little trash..
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (int argc, char *argv[])
{
/* doesnt check when args are not entered */
char *tmp = argv[1];
char buff[5];
/* copy -num, doesnt check */
memcpy(buff, tmp, 4);
buff[4] = '\0';
if (!strcmp(buff, "-num"))
{
printf("-num ");
/* larger than -num */
if(argc==3 || strlen(tmp) > 4 ){
printf("3rdArgcEntered");
}
}
else
{
printf("None");
}
printf("\n");
return 0;
}
Your program can't affect how the arguments are passed; obviously main has already been called at the beginning of main. You need to parse each argument yourself, or use a library that does it for you. For parsing them yourself, you could look at strtok or sscanf, or iterate over the characters. But a more specific library, such as getopt, is generally preferable, since it makes it easy to get your program to behave in a manner consistent with other command-line utilities.