0

I am trying to write a simple programme to familiarise myself with command line argumentation. Currently, I am trying to specify that the programme ought only to accept a command line with two arguments. My attempt to specify that the programme take no more than two arguments worked fine:

#include <stdio.h>

int main(int argc, char* argv[])
{
    for (int z = 1; z < argc; z++)
    {
        if (argc > 2)
            {
                printf("Nope\n");
                return 0;
            }
    }
}

However, I then tried to specify that the programme should accept no less than two arguments:

#include <stdio.h>

int main(int argc, char* argv[])
{
    for (int z = 1; z < argc; z++)
    {
        if (argc > 2)
            {
                printf("Nope\n");
                return 0;
            }
        if (argc < 2)
            {
                printf("Nope\n");
                return 0;
            }
    }
}

The programme has compiled fine, but when I try to run the programme with only one argument, I simply receive the message Segmentation fault. I have tried googling this, but I am very new to programming and everything I can find on segmentation faults is beyond my basic knowledge. Anything which could help me fix my programme or help me understand what a segmentation fault is and why I have run afoul of it would be greatly appreciated.

BlueKhakis
  • 293
  • 1
  • 8
  • Why are your checks in a loop? `argc` isn't going to change. – Retired Ninja Dec 26 '20 at 20:28
  • 1
    Your segmentation fault question is answered in the duplicate. In your program I suspect the bug is not having any kind of return statement when exiting main. – Zan Lynx Dec 26 '20 at 20:30
  • Do you mean to iterate through argv[] rather then argc? – ryyker Dec 26 '20 at 20:30
  • I don't know what kind of system you're building on. My Linux machines do not seg-fault with your program. But what might cause it is if the wrapper code around the main function expects an integer on the stack, and your main function did not push one onto the stack, then it will pop off whatever value happens to be on the stack. As a result when it reaches the exit to the operating system it will try to pop the final stack value and nothing will be there causing the stack pointer to be pointing to unallocated memory, causing a segmentation fault. – Zan Lynx Dec 26 '20 at 20:39
  • @ZanLynx: `main` does not require a `return` statement. If control flows to the end of `main`, it automatically returns zero, per C 2018 5.1.2.2.3 1. – Eric Postpischil Dec 26 '20 at 21:42
  • @EricPostpischil Since it doesn't crash anyway because most modern things return an int in EAX or RAX, I suspect he's using Borland Turbo C or something. Certainly not 2018. – Zan Lynx Dec 26 '20 at 23:46
  • @ZanLynx: I cited the 2018 standard, but this is not new. It is in the 1999 standard. And a non-zero return from `main` would not be reported as a segmentation fault. – Eric Postpischil Dec 26 '20 at 23:58
  • @EricPostpischil I explained under which circumstances it would seg-fault. If the exit code tries to pop the last value off the stack. – Zan Lynx Dec 27 '20 at 00:55
  • 1
    @EricPostpischil and just to clarify here. I've been writing C code since way before 1999. And I have seen segmentation faults caused by not returning a value. – Zan Lynx Dec 27 '20 at 01:07

0 Answers0