0

I am using Vsc to write my program in C, the program should return an error but nothing is shown, only the final code is provided (code = 3221225477), how can I get the error displayed?

The final output is: [Done] exited with code=3221225477 in 0.569 seconds

#include <stdio.h>
#include <stdlib.h>

int main(){

    char x = 'a';
    printf("%s", x);   //Error cause i'm using %s for a char
}

P.s. I have already installed .Run and the C / C ++ extension

rioV8
  • 24,506
  • 3
  • 32
  • 49
  • 3
    *"the program should return an error"* - no, the program has *undefined behavior*. There is no requirement for an error – UnholySheep Jun 06 '22 at 22:30
  • From the program's point of view, there is no error message - the program crashed. If you run it from the command line the shell would probably just print "segmentation fault" or something but that is the shell printing the message and not the program. Still, you can probably change the tasks.json so that it runs your program using a shell and then you might get the error you expect. But that depends on what shell you use - what shell is it? – Jerry Jeremiah Jun 06 '22 at 22:32
  • Also, you can probably run it using the debugger and that will give you more info. – Jerry Jeremiah Jun 06 '22 at 22:35
  • Luca Orland, what version of VSC? – chux - Reinstate Monica Jun 06 '22 at 22:36
  • In this case, the problem is that your printf format string says you are passing a string to a zero terminated string but you are actually just passing a single character so printf tries to dereference the pointer and fails because it isn't a pointer. – Jerry Jeremiah Jun 06 '22 at 22:37
  • 1
    @chux-ReinstateMonica 1.67.2 – Luca Orlandi Jun 06 '22 at 22:40
  • @JerryJeremiah if I try to compile the code no value is printed, I would like it to tell me that there is an error and cannot print it – Luca Orlandi Jun 06 '22 at 22:41
  • @JerryJeremiah using the debugger instead the value is printed correctly, it is as if I had used "% c" – Luca Orlandi Jun 06 '22 at 22:43
  • 1
    If you are using GCC, try `-Wall -Werror` option to get error. [https://wandbox.org/permlink/cQnKun8cc6wRT0GY](https://wandbox.org/permlink/cQnKun8cc6wRT0GY) – MikeCAT Jun 06 '22 at 22:54
  • 1
    An editor (or in general any developer tool) is not a babysitter. There can be ambiguous error message or even no error message (depending on the languages and specific tools), so developers should carefully review their code and learn more on troubleshooting tips (break points, application logs and so on). Thus, for this very specific scenario you described, unfortunately VS Code won't help much further. Another tool (like CLion) might work differently but that's a different story. – Lex Li Jun 06 '22 at 23:06
  • @MikeCAT It worked, do you know how I can put this option on vsc? – Luca Orlandi Jun 06 '22 at 23:08
  • 1
    @LucaOrlandi *I would like it to tell me that there is an error and cannot print it* — As I was just mentioning to another poster, "C is a challenging language to learn as a beginner, and especially in the 21st century, because it is not a particularly safe, forgiving language. There are quite a few things you can do that seem reasonable, but are quite wrong. Your expectation might be that 'this should work', or 'the compiler should take care of this for me' or 'I'll get an error if I do something wrong', but those expectations are, I'm sorry, misguided." – Steve Summit Jun 06 '22 at 23:16
  • Now, with that said, MikeCAT is right, gcc will give you a bunch more useful warnings if you ask it to. (But I don't use vsc, so I can't tell you how to enable those warnings there.) – Steve Summit Jun 06 '22 at 23:18

1 Answers1

1

The Microsoft C/C++ extension if configured properly (problemMatcher in tasks.json) should also trigger similar errors based on the compiler you select.

After investigation, you might get some help from the clangd extension.

  1. Install the extension in VS Code from https://marketplace.visualstudio.com/items?itemName=llvm-vs-code-extensions.vscode-clangd
  2. Open your project folder in VS Code and open the source file (like main.c).

Then this extension should run clangd in the background and show a warning "Format specifies type 'char *' but the argument has type 'char' (fix available)".

Lex Li
  • 60,503
  • 9
  • 116
  • 147