Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
11-18-2006, 03:53 PM
|
#1
|
LQ Newbie
Registered: Oct 2005
Posts: 13
Rep:
|
strange output (c)
Hi,
i have troubles understandig the following code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
long getNumb()
{
long zahl;
printf("input number: ");
scanf("%ld", &zahl);
return zahl;
}
long getNumbgetNumb()
{
long a, b;
a = getNumb();
b = getNumb();
}
int main()
{
long a,b;
getNumbgetNumb();
printf("%ld\n", a);
printf("%ld\n", b);
system("PAUSE");
}
For every input in getNumb() i become a=2 b=22 in
main()..y?
p.s. is not a homework ..
|
|
|
11-18-2006, 03:59 PM
|
#2
|
Senior Member
Registered: Apr 2005
Location: Penguin land, with apple, no gates
Distribution: SlackWare > Debian testing woody(32) sarge etch lenny squeeze(+64) wheezy .. bullseye bookworm
Posts: 1,834
Rep:
|
Hi,
both a and b are local.
Happy Penguins.
|
|
|
11-18-2006, 04:04 PM
|
#3
|
Member
Registered: Oct 2005
Posts: 970
Rep:
|
Take this with a grain of salt as I've been off the mark today.
This should not even compile as getNumbgetNumb doesn't return a value.
a and b in main scope are not initialised and are different variables to those in getNumbgetNumb scope.(edit: which kaz2100 already mentioned)
Last edited by dmail; 11-18-2006 at 04:07 PM.
|
|
|
11-19-2006, 05:28 AM
|
#4
|
LQ Newbie
Registered: Oct 2005
Posts: 13
Original Poster
Rep:
|
Yes i know there are local but why output is 2 and 22? or there is no logical explanation
|
|
|
11-19-2006, 06:03 AM
|
#5
|
Member
Registered: Jan 2004
Location: Australia
Distribution: Debian
Posts: 168
Rep:
|
You never initialise the a and b declared in main. Thus, their value is never written to, and whatever value is left in that position in ram from whatever program ran before is printed out.
|
|
|
11-19-2006, 06:51 AM
|
#6
|
Senior Member
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530
Rep:
|
Quote:
Originally Posted by linuxi
Yes i know there are local but why output is 2 and 22? or there is no logical explanation
|
Until you assign a value to a variable, it's value is undefined. The behaviour of a program which is asked to use an undefined variable is also undefined. Read this to mean "It might do something weird. It might do something boring. It might format your hard disk and put sugar in your fuel tank.".
The exact reasons for the value you get will be very deep in how the compiler/links/os is working. It may well be interesting, but it's not, much to do with C the language itself.
|
|
|
11-19-2006, 10:43 AM
|
#7
|
LQ Newbie
Registered: Oct 2005
Posts: 13
Original Poster
Rep:
|
thx for ur explanations
|
|
|
All times are GMT -5. The time now is 09:45 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|