2 Replies - 5730 Views - Last Post: 29 February 2008 - 04:26 AM Rate Topic: -----

#1 sandeepcu   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 22-December 07

(1)Why does scanf fails? (2)Why program crashes if & is removed?

Posted 29 February 2008 - 03:00 AM

Consider this code:

#include <stdio.h>

		int num_bns, num_ch;
		printf("How many brothers and sisters do you have?\n");
		printf("How many children do you have?\n");
		printf("You have %d brothers and sisters and %d children.\n",num_bns,num_ch);

Question1) Now if i enter a alphabet(instead of a number) for num_bns, then the program does something wired while reading num_bns. It doesn't accepts input for 2nd question and finally prints "You have 2 brothers and sisters and 75 childern." WHY DOES IT DO SO?

Question2) If i remove the "&" symbol then while reading num_bns using scanf, the program crashes? WHY DOES IT CRASH?

Glad if you could answer these two questions.


This post has been edited by sandeepcu: 29 February 2008 - 03:02 AM

Is This A Good Question/Topic? 0
  • +

Replies To: (1)Why does scanf fails? (2)Why program crashes if & is removed?

#2 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6635
  • View blog
  • Posts: 30,900
  • Joined: 10-May 07

Re: (1)Why does scanf fails? (2)Why program crashes if & is removed?

Posted 29 February 2008 - 03:05 AM

You use the ampersand with digits & numbers, because you are assigning the address value to that variable, not the ascii value of what you see as the 'digit'. The number 1 is actually ascii value 49 ( I believe). However, if the variable that scanf is assigning is an integer, then you must use the ampersand to assign the memory address.

http://www.cplusplus...tdio/scanf.html <-- probably explains it better than I do.
Was This Post Helpful? 0
  • +
  • -

#3 AmitTheInfinity   User is offline

  • C Surfing ∞
  • member icon

Reputation: 119
  • View blog
  • Posts: 1,565
  • Joined: 25-January 07

Re: (1)Why does scanf fails? (2)Why program crashes if & is removed?

Posted 29 February 2008 - 04:26 AM

well, I got it as


You have 2 brothers and sisters and 26 children.

so first value is fix but the second is varying.

I think the first value is ASCII for something called "start of the text"
It skips the second scanf because of input buffer problem. [you can get info about what it is on internet easily.]

btw, from where you found this interview type of question? Plus actually scanf is a not recommended function to use to get inputs from user prompt. See here

I don't know why people waste their time in finding why some library functions works/ not works in specific way, if they don't work then they don't work. Why you want to waste time in digging why they don't work, if finding the reason is not going to help you. If experts recommend not to use them then don't use them and go with workaround instead of wasting time in finding the reasons. Improving on application development/programming logic development skills can help them more.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1