-3

I tried to run a simple C code like this below in Clion ide ,and everytime i do compile ,it shows me this message :

Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)

C code :

#include <stdio.h>
#include <string.h>

int main(void)
{
    char ch = 'A';
    short s = ch;
    printf(s);

    printf("\n________________________\n");

    short s2 = 67;
    char ch2 = s2;
    printf(ch2);

    return 0;
}
0___________
  • 60,014
  • 4
  • 34
  • 74
meddhiaka
  • 11
  • 1
  • 2
    `printf(s);` is *undefined behaviour* as is `printf(ch2);`. Please see [exit code 139 site:stackoverflow.com](https://www.google.com/search?q=exit+code+139+site:stackoverflow.com) – Weather Vane Aug 27 '21 at 16:43
  • [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – R Sahu Aug 27 '21 at 16:44
  • 1
    Did you get any compiler warnings? If so you should pay attention to them. – Jim Rhodes Aug 27 '21 at 16:45
  • Quick way to track down a crash like this: Run the program in the debugging tool that should have come with your development tools (and if you tools have no debugger, get new tools). When the program crashes, the debugger will halt and allow you to inspect the crash site. If it doesn't crash in your code use the back trace (often called a call stack) to find where your code called into the unknown code. Inspect the parameters passed to the function and make sure they match what the documentation for the function says you should provide. – user4581301 Aug 27 '21 at 17:00
  • If everything matches, things get harder. Look back further in the code for a mistake that can mortally wound the program without making it immediately crash. If you don't see the problem, you'll probably have to step through what lead up to the crash line by line with an eye out for unexpected behaviour like the program taking the wrong path or storing the wrong value. The unexpected is usually a bug, and when it isn't it means your expectations are wrong. Either problem needs to be fixed. – user4581301 Aug 27 '21 at 17:00
  • Nuts. The C++ tag is gone. Now I can't go on a rant about `cout`, `string` and other stuff that'll be useless to the asker. – user4581301 Aug 27 '21 at 17:02
  • @user4581301 because it is not C++. I have removed `compiler-errors` tag as well – 0___________ Aug 27 '21 at 17:08

4 Answers4

1

here error code 139 indicates segment fault that is arising because you haven't used format specifiers in the code the correct code will be

#include <stdio.h>
#include <string.h>

int main(void)
{
    char ch = 'A';
    short s = ch;
    printf("%d",s);

    printf("\n________________________\n");

    short s2 = 67;
    char ch2 = (char)s2;
    printf("%c",ch2);

    return 0;
}
Rahul Kumar
  • 65
  • 1
  • 6
1

Your problem is the line

printf(s);

printf expects its first argument to be a format string, and the type of the argument must be const char *. It's trying to treat s as the address of a string, but the value of 'A' (65 in ASCII) is not a valid address, hence the error.

To print the value of s, you need to do something like

printf( "%hd", s );

The format specifier %hd tells scanf to expect a short argument, and to print the string equivalent of its value as decimal digits ("65").

You have a similar issue in the second printf call.

John Bode
  • 119,563
  • 19
  • 122
  • 198
0

https://www.cplusplus.com/reference/cstdio/printf/ expects a pointer to 0-terminated string. You are passing some short value (A, wich is 65) that is interpreted as an address.

Vlad Feinstein
  • 10,960
  • 1
  • 12
  • 27
0

You can use type casting and format specifiers as follows:

#include <stdio.h>
#include <string.h>

int main(void)
{
    char ch = 'A';
    short s = ch;
    
    //short format specifier
    printf("%hd",s);

    printf("\n________________________\n");

    short s2 = 67;

    //int to char
    char ch2 = (char)s2;

    //char format specifier
    printf("%c",ch2);

    return 0;
}