1

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;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
Gibas
  • 55
  • 1
  • 7

3 Answers3

1

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++

Community
  • 1
  • 1
P.P
  • 117,907
  • 20
  • 175
  • 238
1

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;
    }
chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • Why when we use char with arrays, we make attributions like: C[10] = 'hello' and when we use pointers we do this: C = "hello" why we use '' on the first, and "" on the last? Aren't both strings? – Gibas Nov 13 '16 at 19:39
  • @Gibas: `C[10] = 'hello'` is a syntax error (or an obsolete multi character character constant which should never be used). Strings are always surrounded with double quotes `"`. They can be used to initialize arrays: `char C[10] = "e primo";` or in contexts where they decay into a pointer to their initial character as in pointer initialization: `char *p = "e primo";` and pointer assignments: `p = "nao e primo";` – chqrlie Nov 13 '16 at 20:23
  • I utilized the wrong example, what I meant is that you can't use c = "j" but you can use char C[10] = "e primo" Is it that you need a different notation if it's a single char or a string? – Gibas Nov 13 '16 at 20:46
  • @Gibas: the syntax with single quotes is for a single character constant, it evaluates to an `int` whose value is the character number in the host encoding: `int c = 'e';` The syntax with double quotes (`""`) is for character strings which are arrays of `char` with an implicit trailing null terminator (a `char` with the value `0`, conventionally represented as `'\0'`). For example `"abcdef"[2] == 'c'` is true. – chqrlie Nov 13 '16 at 20:50
  • Thank you for answering all my questions! – Gibas Nov 13 '16 at 21:00
0

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.

concrete_rose
  • 198
  • 3
  • 15
  • Strange characters are regarding absence of **'\0'** as the last character. When assigning **char *p = "Some constant string"**, C does this. – concrete_rose Nov 13 '16 at 19:07