#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    9
    Rep Power
    0

    Check the output of this Program


    #include<stdio.h>
    int main()
    {
    char gender,degree;
    int experience,salary;
    scanf("%c %c %d",&gender,&degree,&experience);
    salary=(gender=='b'&&degree=='m'?
    (experience>=10?printf("Salary is 15000"):printf("Salary is 10000")):
    (gender=='b'&&degree=='g'?(experience>=10?printf("Salary is 10000"):printf("Salary is 7000")):
    (degree=='m'?(experience>=10?printf("Salary is 12000"):printf("Salary is 10000")):
    (degree=='g'?(experience>=10?printf("Salary is 9000"):printf("Salary is 6000")):printf("Love")))));
    printf("%d",salary);
    return EXIT_SUCCESS;
    }


    Actually any printf statement that gets executed a number like 14 or 15 is ssociated with it. Someone please help.

    Comments on this post

    • salem disagrees : Why do you keep posting the same code WITHOUT [code][/code] tags - unreadable mess of smilies -> IGNORED
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222
    1. Use CODE TAGS! Unformatted code is unreadable! If you post unreadable code, then we will not be inclined to attempt to read it. So use CODE TAGS!

    2. Disable smilies! Those useless doo-dads add nothing and messes up code, making your unreadable code even more unreadable! So disable them!

    3. Just what is this supposed to do?:
    Code:
       salary=(gender=='b'&&degree=='m'?
               (experience>=10?printf("Salary is 15000"):printf("Salary is 10000")):
               (gender=='b'&&degree=='g'?(experience>=10?printf("Salary is 10000"):printf("Salary is 7000")):
                                          (degree=='m'?(experience>=10?printf("Salary is 12000"):printf("Salary is 10000")):
                                                    (degree=='g'?(experience>=10?printf("Salary is 9000"):printf("Salary is 6000")):printf("Love")))));
    With over 20 years of professional experience with C, I sure can't make any sense of it and I very much doubt that you can either. If you want to write Lisp code (with its very deeply embedded style requiring large numbers of closing parentheses), then stick to Lisp. If you want to write C, then write code that you can understand.

    Break it up into multiple lines so that you will be able to read your own code. Replace that hyper-embedded conditional operator with explicit if- and else if- statements.

    And disable those stupid smilies!
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222
    Actually any printf statement that gets executed a number like 14 or 15 is ssociated with it. Someone please help.
    Take a look at a normal readable use of the conditional operator:
    Code:
        a = (x > 5) ? 42 : 0;
    What does that do? Well, first it evaluates (x > 5), which will be 1 (or at least non-zero) if true and 0 if false. If true, then the entire conditional operator expression will evaluate to 42, and if false it will evaluate to 0. Then the entire conditional operator expression' evaluated value is assigned to a. The upshot of all that is that if x>5 is true, then a will be assigned a value of 42 , or a value of 0 if not.

    Now let's look at a readable form from your ungodly mess (I prefer to enclose the condition in parentheses, which I believe makes it more readable and more readily identifiable as a condition):
    Code:
        salary = (experience>=10) ? printf("Salary is 12000") : printf("Salary is 10000");
    OK, what will that entire expression on the right side (the rvalue evaluate to? It will evaluate to the return value of either call to printf. Well, what value does printf return? From http://linux.die.net/man/3/printf:
    Originally Posted by man page printf
    Return value

    Upon successful return, these functions return the number of characters printed (excluding the null byte used to end output to strings).
    So then, printf("Salary is 12000") prints 15 characters and so will return 15. Ditto for printf("Salary is 10000");. Therefore, regardless of how the condition evaluates out, salary will be set to 15.

    Since all you appear to be doing is setting salary to the number of characters printed out, it will be set either to 14 or 15.

    Duh?
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    9
    Rep Power
    0
    Originally Posted by dwise1_aol
    Take a look at a normal readable use of the conditional operator:
    Code:
        a = (x > 5) ? 42 : 0;
    What does that do? Well, first it evaluates (x > 5), which will be 1 (or at least non-zero) if true and 0 if false. If true, then the entire conditional operator expression will evaluate to 42, and if false it will evaluate to 0. Then the entire conditional operator expression' evaluated value is assigned to a. The upshot of all that is that if x>5 is true, then a will be assigned a value of 42 , or a value of 0 if not.

    Now let's look at a readable form from your ungodly mess (I prefer to enclose the condition in parentheses, which I believe makes it more readable and more readily identifiable as a condition):
    Code:
        salary = (experience>=10) ? printf("Salary is 12000") : printf("Salary is 10000");
    OK, what will that entire expression on the right side (the rvalue evaluate to? It will evaluate to the return value of either call to printf. Well, what value does printf return? From http://linux.die.net/man/3/printf:

    So then, printf("Salary is 12000") prints 15 characters and so will return 15. Ditto for printf("Salary is 10000");. Therefore, regardless of how the condition evaluates out, salary will be set to 15.

    Since all you appear to be doing is setting salary to the number of characters printed out, it will be set either to 14 or 15.

    Duh?

    See the output I get is like 1500015, 12000012. Not just the number of characters in the printf statement. Also I am the absolute beginner in the programming world so I may make some mistakes.

    I am just curious that why only in this code it gives the number of characters in the printf statement. I wrote same program using if else but there was no such thing.
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,711
    Rep Power
    480
    Great, you're learning to nest question mark operators. As I answered this question on your "clarification needed" post:
    Originally Posted by LambertDW
    Please tell the forum you can see the difference between these two statements.
    Code:
      salary=(gender=='b'&&degree=='m'?
    	  (experience>=10?printf("Salary is 15000"):printf("Salary is 10000")):
    	  (gender=='b'&&degree=='g'?(experience>=10?printf("Salary is 10000"):printf("Salary is 7000")):
    	   (degree=='m'?(experience>=10?printf("Salary is 12000"):printf("Salary is 10000")):
    	    (degree=='g'?(experience>=10?printf("Salary is 9000"):printf("Salary is 6000")):printf("Love")))));
    
      salary=(gender=='b'&&degree=='m'?
    	  (experience>=10?15000:10000):
    	  (gender=='b'&&degree=='g'?(experience>=10?10000:7000):
    	   (degree=='m'?(experience>=10?12000:10000):
    	    (degree=='g'?(experience>=10?9000:6000):printf("Love")))));
    [code]Code tags[/code] are essential for python code and Makefiles!
  10. #6
  11. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222
    Originally Posted by coolguy29
    Also I am the absolute beginner in the programming world so I may make some mistakes.
    Then write like a beginner and learn how to do it right! Would an absolute beginner Lindy Hopper upon seeing the pros doing aeriels (which he flings her about his body and tosses her straight up in the air for her to slide down his back whereupon he catches her between his legs and pulls her back out in front of him) be able to go out and do it? No, of course not. For one thing, he could very severely injure his partner and himself. For that matter, just dipping her without knowing what he's doing could seriously injure his back. Rather, that beginning Lindy Hopper would learn the basics first and then have the pros teach him how to do aeriels. You can't just go out and pull stupid stunts without knowing what you're doing.

    Have you ever learned a foreign language, meaning a human language? Do you start out creating monstrously long, run-on sentences that are incredibly convoluted? No, you start out with simple statements. Which you learn to transform into negative statements, and then into questions, and then into commands. And then you learn to form simple compound statements. And then you learn to add dependent clauses. And so on. Though you never do learn to create monstrously long, run-on sentences that are incredibly convoluted, because nobody, not even a native speaker, ever speaks like that! Because the main goal in learning to speak that language is to make yourself understood! If what you say or write is incomprehensible, then you have failed!

    Learning computer languages is little different, though rendered somewhat easier by not needing to develop oral and aural skills in that language. You start with simple statements in simple programs. Then you learn to add structures, etc. The main complexity of your programs will be in their size and organization; the actual individual statements used will remain relatively simple. Statements such as your conditional operator monstrosity are to be avoided like the plague because they are incomprehensible and unmaintainable!

    In real life, in constrast to class-room exercises such as your programming assignments, every program has to be maintained. That means that as it's used, bugs will be found, and improvements will be requested, and changes in other software that it's dependent on will require changes, etc. Also, a new programming project very often will use another program or parts of several programs as its starting baseline. Indeed, the vast majority of the life of a program and the vast majority of cost and labor devoted to that program will be in maintaining that program. Therefore, the programmer needs to do everything that he can do to make the maintenance of his program easier and less costly.

    And since in real life you would be working as a member of a team of programmers, you will most of the time have to maintain other people's code, just as most of your code will be maintained by other people. That means that making your code readable and easier to understand is of the utmost importance. Just as important, if not more so, is that you make your code easy for you to understand, because after a few years or even a few months you will have forgotten your own code and will not remember what you were thinking when you created such massively convoluted statements as your conditional operator monstrosity. This is also an argument for you to use meaningful variable names and to write meaningful comments, because even you will need them in order to understand what you had written.

    Not to mention that when you turn your assignments in to your teacher for grading, he will need to be able to read and understand your code in order to give you a grade. Understandable code will get you a higher grade that an incomprehensible mess.


    Originally Posted by coolguy29
    I am just curious that why only in this code it gives the number of characters in the printf statement. I wrote same program using if else but there was no such thing.
    The reason why your previous program (I had to go hunting for it, no thank you very much) did not exhibit this problem is because you did not have any salary variable to try to print out. So, assuming that your monstrosity works the same as the much more readable code in your previous program did, all you need to return this code to that previous functionality would be to eliminate this one line of code:
    printf("%d",salary);
    There, the problem as you just stated has been solved!

    Though somehow I suspect that that is not what you want to do. What you need to do to set salary is that you need to set salary. As in the assignment statement, salary = 12000;. Go back to your previous program and add those assignment statements! You can have multiple statements dependent on an if-statement when you enclose them in braces; eg:
    Code:
            if(exp>=10)
            {
                printf("Salary is 15000");
                salary = 15000;
            }
            else
            {
                printf("Salary is 10000");
                salary = 10000;
            }
    There is a way you could do the same thing in that condition operator monstrosity of yours, but that would only grow it into an even more horrifying monster. Viktor, you need to destroy that monster and promise to never create another one ever again!

    Comments on this post

    • ptr2void agrees
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    9
    Rep Power
    0
    Originally Posted by dwise1_aol
    Then write like a beginner and learn how to do it right! Would an absolute beginner Lindy Hopper upon seeing the pros doing aeriels (which he flings her about his body and tosses her straight up in the air for her to slide down his back whereupon he catches her between his legs and pulls her back out in front of him) be able to go out and do it? No, of course not. For one thing, he could very severely injure his partner and himself. For that matter, just dipping her without knowing what he's doing could seriously injure his back. Rather, that beginning Lindy Hopper would learn the basics first and then have the pros teach him how to do aeriels. You can't just go out and pull stupid stunts without knowing what you're doing.

    Have you ever learned a foreign language, meaning a human language? Do you start out creating monstrously long, run-on sentences that are incredibly convoluted? No, you start out with simple statements. Which you learn to transform into negative statements, and then into questions, and then into commands. And then you learn to form simple compound statements. And then you learn to add dependent clauses. And so on. Though you never do learn to create monstrously long, run-on sentences that are incredibly convoluted, because nobody, not even a native speaker, ever speaks like that! Because the main goal in learning to speak that language is to make yourself understood! If what you say or write is incomprehensible, then you have failed!

    Learning computer languages is little different, though rendered somewhat easier by not needing to develop oral and aural skills in that language. You start with simple statements in simple programs. Then you learn to add structures, etc. The main complexity of your programs will be in their size and organization; the actual individual statements used will remain relatively simple. Statements such as your conditional operator monstrosity are to be avoided like the plague because they are incomprehensible and unmaintainable!

    In real life, in constrast to class-room exercises such as your programming assignments, every program has to be maintained. That means that as it's used, bugs will be found, and improvements will be requested, and changes in other software that it's dependent on will require changes, etc. Also, a new programming project very often will use another program or parts of several programs as its starting baseline. Indeed, the vast majority of the life of a program and the vast majority of cost and labor devoted to that program will be in maintaining that program. Therefore, the programmer needs to do everything that he can do to make the maintenance of his program easier and less costly.

    And since in real life you would be working as a member of a team of programmers, you will most of the time have to maintain other people's code, just as most of your code will be maintained by other people. That means that making your code readable and easier to understand is of the utmost importance. Just as important, if not more so, is that you make your code easy for you to understand, because after a few years or even a few months you will have forgotten your own code and will not remember what you were thinking when you created such massively convoluted statements as your conditional operator monstrosity. This is also an argument for you to use meaningful variable names and to write meaningful comments, because even you will need them in order to understand what you had written.

    Not to mention that when you turn your assignments in to your teacher for grading, he will need to be able to read and understand your code in order to give you a grade. Understandable code will get you a higher grade that an incomprehensible mess.




    The reason why your previous program (I had to go hunting for it, no thank you very much) did not exhibit this problem is because you did not have any salary variable to try to print out. So, assuming that your monstrosity works the same as the much more readable code in your previous program did, all you need to return this code to that previous functionality would be to eliminate this one line of code:
    printf("%d",salary);
    There, the problem as you just stated has been solved!

    Though somehow I suspect that that is not what you want to do. What you need to do to set salary is that you need to set salary. As in the assignment statement, salary = 12000;. Go back to your previous program and add those assignment statements! You can have multiple statements dependent on an if-statement when you enclose them in braces; eg:
    Code:
            if(exp>=10)
            {
                printf("Salary is 15000");
                salary = 15000;
            }
            else
            {
                printf("Salary is 10000");
                salary = 10000;
            }
    There is a way you could do the same thing in that condition operator monstrosity of yours, but that would only grow it into an even more horrifying monster. Viktor, you need to destroy that monster and promise to never create another one ever again!
    I don't have the words to express the thanks for this wonderful post. God willing I will be careful in future and write the easily readable code. I also understood what was the problem with the output. Thanks for that also. Stay blessed.

IMN logo majestic logo threadwatch logo seochat tools logo