LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 06-04-2019, 06:18 PM   #1
anon033
Member
 
Registered: Mar 2019
Posts: 188

Rep: Reputation: 13
Print All Arguments Except the First


I am working on writing my own implementations of a few UNIX tools for practice in C. I am working on echo, but need some help. So far I have this

Code:
#include <stdio.h>

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

        for (i = 0; i < argc; i++)
                printf("\n%s", argv[i]);
        return 0;
}
I only really see one issue with this, when I run the program it prints all arguments. The issue is that this means that it prints argv[0] which is the name of the file. How do I print all the arguments except argv[0]?

# Solved!

Not only did I solve this, but I was able to write the most minimal Echo command

Code:
#include <stdio.h>

int main(int argc, char *argv[])
{
        int i;
        unsigned char meep = 1;

        for (i = 0; i < argc; i++) {
                if (meep == 1) {
                        meep = 0;
                continue;
                }
                printf("%s ", argv[i]);
        }
        return 0;
}
Yes, I know no error handling and such.

Last edited by anon033; 06-08-2019 at 12:03 PM.
 
Old 06-04-2019, 06:25 PM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Think carefully about what your code is doing.

How does the printf statement access the members of argv[]?

Why would that include argv[0]?

How would you then make it not include argv[0]?

Think about that and see if the answer will not materialize before your eyes!

Let us know if it does not!

Last edited by astrogeek; 06-04-2019 at 06:26 PM. Reason: typos
 
Old 06-04-2019, 06:30 PM   #3
anon033
Member
 
Registered: Mar 2019
Posts: 188

Original Poster
Rep: Reputation: 13
Quote:
Originally Posted by astrogeek View Post
Think carefully about what your code is doing.

How does the printf statement access the members of argv[]?

Why would that include argv[0]?

How would you then make it not include argv[0]?

Think about that and see if the answer will not materialize before your eyes!

Let us know if it does not!
I think I have an idea, so echo.c will naturally be echo right? Most people not echo echo, so I can use strncmp to say if the string is echo, then don't print.
 
Old 06-04-2019, 06:34 PM   #4
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Quote:
Originally Posted by FOSSilized_Daemon View Post
I think I have an idea, so echo.c will naturally be echo right? Most people not echo echo, so I can use strncmp to say if the string is echo, then don't print.
No. Think some more.

Did you write that code, or copy and paste? If you understood it well enough to write it then you can understand the solution!

If you did not write it, then this will require a different sort of answer.
 
Old 06-04-2019, 06:37 PM   #5
anon033
Member
 
Registered: Mar 2019
Posts: 188

Original Poster
Rep: Reputation: 13
Quote:
Originally Posted by astrogeek View Post
No. Think some more.

Did you write that code, or copy and paste? If you understood it well enough to write it then you can understand the solution!

If you did not write it, then this will require a different sort of answer.
I wrote the code, but I am still learning and often get confused... I'll keep taking a look, but I am confused. Do you have any links to videos and docs on this sort of thing that you can link for me to look over?
 
Old 06-04-2019, 06:47 PM   #6
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,223

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
In the for loop, initialize i to 1 instead of 0.
 
2 members found this post helpful.
Old 06-04-2019, 06:48 PM   #7
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Quote:
Originally Posted by FOSSilized_Daemon View Post
I wrote the code, but I am still learning and often get confused... I'll keep taking a look, but I am confused. Do you have any links to videos and docs on this sort of thing that you can link for me to look over?
Sorry, I don't do video much as a primary learning tool and would recommend you avoid them as they mostly seem to prepetuate the confusion in my experience! Read and think at your own speed, not the video frame rate - it works better!

There are many good links in the C/C++ Tutorials sticky at top of this forum, I suggest that you explore them.

But as you have not focused on a specific programming concept it would be difficult to recommend a link to anything else!

So I really think you should try to focus on understanding this simple problem in terms of what you already know rather than simply looking for an answer spelled out for you.

Here is a hint: The argv[] array is accessed by the loop variable 'i'. So it is printing argv[0] when i==0. The loop then increments 'i' to the next index and accesses the next argv[i]. What would happen if you started the loop with a different value of 'i'?

Think...

Last edited by astrogeek; 06-04-2019 at 06:57 PM. Reason: typos
 
2 members found this post helpful.
Old 06-07-2019, 08:01 AM   #8
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
@FOSSilized_Daemon,

Why do you call this echo? Because this program prints out the program arguments. Echo is something like on a terminal and whether or not it will echo things which are typed. You typically do see that for instance on a command line, when you type, you see what you're typing. Therefore it is echo'ing the characters.

As far as not understanding what that list contains, you should now know that it contains everything you typed to run the program, including the program name.

As others have told you, the first element of that list is the program name, thus you can optionally skip it, as dugan noted.

A thing which may help here is a more clear statement from you as to why you chose to write this exact program.

If you happened to copy something, even if you modified it, it helps us knowing this.

We're not going to judge if you copied something exactly and were exploring it. Many programmers do that all the time, or they start with something they wrote before and modify it to suit their current needs.

My point here is that you are new to programming, you're testing it out and asking questions. Because of your newness with the topic, some of your questions or how you express yourself may not make sense to us. This is not because you're in a hopelessly uninformed state, just that you don't think the same way, or use the same terminology. Hence my question as to why you use the phrase echo.

