Thread: Is this printf output for anything?

  1. #1
    Registered User
    Join Date
    Sep 2023
    Posts
    2

    Is this printf output for anything?

    Hallo,
    I have this this example, but is this usefull in practical use?
    Normaly I would work with IF or CASE, but this version can only answer 0 or 1.
    But can I use it an put the result in a variable or something with this system?
    Orf write different text statements?


    Code:
    #include <stdio.h>
    #include <conio.h>
    int main(void){
        int temper = 20;
        printf("Ist kleiner als 23°C %d\n", temper < 23);
        printf("Ist größer als 23°C %d\n", temper  > 23);
        return 0;     
    }
    Last edited by Salem; 09-20-2023 at 04:18 AM. Reason: line wrapping

  2. #2
    Registered User
    Join Date
    Sep 2023
    Posts
    2
    HM, ok, this doesn't work
    I hoped it is like an IF oder Case

    Code:
        c=5 == 8 > 6;
        printf("C ist %d\n", c);
    But why is always C=0?

    Code:
    	int c=99;
    
    
    
    
    	c=5 == 6 > 1;
    	printf("C ist %d\n", c);
    The result is always 0?! Why not 99?
    Last edited by corado; 09-20-2023 at 03:17 AM.

  3. #3
    Registered User
    Join Date
    Sep 2022
    Posts
    55
    Comarison operations yield an int with 0 representing logical false, and 1 (or any other value) representing logical true. You have to specify string literals that you conditionally pass to printf, along with the %s format specifier for a string. Like
    Code:
    #include <stdio.h>
    
    int main(void)
    {
      static const char eq[] = "gleich";
      static const char lt[] = "kleiner als";
      static const char gt[] = "größer als";
    
    
      int temper = 25;
      printf("Ist %s 23°C.\n", temper == 23 ? eq : (temper < 23 ? lt : gt));
    }
    I used inlined ternary operators here. You could also use if-else or switch-case to assign the string to a variable first.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. explain the output of printf
    By royroy in forum C++ Programming
    Replies: 1
    Last Post: 06-05-2015, 08:18 AM
  2. printf style output for C++
    By Mozza314 in forum C++ Programming
    Replies: 2
    Last Post: 11-09-2011, 12:31 AM
  3. Formatting output with printf
    By Sn0wcra5h in forum C Programming
    Replies: 1
    Last Post: 01-25-2010, 03:44 PM
  4. printf output
    By shyguy24x7 in forum C Programming
    Replies: 3
    Last Post: 01-31-2009, 02:01 PM
  5. Printf Output Confusion
    By simpleid in forum C++ Programming
    Replies: 2
    Last Post: 11-16-2006, 11:36 AM

Tags for this Thread