Thread: C Pop Quiz?

  1. #1
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263

    C Pop Quiz? You Think You Know It All?

    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?
    Last edited by no-one; 08-29-2001 at 11:56 PM.

  2. #2
    Registered User Coder's Avatar
    Join Date
    Aug 2001
    Location
    Cairo, Egypt
    Posts
    128
    use scanf("%d %d %d",var,&var,var);
    Although it's not a good idea, because it can mess some other data :

    Code:
    // ----------------------------------------------------------------
    // 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
    	getch();
    	// 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
    Muhammad Haggag

  3. #3
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    Rather curious... Why did you think I had the best chance, no-one?

    Congratz on being the last (finally)

  4. #4
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    Code:
    #include<stdio.h>
    int main()
    {
    	int var;
    
    	scanf("%*d%d%*d",&var);
    	printf("%d",var);
    
    	return 0;
    }
    I compile code with:
    Visual Studio.NET beta2

  5. #5
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    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...
    Last edited by no-one; 08-30-2001 at 12:57 PM.

  6. #6
    Registered User Coder's Avatar
    Join Date
    Aug 2001
    Location
    Cairo, Egypt
    Posts
    128

    The second variable

    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'
    Muhammad Haggag

  7. #7
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    i see... then i must say cleverly done...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. push n pop
    By salmansalman in forum C++ Programming
    Replies: 3
    Last Post: 05-27-2008, 09:41 AM
  2. Queues
    By ramayana in forum C Programming
    Replies: 22
    Last Post: 01-01-2006, 02:08 AM
  3. pop up menu problem
    By bigtamscot in forum Windows Programming
    Replies: 5
    Last Post: 06-10-2003, 01:46 AM
  4. pop function
    By Troll_King in forum C Programming
    Replies: 12
    Last Post: 10-15-2001, 04:45 AM
  5. pop quiz on boolean algebra
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 09-27-2001, 07:11 AM