View Full Version : C Pop Quiz?

08-29-2001, 11:03 PM
heres a programming question to test your knowledge.

though govtcheese has a better chance of getting this right than anyone i won't say why. The more exp'ed C programmers should know it as well.

given a single variable of type int

int var;

where scanf() is the only input function

given three seperate integer's as input such as

10 123 7

and allowing a SINGLE call to scanf()

how do you get the only middle int?

08-30-2001, 03:26 AM
use scanf("%d %d %d",var,&var,var);
Although it's not a good idea, because it can mess some other data :

// ----------------------------------------------------------------
// File : main.cpp
// Date & Time : 8/30/2001 12:05:53 PM
// Description : Test application for scanf() when passed values instead of addresses
// Notes : None
// Compiler : Microsoft Visual C++ 6.0
// OS : Microsoft Windows 2000 proffessional
// App Mode : Console
// ----------------------------------------------------------------

// ----------------------------------------------------------------
// Headers
// getch()
#include <conio.h>
// printf(),scanf()
#include <stdio.h>

// ----------------------------------------------------------------
// Name : main
// Access : public
// Ret : int
// Desc : App entry in general, and it's the whole app here
// ----------------------------------------------------------------
int main()
int iData = 20;
// Assign address of iData to iInput, this could happen with uninitialized variables
// So assume this happened randomly.
int iInput = (int)(&iData);
// Output iData before messing its contents
printf("iData = %d\n",iData);
// Prompt for input
printf("Enter 3 numbers separated by spaces : ");
// Get the input, use the address of iInput for the middle number
scanf("%d %d %d",iInput,&iInput,iInput);
// Echo the number we got
printf("%d was extracted\n",iInput);
// Output the messed iData
printf("iData = %d, Press anykey to continue",iData);
// Wait for a keypress
// Done
return 0;

A typical run :
iData = 20
Enter 3 numbers separated by spaces : 0 15 30
15 was extracted
iData = 30, Press anykey to continue

08-30-2001, 06:25 AM
Rather curious... Why did you think I had the best chance, no-one?

Congratz on being the last (finally)

08-30-2001, 06:30 AM
int main()
int var;


return 0;

08-30-2001, 10:51 AM
coder you only had on variable to use...
var your second int was illegal

Kudos to Witch_King you got it right.

>Rather curious... Why did you think I had the best chance, no-one?

on a recent thread you started "getch() actin up" or something like that i used the * modifier in scanf to remove the '\n' that was causing your getch() not to work so i figured you might put it together more eaisly...

>Congratz on being the last (finally)

thank you, thank you please please hold your applause...

08-30-2001, 10:31 PM
coder you only had on variable to use...
var your second int was illegal
I used the second int 'iData' to demonstrate that using scanf that way causes messing up data elsewhere, for the answer itself I wrote :

use scanf("%d %d %d",var,&var,var);
which uses one variable 'var'

08-30-2001, 10:33 PM
i see... then i must say cleverly done...