Starting a C program with a command

In summary, the conversation is about analyzing a code and figuring out its output when started with a specific command. The code involves using a debugger and understanding various elements such as argc, argv, and atoi. There are also some questions and comments about the code being difficult to analyze. The output of atoi(argv[3]) is discussed, and there is a suggestion to modify a variable to test a hypothesis.
  • #1
diredragon
323
15

Homework Statement


Analyze the code below and figure out what it outputs if it's started with this command:
kod.PNG

The correct solution is given so i can better understand the parts of the problem.

Homework Equations


3. The Attempt at a Solution [/B]
I had typed it out and tried to get the same result on Visual Studio 2017 by passing it's command to Visual Debugger Command line Argument and it keeps outputting
output.PNG

Maybe i did something wrong with the command line? I do not know. I continued to analyze it by hand and i had a few questions along the way that were stopping me from figuring it out. Here is fully typed out and annotated code in which i showed my understanding of it.
C:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

   //./program.exe polisemija metonimija 0x1 2

void f(char *a, char *b, int c) {
   if (a < b && c>0) f(a++, b--, c - 1);
   *a = *a < *b ? *a : ++*b;
}

   //argc = 5 and argv start from 0 with argv[0] = ./program.exe and end with argv[4] = 2
void main(int argc, char *argv[]) {
   int c = atoi(argv[argc - 1]);
       // c = 2;
   char *p = argv[atoi(argv[3]) + 1],
       //*p = argv[1 + 1] = "metonimija" but my debigger shows *p = "./program.exe"
       //how can that be?
       *q = p + strlen(p) - 1;
       //here q = p + 13 - 1; if we take *p = "./program.exe" but what is p here ?
       //my debugger gives q = 'e' so does this mean it points to 12th charachter of *p?
   f(p, q, c);
   printf("%s", p);
I wanted to stop at this point to make sure my i typed in everything correctly as i do not get the right result. What is wrong here?
 
Physics news on Phys.org
  • #2
What is the output of atoi(argv[3])? Maybe it has a problem with the hexadecimal number. If it returns -1, ...
diredragon said:
*q = p + strlen(p) - 1;
What a horrible code. Anyway, you can modify p to test your assumption.
 
  • #3
diredragon said:

Homework Statement


Analyze the code below and figure out what it outputs if it's started with this command:
View attachment 205433
The correct solution is given so i can better understand the parts of the problem.

Homework Equations


3. The Attempt at a Solution [/B]
I had typed it out and tried to get the same result on Visual Studio 2017 by passing it's command to Visual Debugger Command line Argument and it keeps outputting
View attachment 205434
Maybe i did something wrong with the command line? I do not know. I continued to analyze it by hand and i had a few questions along the way that were stopping me from figuring it out. Here is fully typed out and annotated code in which i showed my understanding of it.
C:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

   //./program.exe polisemija metonimija 0x1 2

void f(char *a, char *b, int c) {
   if (a < b && c>0) f(a++, b--, c - 1);
   *a = *a < *b ? *a : ++*b;
}

   //argc = 5 and argv start from 0 with argv[0] = ./program.exe and end with argv[4] = 2
void main(int argc, char *argv[]) {
   int c = atoi(argv[argc - 1]);
       // c = 2;
   char *p = argv[atoi(argv[3]) + 1],
       //*p = argv[1 + 1] = "metonimija" but my debigger shows *p = "./program.exe"
       //how can that be?
       *q = p + strlen(p) - 1;
       //here q = p + 13 - 1; if we take *p = "./program.exe" but what is p here ?
       //my debugger gives q = 'e' so does this mean it points to 12th charachter of *p?
   f(p, q, c);
   printf("%s", p);
I wanted to stop at this point to make sure my i typed in everything correctly as i do not get the right result. What is wrong here?
Minor point -- your code is missing the final right brace at the end. You must have not copied it, since your code actually compiles and runs.

When you run your program, argc = 5, argv[0] contains the name of the program, argv[1] is "polsemija", argv[2] is "polisemija", argv[3] is "0x1" and argv[4] is "2".
The expression atoi(argv[3] is 0, not 1 as you show in your comment. The reason is that atoi("0x1") evaluates to 0, since 'x' is not a decimal digit. (See http://www.cplusplus.com/reference/cstdlib/atoi/?kw=atoi for more information.)
So argv[atoi(argv[3]) + 1] is argv[0 + 1] or argv[1], which is the string "polisemija", and p contains the address of that string. I don't know why your debugger shows a different string.
 
Last edited:
  • #4
mfb said:
What is the output of atoi(argv[3])? Maybe it has a problem with the hexadecimal number. If it returns -1, ...
What a horrible code. Anyway, you can modify p to test your assumption.
I know, it's bugging me too but this is how the tests in my school are created. They tend to write purposely complicated and horrible code to make it harder for us to analyze it. Sad.
Mark44 said:
Minor point -- your code is missing the final right brace at the end. You must have not copied it, since your code actually compiles and runs.

When you run your program, argc = 5, argv[0] contains the name of the program, argv[1] is "polsemija", argv[2] is "polisemija", argv[3] is "0x1" and argv[4] is "2".
The expression atoi(argv[3] is 0, not 1 as you show in your comment. The reason is that atoi("0x1") evaluates to 0, since 'x' is not a decimal digit. (See http://www.cplusplus.com/reference/cstdlib/atoi/?kw=atoi for more information.)
So argv[atoi(argv[3]) + 1] is argv[0 + 1] or argv[1], which is the string "polisemija", and p contains the address of that string. I don't know why your debugger shows a different string.
1-st prog.PNG

Is this where i was suppose to write the command argument? Maybe there is something wrong with this. Also here is a debugging screenshot:
2-nd.jpg

It shows that p is the first parameter of the argument but it should be the argv[1] = "polisemija" like you said and not this.
 
  • #5
Don't include the program name in the command arguments. The program name (and path) is already argv[0], so you're essentially getting it twice by making it argv[1].

Also, the argv array should contain 5 values, not the 6 that you show.
 
Last edited:
  • #6
Mark44 said:
Don't include the program name in the command arguments. The program name (and path) is already argv[0], so you're essentially getting it twice by making it argv[1].

Also, the argv array should contain 5 values, not the 6 that you show.
That was it! Without the program name it runs fine. But i can't still get the result by analyzing. Some thing aren't clear to me.
C:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

   //./program.exe polisemija metonimija 0x1 2

void f(char *a, char *b, int c) {
   if (a < b && c>0) {
       f(a++, b--, c - 1);
   }
   int t = *a < *b;
   *a = *a < *b ? *a : ++*b;
}
   //here is the analysis of the void f function
   //the first thing that comes to my attention is a < b? How is a smaller than b?
   //since that is true we move on to do the function again passing a++,b--, c-1;
   //my debugger here shows only that c changed from c=2 to c=1 and no change in a or b. Why?
   //the loops finished and we now execute the last line of the function 3 times
   //first time
   //*a = 0 ? *a : ++*b;  here *a = "olisemijb" and *b = "jb"
   //before i continue i need to understand how this changed.
   //++*b line was supposed to change "a" to "b" right?
   
void main(int argc, char *argv[]) {
   int c = atoi(argv[argc - 1]);
       // c = 2;
   int number = atoi(argv[3]) + 1;
       //number = 1;

   char *p = argv[number], *q = p + strlen(p) - 1;
       //p = "polisemija" and q = a
       
   f(p, q, c);
   printf("%s", p);
}
 
  • #7
What is unclear?

If it is q, see what happens if you change polisemija to polisemijb or poliserhrhorhemija, for example.
 
  • #8
One thing that is tricky in this program is that the function f is recursive, since it calls itself. Another thing that you might not be understanding is that the expression a < b is comparing addresses, not the characters at those addresses.
 
  • #9
Mark44 said:
One thing that is tricky in this program is that the function f is recursive, since it calls itself. Another thing that you might not be understanding is that the expression a < b is comparing addresses, not the characters at those addresses.
Oh i did not know that. So a < b will always be true because the address of the firstly declared is always lower.

mfb said:
What is unclear?

If it is q, see what happens if you change polisemija to polisemijb or poliserhrhorhemija, for example.
I don't get what happens hen they do f(a++, b--, c -1). I know i enter the function again with the old values of a and b but since they are the addresses what happens when i decrement an address? What does a++ do? Also when i get to the *a < *b is a a pointer the the first letter? Does ++*b increment the value of the first letter then?
 
  • #10
diredragon said:
what happens when i decrement an address?
You point to the element one before it. In this case, the previous character in the string.
Increasing a char should give the following letter in the alphabet (or something else from z and Z). Who does that?
 
  • Like
Likes diredragon
  • #11
diredragon said:
Oh i did not know that. So a < b will always be true because the address of the firstly declared is always lower.
Not necessarily. If the variables are local to the function, and likely allocated on the stack, it's probably true that the last one declared has a lower address.
diredragon said:
I don't get what happens hen they do f(a++, b--, c -1). I know i enter the function again with the old values of a and b but since they are the addresses what happens when i decrement an address? What does a++ do? Also when i get to the *a < *b is a a pointer the the first letter? Does ++*b increment the value of the first letter then?
 
  • Like
Likes diredragon

1. How do I start a C program with a command?

To start a C program with a command, you will need to open a command prompt or terminal window and navigate to the directory where your program is saved. Then, use the gcc command to compile your program and create an executable file. Finally, use the ./ command followed by the name of your executable file to run your program.

2. What is the purpose of starting a C program with a command?

The purpose of starting a C program with a command is to compile and execute the program from the command line. This allows for more control and flexibility in running the program, as well as the ability to pass command line arguments to the program.

3. Can I start a C program with a command on any operating system?

Yes, you can start a C program with a command on any operating system that supports the C programming language. This includes Windows, Mac OS, and Linux.

4. Do I need to have a compiler installed to start a C program with a command?

Yes, you will need to have a C compiler installed on your computer to start a C program with a command. The most common C compiler is gcc, but there are also other options available.

5. Is starting a C program with a command the only way to run a C program?

No, there are other ways to run a C program such as using an integrated development environment (IDE) or a code editor. However, starting a C program with a command is a common and efficient way to run the program, especially for more experienced programmers.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Programming and Computer Science
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
947
  • Engineering and Comp Sci Homework Help
Replies
3
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
15
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
Back
Top