Strange results from the C program

Hello, I am getting up to speed with C during the week and I have written one of my first programs and I do not understand why the return of the function. If I input X as 3, Y as 4 and N as 6 I get the return as several million. I should be getting the return of check funtion as 1 with those inputs.

#include

int check(int x,int y,int n);

int main(void)
{
int x,y,n;
printf("
Value of X: ");
scanf("%d", &x);

printf("
Value of Y: ");
scanf("%d", &y);

printf("
Value of N: ");
scanf("%d", &n);

check(x,y,n);
printf("check = %d", check);
if (check == 1){
printf("vx and y checked out.");
}
else if (check == 0){
printf("Digits failed the check");
}
}

int check(int x,int y,int n)
{
int i=0;
if (x>0 && x<(n-1)){
if (y>0 && y<(n-1)){
return 1;
}
}

}

Comments

  • Because if your IFs fail, you don't return anything at all.
  • Because if your IFs fail, you don't return anything at all.
  • Hi Kotik..

    I think it's because you call check function directly without put it into variabel.
    Please try this code , maybe it help you.

    #include

    int main(void)
    {
    int x,y,n;
    [b]int z;[/b]
    printf("
    Value of X: ");
    scanf("%d", &x);

    printf("
    Value of Y: ");
    scanf("%d", &y);

    printf("
    Value of N: ");
    scanf("%d", &n);

    [b]z = check(x,y,n);
    printf("check = %d", z);[/b]
    if (z == 1){
    printf("vx and y checked out.");
    }
    else if (check == 0){
    printf("Digits failed the check");
    }
    }

    int check(int x,int y,int n)
    {
    int i=0;
    if (x>0 && x<(n-1)){
    if (y>0 && y<(n-1)){
    return 1;
    }
    }

    }
  • : Hello, I am getting up to speed with C during the week and I have
    : written one of my first programs and I do not understand why the
    : return of the function. If I input X as 3, Y as 4 and N as 6 I get
    : the return as several million. I should be getting the return of
    : check funtion as 1 with those inputs.
    :
    : #include
    :
    : int check(int x,int y,int n);
    :
    : int main(void)
    : {
    : int x,y,n;
    : printf("
    Value of X: ");
    : scanf("%d", &x);
    :
    : printf("
    Value of Y: ");
    : scanf("%d", &y);
    :
    : printf("
    Value of N: ");
    : scanf("%d", &n);
    :
    : check(x,y,n);
    : printf("check = %d", check);
    : if (check == 1){
    : printf("vx and y checked out.");
    : }
    : else if (check == 0){
    : printf("Digits failed the check");
    : }
    : }
    :
    : int check(int x,int y,int n)
    : {
    : int i=0;
    : if (x>0 && x<(n-1)){
    : if (y>0 && y<(n-1)){
    : return 1;
    : }
    : }
    :
    : }
    :
    :
    Hi Kotik..

    I think it's because you call check function directly without put it into variabel.
    Please try this code , maybe it help you.

    #include <stdio.h>

    int main(void)
    {
    int x,y,n;
    [b]int z;[/b]
    printf("
    Value of X: ");
    scanf("%d", &x);

    printf("
    Value of Y: ");
    scanf("%d", &y);

    printf("
    Value of N: ");
    scanf("%d", &n);

    [b]z = check(x,y,n);
    printf("check = %d", z);[/b]
    if (z == 1){
    printf("vx and y checked out.");
    }
    else if (check == 0){
    printf("Digits failed the check");
    }
    }

    int check(int x,int y,int n)
    {
    int i=0;
    if (x>0 && x<(n-1)){
    if (y>0 && y<(n-1)){
    return 1;
    }
    }

    }
  • you are getting such results in printf statement because you have written your statement as

    [b]printf("check = %d", check);[/b]

    What this will do is print the address where your function is present in the memory. But if your statement as

    [b]printf("check = %d", check());[/b]

    which I thing you intended for.

    One more thing you should rewrite your check method/function, as if conditions are not satisfied you will end up in returning a garbage value to your return statement. For beginners it is prefered to code in the way our other fellow programmers have modified your code.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories