Gibas
Gibas

Reputation: 55

Printf strange characters

Why it doesn't work? In the end of the program, it shows 2 strange characters instead of "e primo" or "nao e primo". I would be grateful if you could help me.

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

int main() {
    // var
    int n, c = 2;
    char p[11];
    // code
    printf("Informe um numero para checar se e primo: ");
    scanf("%d", &n);
    do {
        if (n % c == 0) {
            p[11] = 'e primo';
            break;
        } else {
            p[11] = 'nao e primo';
        }
        c = c + 1;
    } while (c != n / 2);
    printf("\nO numero %s", p);
    return 0;
}

Upvotes: 1

Views: 3414

Answers (3)

chqrlie
chqrlie

Reputation: 145297

Your program has some problems:

  • you cannot copy string with a simple assignment p[11] = 'e primo';. You could use strcpy() with a string if you make the buffer larger or you could just use a string pointer const char *p;.

  • The loop runs forever if n is less than 4. More precisely, it invokes undefined behavior when c = c + 1; causes arithmetic overflow.

  • The result is the exact opposite for other values.

  • The loop is very slow for large prime numbers, you should stop when c * c > n.

Here is a corrected version:

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

int main(void) {
    // var
    int n, c;
    const char *p = "e primo";
    // code
    printf("Informe um numero para checar se e primo: ");
    if (scanf("%d", &n) == 1) {
        for (c = 2; c * c <= n; c = c + 1) {
            if (n % c == 0) {
                p = "nao e primo";
                break;
            }
        }
        printf("O numero %s\n", p);
        return 0;
    }

Upvotes: 1

P.P
P.P

Reputation: 121427

There's no 12th element in your array; they are only 11 elements in p.Your assignments (p[11] = 'e primo';) are thus results in undefined behaviour.

'e primo' is a multi-byte character literal with implementation-defined behaviour. You probably want to use strcpy() to copy.

strcpy(p, "e primo");

(and increase the array size to accommodate longer string in your other string copy as pointed in the comment).

Or, you can simply use a pointer that point to the string literal as yo don't really need the array.

char *p = "nao e primo";
printf("Informe um numero para checar se e primo: ");
scanf("%d", &n);
do {
    if (n % c == 0) {
        p = "e primo";
        break;
    } else {
        p = "nao e primo";
    ...

printf("\nO numero %s", p);

Related: Single quotes vs. double quotes in C or C++

Upvotes: 1

concrete_rose
concrete_rose

Reputation: 198

Firstly, you cannot say

p[11] = 'e primo';

p is array of chars, and with p[11] you can set or retrieve the char on 11th position. Secondly, there is no char with index 11. In C, indexing begins from 0, so you can retrieve chars at p[0], p[1], ..., p[10]. 11 elements.

Did you read warnings?

1.c: In function ‘main’:
1.c:16:21: warning: character constant too long for its type
             p[11] = 'e primo';
                     ^
1.c:16:21: warning: overflow in implicit constant conversion [-Woverflow]
1.c:21:21: warning: character constant too long for its type
             p[11] = 'nao e primo';
                     ^
1.c:21:21: warning: overflow in implicit constant conversion [-Woverflow]

It actually says that character constant too long for its type.

What you can say:

p[10] = 'e'

Then, with %s you print "string": array of characters determined with determinate 0. So, after last character that should be visible, you have to say e.g.: p[10] = '\0'.

I would make the code work, but I am not sure what is actually the point. As it seems, you are always assigning last character to something.

Upvotes: 0

Related Questions