ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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;
}
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.
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.
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?
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
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?
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.
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?
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.