ProgrammingThis 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.
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.
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?
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)
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.