C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28695
Number of posts: 94715

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Strange results from the C program Posted by Kotik on 9 Oct 2012 at 7:41 AM
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 <stdio.h>

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

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

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

printf("\nValue 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;
}
}

}

Report
Re: Strange results from the C program Posted by WaltP on 9 Oct 2012 at 8:52 AM
Because if your IFs fail, you don't return anything at all.
Report
Re: Strange results from the C program Posted by WaltP on 9 Oct 2012 at 8:54 AM
Because if your IFs fail, you don't return anything at all.
Report
Re: Strange results from the C program Posted by h4ckjr on 9 Oct 2012 at 7:52 PM
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;
int z;
printf("\nValue of X: ");
scanf("%d", &x);

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

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

z = check(x,y,n);
printf("check = %d", z);

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;
}
}

}
Report
Re: Strange results from the C program Posted by h4ckjr on 9 Oct 2012 at 7:55 PM
: 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 <stdio.h>
:
: int check(int x,int y,int n);
:
: int main(void)
: {
: int x,y,n;
: printf("\nValue of X: ");
: scanf("%d", &x);
:
: printf("\nValue of Y: ");
: scanf("%d", &y);
:
: printf("\nValue 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;
int z;
printf("\nValue of X: ");
scanf("%d", &x);

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

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

z = check(x,y,n);
printf("check = %d", z);

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;
}
}

}
Report
Re: Strange results from the C program Posted by sapoorva on 29 Oct 2012 at 11:15 PM
you are getting such results in printf statement because you have written your statement as

printf("check = %d", check);

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

printf("check = %d", check());

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.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.