LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-13-2021, 12:14 PM   #1
FFX
LQ Newbie
 
Registered: Jul 2021
Distribution: Debian, Ubuntu
Posts: 7

Rep: Reputation: Disabled
Problem with a little piece of code in C


Hi,

I am new to C programming and I came across a problem. When I comiple and run the following code I get the output: You have entered +:

Code:
#include <stdio.h>

int main(int argc, char *argv[])
{


if ((*argv[1]) == '+'){


    printf("You have entered +");

}


    return 0;
}

But when I compile and run the following code I get not output at all:

Code:
#include <stdio.h>

int main(int argc, char *argv[])
{


if ((*argv[1]) == '*'){


    printf("You have entered *");

}


    return 0;
}
So what is the problem and how can I dissolve this?

Thanks.

Ben
 
Old 11-13-2021, 12:53 PM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_12{.0|.1}
Posts: 5,777
Blog Entries: 23

Rep: Reputation: 3785Reputation: 3785Reputation: 3785Reputation: 3785Reputation: 3785Reputation: 3785Reputation: 3785Reputation: 3785Reputation: 3785Reputation: 3785Reputation: 3785
Welcome to the Programming forum!

The problem is that when you enter '*' the shell expands that before it is sent to the program, so that *argv[1] is not a literal '*' but is the first value resulting from the expansion and the test fails.

To see what is actually there you might try adding an else clause to show the actual value pointed by argv[1] on failure.

When writing code and some test does not produce the expected result, a good first step for troubleshooting is to write in a way to see what the actual test parameters are when the program runs. Sometimes you may want to place that immediately before the test, and sometimes after or as an alternate when the test fails as I have suggested.

Good luck with your C programming!

Last edited by astrogeek; 11-13-2021 at 01:01 PM.
 
3 members found this post helpful.
Old 11-14-2021, 07:31 AM   #3
FFX
LQ Newbie
 
Registered: Jul 2021
Distribution: Debian, Ubuntu
Posts: 7

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by astrogeek View Post
Welcome to the Programming forum!
Thanks, and thanks for the help.

Ben
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
piece of code Volcano Programming 8 08-04-2009 10:28 AM
is there a piece of c code that can set the terminal window tinieprotonjam Programming 2 01-27-2007 08:20 AM
building shell command piece by piece aunquarra Linux - General 7 05-02-2006 07:43 AM
Unusual piece of bash script code raypen Slackware 2 03-18-2006 11:49 PM
nice piece of code ....HOW DOES IT WORK? deadhead Programming 7 12-07-2003 01:52 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:56 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration