Stopped running when I added printf

0

I made a source code in C, conditional, but when I add a function printf windows reports that the program stopped running. See the code: 1 : link

NOTE: I USE WINDOWS 7 HOME BASIC, AND MY EDITOR AND COMPILER IS DEVC ++, IF THAT INTERFERE IN SOMETHING PLEASE NOTIFY ME. AND IF IT IS A HARDWARE PROBLEM, AND SEE ALSO

#include <stdio.h>

int main(void) {
    int valor = 3;
    char resultado;

    if(valor = 1){
        resultado = 's';
    }else{
        resultado = 'n';
    }
    printf("a resposta é:%s", resultado);
}
    
asked by anonymous 16.03.2016 / 14:52

2 answers

3

The errors are because you want to print% w / o% (% c) as% w / o% (% s) in your printf (); And there is a possible error in your if, because the way it is written will result in 's' forever, because char is a comparison, but string is an assignment. The correct code looks like this:

#include <stdio.h>

int main(void) {
    int valor = 3;
    char resultado;

    if(valor == 1){
        resultado = 's';
    }else{
        resultado = 'n';
    }
    printf("a resposta é:%c", resultado);
}
    
16.03.2016 / 15:08
2
The code problem is that you are passing %s (string) to printf when variable is char (% c) so do printf :

printf("a resposta%c", resultado);
    
16.03.2016 / 15:03