Thread: C code for operation of a switch

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    12

    C code for operation of a switch

    How to write a code for the operation of a hardware switch ?

    It can be a simple digital switch to switch it ON and OFF.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    How about saying something about what OS/Compiler/interfaces you've got?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    12
    You can assume.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Is this what you answered on the interview?

    Code:
    #define ON 1
    #define OFF 0
    
    #define ON_OFF_SWITCH_ADDR 0xC0000400
    
    int *on_off_switch_ptr = (int *)ON_OFF_SWITCH_ADDR;
    
    int main(int argc, char **argv)
    {
       if (argc != 2)
          printf("Invalid arguments\n");
       else {
           if (strcmp(argv[1], "ON") == 0)
             *on_off_swtich_ptr = ON;
           else if (strcmp(argv[1], "OFF") == 0)
             *on_off_swtich_ptr = OFF; 
           else printf("Choose either ON or OFF\n");
       }
       return 0;
    }
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    12
    I had told the same n of course it is correct.

    But i was expecting anyother approach for this. Is there any other approach for d same ?

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    There is any number of answers - how you do it depends on what the enviroment is.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Yes there is, look up the truth tables for the bitwise operators & | ^
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 23
    Last Post: 04-20-2009, 07:35 AM
  2. Explain this C code in english
    By soadlink in forum C Programming
    Replies: 16
    Last Post: 08-31-2006, 12:48 AM
  3. Replies: 7
    Last Post: 05-25-2006, 12:51 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 4
    Last Post: 01-16-2002, 12:04 AM