LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 11-18-2006, 03:53 PM   #1
linuxi
LQ Newbie
 
Registered: Oct 2005
Posts: 13

Rep: Reputation: 0
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 ..
 
Old 11-18-2006, 03:59 PM   #2
kaz2100
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,833

Rep: Reputation: 108Reputation: 108
Hi,

both a and b are local.

Happy Penguins.
 
Old 11-18-2006, 04:04 PM   #3
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
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.
 
Old 11-19-2006, 05:28 AM   #4
linuxi
LQ Newbie
 
Registered: Oct 2005
Posts: 13

Original Poster
Rep: Reputation: 0
Yes i know there are local but why output is 2 and 22? or there is no logical explanation
 
Old 11-19-2006, 06:03 AM   #5
urzumph
Member
 
Registered: Jan 2004
Location: Australia
Distribution: Debian
Posts: 168

Rep: Reputation: 30
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.
 
Old 11-19-2006, 06:51 AM   #6
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
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.
 
Old 11-19-2006, 10:43 AM   #7
linuxi
LQ Newbie
 
Registered: Oct 2005
Posts: 13

Original Poster
Rep: Reputation: 0
thx for ur explanations
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
strange ps -ax output debyani Linux - Security 1 10-27-2006 08:38 AM
Strange output from C program Gins Programming 8 07-11-2006 10:29 PM
Strange traceroute output egurski Linux - Networking 0 07-13-2004 06:25 PM
Strange dmesg output voyciz Linux - Networking 3 06-08-2004 12:05 PM
strange ofstream output maarten Programming 2 09-16-2003 11:42 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:33 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration