Typo

Asked

Viewed 50 times

-3

I took a C book to study, but it’s kind of outdated, and I came across the following code:

#include<stdio.h>

is_in(char *s, char c);

void main(void){
is_in("olac", "ola");
}

is_in(char *s, char c){
    while(*s)
        if(*s == c) return 1;
        else s++;
    return 0;
}

http://pastebin.com/raw/7dUSC0cj
someone can help me?

  • What error does it bring? Uses the code block that it corrects simtaxe error

  • 1

    What is the problem or doubt about the code snippet?

1 answer

3

About compile error, we are missing the types in the prototype and function declaration:

int is_in(char *s, char c);

In the method call, you are passing a string instead of a char.

is_in("olac", "ola");

GCC compiles the code, but cannot transform the inserted string. The correct would be:

is_in("olac", 'o');

Remembering that char always stays inside plics (').

What the code does: it checks whether a character is present within a string. If positive, returns 1 (true); 0 if negative (false).

Browser other questions tagged

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