-1

When I run the code (codes in attached image) nothing appears on the output screen when I try argv[] as arguments of strncmp() function. I searched on the web but I did not see any application like this for strncmp!

Everything I found was nearly like this:

int strncmp(
   const char *string1,
   const char *string2,
   size_t count
);

Not MSVC version code

I tried the codes in the image I attached in MSVC like this:


#include "stdafx.h"
#include <stdio.h>
#include <windows.h>
#include<string.h>
void main(int argc, char *argv[]) {
    
    system("cls");
    if (!strncmp(argv[1], argv[2], 8))
        printf("Both strings are the same!");
    else
        printf("Not same!");
}

But nothing was on the output screen and no error and no warnings detected by MSVC. argv[1] and argv[2] each one is .txt files which I create before and their contents are exactly the same.

Then, I tried this code with two string arrays instead of previous .txt files, and it worked as I expected and fine. (According the strings the output was "Both strings are the same!" or "Not same!" )

How can I rewrite the codes in the image I attached, so that it works in MSVC as well?! How I pass my command line arguments It works on code::blocks

  • 1
    What actual arguments do you pass to your program when you run it? – Some programmer dude Dec 16 '22 at 11:30
  • 1
    Also remember that `argv` is an array of strings, each element in the array is an actual argument to your program. It is *not* the contents of any files. – Some programmer dude Dec 16 '22 at 11:31
  • You've correctly rewrote the code from the screenshot into MSVC, it's mostly standard C. I suspect you did not pass command-line arguments when running the program, see [here](https://stackoverflow.com/questions/298708/debugging-with-command-line-parameters-in-visual-studio), and got out-of-array-bounds, invoking undefined behavior, which may result in _any_ behavior. – yeputons Dec 16 '22 at 11:33
  • For example, if your program is called `foo.exe`, then if you run it as `foo.exe argument argument` then it would report that both strings are equal. If you run as `foo.exe oneargument anotherargument` then the program would report that they are not the same. If you run as `foo.exe oneargument` or just `foo.exe` without any arguments, you will have *undefined behavior* and probable crashes as you use a null pointer. – Some programmer dude Dec 16 '22 at 11:34
  • "argv[1] and argv[2] each one is .txt files which I create before and their contents are exactly the same." What does that mean? You have 2 files with the same name? `strncmp` does not open files and compare content. It compares the filename if you provide them as command line argument. And 2 files cannot have same name. – Gerhardh Dec 16 '22 at 11:36
  • @Some programmer dude as I said at the top, I pass two files as command arguments that both content was only two sam character "sa". Also, I tried that on code::blocks compiler and it works fine with `argv` and no problem. Of course before that I changed that 8 (` if (!strncmp(argv[1], argv[2], 2))` ) to 2 or 3 so that it works. But in MSVC It does not work. – The mayor of kazeroun Dec 16 '22 at 11:49
  • 1
    You seem to have some basic misunderstanding how program arguments works You don't pass files to your program, you only pass the file *names*. – Some programmer dude Dec 16 '22 at 11:51
  • And please **[edit]** your question to tell us exactly how you configured MSVC to set the arguments. – Some programmer dude Dec 16 '22 at 11:52
  • Also please take some time to read [the help pages](http://stackoverflow.com/help), especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). Also please take the [tour] and read about [ask] good questions. Lastly please read [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude Dec 16 '22 at 11:52
  • @Gerhardh No with different names. Really?? only compare the filename and not the contents? I do not think so. But maybe you're right. I am a beginner. But anyway, as I said at the top, it does not even show the `else` ongoing instruction at output! – The mayor of kazeroun Dec 16 '22 at 11:56
  • The image you show from MSVC shows the settings when running in the *debugger*. It was many years since I used MSVC, but aren't the settings different between running in the debugger, and just running (without the debugger)? – Some programmer dude Dec 16 '22 at 12:07
  • @Gerhardh Uhhh! You were right! It compares filenames not the content. But still nothing on output screen in MSVC. – The mayor of kazeroun Dec 16 '22 at 12:22
  • You provide names, the program receives names, it compares names! End of story. How would the shell or the program know that you want it to open the file and compare the content if you don't tell it to do so? If your assumption was right, how would you be able to pass the name only without reading the file? – Gerhardh Dec 16 '22 at 13:14
  • 2
    I would suggest adding '\n' to the strings you are printing. – stark Dec 16 '22 at 13:23
  • @stark It did not help! – The mayor of kazeroun Dec 16 '22 at 19:05

1 Answers1

0

What probably happens is that you haven't configured MSVC to pass the arguments correctly, which means you will pass a null pointer to strncmp (and possibly an indeterminate pointer as well).

That leads to undefined behavior, and very likely crashes.

To solve that you must always check argc to make sure that you have enough arguments.

The value of argc is the number of valid elements, so if you expect two arguments then you must check that it's at least 3:

if (argc < 3)
{
    printf("Not enough arguments! At least two expected.\n");
    return 1;
}

To be brutally honest, this should have been taught in any decent class, tutorial or book.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • I add your code to mine, but nothing on the output screen this time too. `#include "stdafx.h" #include #include #include void main(int argc, char *argv[]) { if (argc < 3) { printf("Not enough arguments! At least two expected.\n"); } system("cls"); if (!strncmp(argv[1], argv[2], 2)) printf("Both strings are the same!"); else printf("Not same!"); }` – The mayor of kazeroun Dec 16 '22 at 12:10
  • @Themayorofkazeroun Your [`main` function](https://en.cppreference.com/w/c/language/main_function) *must* be declared to return an `int` value. And when there's not enough arguments you should `return` from the function, not carry on like nothing happened. Right now, you clear the screen so the error message is lost, and then you dereference null or other invalid pointers anyway and crash. – Some programmer dude Dec 16 '22 at 12:15
  • @Themayorofkazeroun Okay! I have done your point and now this part works: `if (argc < 3) { printf("Not enough arguments! At least two expected.\n"); return 1; }` I have "Not enough arguments! At least two expected." on the output screen. But why? I have passed arguments in MSVC many times before successfully. But this time it differs. I wonder why?! – The mayor of kazeroun Dec 16 '22 at 12:31
  • @Themayorofkazeroun See my comment to your main question, about debugger and run settings maybe being different. – Some programmer dude Dec 16 '22 at 12:42