In fact, you're questions may be very good ones. As an example, your original question here is fine, "What can I do to not see the program name when I print out all arguments?"

Do you feel you understand the answer here? If not, or even, if so, do you have any follow-up questions?
 
1 members found this post helpful.
Old 06-08-2019, 12:04 PM   #9
anon033
Member
 
Registered: Mar 2019
Posts: 188

Original Poster
Rep: Reputation: 13
Quote:
Originally Posted by astrogeek View Post
Think carefully about what your code is doing.

How does the printf statement access the members of argv[]?

Why would that include argv[0]?

How would you then make it not include argv[0]?

Think about that and see if the answer will not materialize before your eyes!

Let us know if it does not!
I got it, details above
 
Old 06-08-2019, 12:21 PM   #10
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by FOSSilized_Daemon View Post
Not only did I solve this, but I was able to write the most minimal Echo command

Code:
        unsigned char meep = 1;

        for (i = 0; i < argc; i++) {
                if (meep == 1) {
                        meep = 0;
                continue;
That's a pretty complicated way of doing it, for a "minimal" command.
 
2 members found this post helpful.
Old 06-08-2019, 02:42 PM   #11
anon033
Member
 
Registered: Mar 2019
Posts: 188

Original Poster
Rep: Reputation: 13
Quote:
Originally Posted by ntubski View Post
That's a pretty complicated way of doing it, for a "minimal" command.
I just meant in terms of how everyone else (GNU, OpenBSD, FreeBSD etc) has done it. I cut it down some more though
 
Old 06-08-2019, 02:43 PM   #12
anon033
Member
 
Registered: Mar 2019
Posts: 188

Original Poster
Rep: Reputation: 13
Quote:
Originally Posted by rtmistler View Post
@FOSSilized_Daemon,

Why do you call this echo? Because this program prints out the program arguments. Echo is something like on a terminal and whether or not it will echo things which are typed. You typically do see that for instance on a command line, when you type, you see what you're typing. Therefore it is echo'ing the characters.

As far as not understanding what that list contains, you should now know that it contains everything you typed to run the program, including the program name.

As others have told you, the first element of that list is the program name, thus you can optionally skip it, as dugan noted.

A thing which may help here is a more clear statement from you as to why you chose to write this exact program.

If you happened to copy something, even if you modified it, it helps us knowing this.

We're not going to judge if you copied something exactly and were exploring it. Many programmers do that all the time, or they start with something they wrote before and modify it to suit their current needs.

My point here is that you are new to programming, you're testing it out and asking questions. Because of your newness with the topic, some of your questions or how you express yourself may not make sense to us. This is not because you're in a hopelessly uninformed state, just that you don't think the same way, or use the same terminology. Hence my question as to why you use the phrase echo.

In fact, you're questions may be very good ones. As an example, your original question here is fine, "What can I do to not see the program name when I print out all arguments?"

Do you feel you understand the answer here? If not, or even, if so, do you have any follow-up questions?
I understand and solved it thank you It is called Echo, because it is my rewrite of the UNIX command echo.
 
Old 06-08-2019, 07:45 PM   #13
Mechanikx
Member
 
Registered: Jul 2018
Distribution: Slackware
Posts: 351

Rep: Reputation: 258Reputation: 258Reputation: 258
Quote:
Originally Posted by FOSSilized_Daemon View Post
...# Solved!

Not only did I solve this, but I was able to write the most minimal Echo command

Code:
#include <stdio.h>

int main(int argc, char *argv[])
{
        int i;
        unsigned char meep = 1;

        for (i = 0; i < argc; i++) {
                if (meep == 1) {
                        meep = 0;
                continue;
                }
                printf("%s ", argv[i]);
        }
        return 0;
}
Yes, I know no error handling and such.
For my own curiosity, and no offense intended, by why did you choose to introduce a new variable instead of simply initializing 'i' to 1, which is cleaner, like dugan mentioned?
 
2 members found this post helpful.
Old 06-08-2019, 10:36 PM   #14
anon033
Member
 
Registered: Mar 2019
Posts: 188

Original Poster
Rep: Reputation: 13
Smile

Quote:
Originally Posted by Mechanikx View Post
For my own curiosity, and no offense intended, by why did you choose to introduce a new variable instead of simply initializing 'i' to 1, which is cleaner, like dugan mentioned?
I have changed my code to be more simple and thus made that change, but I wrote that prior to checking this thread and didn't see his recommendation.
 
1 members found this post helpful.
  


Reply



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
CUPS Print Server: the print job just prints about 10% of the first page hugocura Linux - General 3 01-21-2019 09:28 AM
wrapper script using getopts to pass all arguments except one threezerous Linux - Newbie 1 04-14-2014 06:08 PM
[SOLVED] using sed to remove all characters on a line except the first sorrymouse Programming 4 10-31-2011 10:33 AM
My HP 720c printer isn't print at all in linux but print in windows. nadavvin Linux - Hardware 9 11-12-2006 09:30 AM
Print-to-file print driver to print PDF Bill Fox Linux - General 3 05-02-2006 04:15 PM

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

All times are GMT -5. The time now is 09:01 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