Thread: Calling to Function & Expression question

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    58

    Smile Calling to Function & Expression question

    Hi,
    I Have 2 question2 regarding this code:
    1 - int_to_bin(c); // Why &c not working here ?
    2 - str[i] = (num&1)?'1':'0'; // What this expression do ? ?'1':'0'

    thanks

    Code:
    #include<stdio.h>
    void int_to_bin(int num);
    int main()
    {
       int c;
       printf("nEnter a number :");
       scanf("%d",&c);
       int_to_bin(c);  // Why &c not working here ?
       return 0;
    }
    
    void int_to_bin(int num) {
      char str[9] = {0};
      int i;
      for (i=7; i>=0; i--) {
        str[i] = (num&1)?'1':'0';  // What this expression do ?    ?'1':'0'
        num >>= 1;
      }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    In main, what is the type of c? In int_to_bin, what is the type of num?

    For the latter, search the Web for "ternary operation".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. primary expression error when calling operation
    By elsparko in forum C++ Programming
    Replies: 2
    Last Post: 05-18-2010, 12:43 PM
  2. Replies: 15
    Last Post: 06-09-2009, 02:19 AM
  3. Replies: 3
    Last Post: 03-24-2008, 07:31 AM
  4. Question about function calling
    By homeyg in forum C++ Programming
    Replies: 3
    Last Post: 12-17-2004, 05:31 PM
  5. Question on function syntax and calling function
    By cbrman in forum C Programming
    Replies: 10
    Last Post: 10-05-2003, 05:32 PM