Ex10 the right format for ' char * '

I was trying to add to this printf() the command to print argv[i] like so:
printf("%d: %c is not a vowel. letter: %c\n", i, letter, argv[i]);

Format %c didn’t work. It gives an error:

I couldn’t find which is the right format for ‘char *’ so I just tried every letter.
Two letters works: %s and %p
Meaning it will compile and run the program. But it just prints random stuff:
%s gives:


notice that between letter 3 and 4 there is 33 which gives some large output…
%p gives:

anyhow. I don’t think I manage to print argv[i].
An explanation for what happened here and how to actually print argv[i] would be very appreciated.

if you do argv[1][i] and call it with an argument( any letter) , it works.

I tried it and here are the results:


It looks like the serial number of the letters. even numbers and signs like $%# have a serial number. Is this what I should be getting? Is this argv?

Copy me all the code you have in that script and paste it here. Don’t forget to put 3 ` before and after.


What I don’t get is why you need this?
letter is argv[1][i]
Why are you calling argv[1][i] again?

Well. I’m trying to figure out what is argv[i]. printing it is the easiest way for me to do so. if I could print it…

It is just out of curiosity I guess…

1 Like

argv[1][i] is the same as letter, so argv[0] would be the name of your script, argv[1] holds all the values you pass as arguments to the script.
argc holds the number of arguments, script name included.
In that script the first if checks if the number of arguments is not equal to 2, because the program expects exactly 2 arguments: the name of the script at position [0] and a string made of 1 or more characters at position 1. Try doing ./ex10 erhdy grt - you have 3 arguments, you’ll get an error. Try doing ./ex10, you have only 1 argument at position [0], you’ll get an error.
If you do smth like ./ex10 ghyrb, you’re good. So the ghyrb int he last example is the second argument at position [1] and now you check each character in it to see if it’s a vowel or not.(You remember a string in C is a list of characters, so you can access each character in it by its index.)
g in ghyrb is at position [0] of the string. So you have argv[1][0] == g
Can you see it now?

You use ‘%s’ to print a whole string. So %s is for argv[i], and %c is for argv[i][1]. Try that, @io_io has a lot of good info but I think that little bit was missing.

2 Likes

Totally forgot that part, got lost in explaining what argv[1][i] does.

1 Like

Thanks. Now I think I got it.

argv[i] is the different arguments you put into the command line.
i is the serial number of the argument.

argc is the number of arguments you have entered (learned at Ex13):

me@debian:~/lcthw/code/ex13$ ./copy 1 2 3 
arg 1:	1,	argc:	4, argv:	1
arg 2:	2,	argc:	4, argv:	2
arg 3:	3,	argc:	4, argv:	3

here’s the relevant part of the source code for the above output:

#include <stdio.h>

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

	//go through each string in argv
	//why am I skipping argv[0]?
	for (i = 1; i < argc; i++) {
		printf("arg %d:\t%s,\targc:\t%d, argv:\t%s\n", i, argv[i], argc, argv[i]);

I think we can close this session now. Thank you @io_io .Thank you @zedshaw

2 Likes