Stopped running when I added the printf

Asked

Viewed 80 times

0

I made a source code in C, conditional, but when I add a printf function windows reports that the program stopped running. Look at the code: 1: http://i.stack.Imgur.com/a9YUf.png

NOTE: I USE WINDOWS 7 HOME BASIC, AND MY EDITOR AND COMPILER IS DEVC++, IF IT INTERFERES WITH ANYTHING LET ME KNOW, PLEASE. AND IF IT’S A HARDWARE PROBLEM, AND LET ME KNOW

#include <stdio.h>

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

    if(valor = 1){
        resultado = 's';
    }else{
        resultado = 'n';
    }
    printf("a resposta é:%s", resultado);
}
  • 2

    Enter the code in your question. To edit: http://answall.com/posts/118452/edit

  • The code in the question must be the original code - not the corrected code after the answers - otherwise it will confuse future readers who may have the same doubt (and leave the answers meaningless)

2 answers

3

Mistakes are because you are wanting to print one char (%c) as string(%s) in its printf(); And there is a possible error in your if, because the way it is written will result in’s' forever, because if(valor == 1) is a comparison, but if(valor = 1) 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);
}

2

The problem with the code is that you’re passing %s(string) in printf when the variable is a char(%c) do so printf:

printf("a resposta%c", resultado);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.