Exercise in C returns wrong value

-1

I'm trying to do an exercise in C that does not generate any syntax errors, I've already looked and I can not find the error, it simply reads or returns the wrong values.

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

    int main()
    {



        int codigo,quantidade;
        float precototal;
        char especificacao[] = "";
        printf("Escreva o codigo do produto:");
        scanf("%d",&codigo);
            printf("Escreva a quantidade do produto:");
        scanf("%d",&quantidade);

        switch(codigo)
        {
        case 100:
        strcpy(especificacao, "Cachorro Quente"); 
         printf("%d",quantidade);
        precototal = 1.20 * quantidade;

        printf("Produto:%s,Quantidade:%d,preco unitario:%f,preco total:%f",especificacao,quantidade,1.20,precototal);

        break;
        case 101:

        break;
        case 102:

        break;
        case 103:

        break;
        }
        return 0;
}

The value that is returned is this:

    
asked by anonymous 09.09.2017 / 19:30

2 answers

2

try to indent your code correctly

I initialized its string char especificacao[] with a size [30] and it worked here see how the code was:

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

int main()
{
    int codigo,quantidade;
    float precototal;
    char especificacao[30];
    printf("Escreva o codigo do produto:");
    scanf("%d",&codigo);
    printf("Escreva a quantidade do produto:");
    scanf("%d",&quantidade);

    switch(codigo)
    {
    case 100:
    strcpy(especificacao, "Cachorro Quente");
    printf("%d",quantidade);
    precototal = 1.20 * quantidade;
    printf("Produto:%s,Quantidade:%d,preco unitario:%f,preco total:%f",especificacao,quantidade,1.20,precototal);
    break;
    case 101:

    break;
    case 102:

    break;
    case 103:

    break;
    }
    return 0;
}

You could also initialize your string especificacao as pointer of char char *especificacao; would also work anyway the error was at initialization of this variable you can not initialize a vetor pointing to "" co_de % in the case.

    
09.09.2017 / 19:47
1

Our friend @Assanges is correct. The problem is the line containing the declaration

char especificacao[] = "";

What happens is that you are not declaring the size of the area in memory reserved to save a String , that is, how many bytes you want to use to save it. For some reason the strcpy function correctly returns the word "Cachorro Quente" to within especificacao .

However, since there is not an exact amount of reserved memory, possibly the return of the function ends up overwriting the reserved area for some of its other variables and therefore the result is incorrect.

The correct way to declare variables of type String, which is a pointer to character, is how Assanges said

char especificacao[30];

Here it separates 30 bytes into memory to save characters. Note that you can only save 29 characters, since the string character denotes the end of the string and is indispensable.

This problem can be best seen if you try to read from the console some arbitrary%% of size into the variable without declared size, as in the example below:

char especificacao[] = "";
        scanf("%s",&especificacao); 
    printf("Produto: %s ",especificacao);

Depending on the size of your input, the program may write your input, or it may fail targeting.

    
09.09.2017 / 22:05