Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


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

Rep: Reputation: 0
strange output (c)

i have troubles understandig the following 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;
    printf("%ld\n", a);
    printf("%ld\n", b);
For every input in getNumb() i become a=2 b=22 in

p.s. is not a homework ..
Old 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,818

Rep: Reputation: 108Reputation: 108

both a and b are local.

Happy Penguins.
Old 11-18-2006, 04:04 PM   #3
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
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
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
Senior Member
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
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
LQ Newbie
Registered: Oct 2005
Posts: 13

Original Poster
Rep: Reputation: 0
thx for ur explanations


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:56 AM.

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