is it possible if I scanf in agrc and agrv ?

Oct 2, 2019 at 7:05pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <stdlib.h>
#include <stdio.h>

int max(int n, int n2)
{
	int a,b;
	if(n > n2)
	{
		b = n;
	}
	else
	{
		b = n2;
	}
}
int main (int argc, char *argv[])
{
	scanf("%d", &argc);
	
	n = argc;
	printf("%d\n", argc);
	printf("%d\n", argv[0]);
	printf("%s \n", argv[0]);
	for( int i= 0; i< argc; i++)
	{
		printf("argv[%d] = %s\n", argv[i]);
	}
	
	return 0;
}


just as the title say I try to use scanf to input the integer without using any text but the execute didn't go as hope for.

if this argc and argv need a text or program how do I make one?
or do I need to put the function first so the argc and argv work ? if so how do I do that ?
Last edited on Oct 2, 2019 at 7:06pm
Oct 2, 2019 at 7:10pm
Can you restate what you're actually trying to do, without getting into the specifics of argc/argv/scanf? Generally, there should be no reason for you to change what's in argc/argv. Those are for arguments passed in externally by command line.

Are you just trying to pass in command-line arguments to your program? That's what argc and argv are for.
e.g. if you run
my_program arg1 arg2
then argc will == 3, and argv[0] == "my_program", argv[1] == "arg1", and argv[2] == "arg2".

Or are you just trying to get the user to input a number or string? If so, why use argc/argv at all?

printf("argv[%d] = %s\n", argv[i]);
This is incorrect, your format specifiers don't match the arguments you're giving.

You want:
printf("argv[%d] = %s\n", i, argv[i]);
Last edited on Oct 2, 2019 at 7:16pm
Oct 2, 2019 at 8:34pm
remember that the first location of argv are beyond your control, holding the program's name.
it sounds like you want atoi or atof.
double d = atof(argv[3]); //d is now the numeric value of the text passed in
int i = atoi(argv[2]); //i is the integer value from text.

(I feel like clippy... it looks like this is what you want, is this what you want?)
Last edited on Oct 2, 2019 at 8:35pm
Topic archived. No new replies allowed.