Thread: how it works..!! is it a flaw in c ??

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    33

    how it works..!! is it a flaw in c ??

    I was tried to execute printf function with different possible arguments. In that time I tried execute the following printf statement after I got surprised.. how it is possible?????
    [Code]
    Int a=5;
    Printf (" %d",a)-1;
    Scanf("%d",&a)-1;
    Printf (" %d",a)-1;
    [\code]
    These printf and scanf are works normally..how it is possible???

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    What output surprised you? Can you be more specific?

    P.S. All tags on the forum must be in all lowercase letters, in order to work.

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    In future, please post your actual code instead of retyping it (incorrectly with initial capital letters!).

    I assume you're wondering why putting a -1 after them doesn't give an error. It's because, although useless, it isn't actually an error. printf and scanf return an int. You are subtracting one from that int, and then throwing the result away.

    If you turn up the warning level of your compiler, it should say something about the value not being used.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by Adak View Post
    P.S. All tags on the forum must be in all lowercase letters, in order to work.
    Actually, the case of the letters doesn't matter. It's the fact that he/she used a backslash (\) instead of a forward slash (/) in the closing tag. It should be:

    [code]
    int a = 5;
    printf(" %d", a) - 1;
    ...
    [/code]
    Last edited by anduril462; 08-12-2013 at 02:48 PM.

  5. #5
    Registered User
    Join Date
    Jan 2011
    Posts
    144
    Quote Originally Posted by oogabooga View Post
    In future, please post your actual code instead of retyping it (incorrectly with initial capital letters!).

    I assume you're wondering why putting a -1 after them doesn't give an error. It's because, although useless, it isn't actually an error. printf and scanf return an int. You are subtracting one from that int, and then throwing the result away.

    If you turn up the warning level of your compiler, it should say something about the value not being used.
    thanks.
    To the OP, please try

    Code:
    #include <stdio.h>
    
    int main()
    {
    	int x;
    	int a=5;
    
    	x = printf(" %d",a)-1;
    	printf("%d",x);
    		
    	getchar();
    	return 0;
    }
    my output is 51

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Try using a manual or online source:

    Upon successful return, these functions return the number of characters printed (excluding the null byte used to end output to strings). ... If an output error is encountered, a negative value is returned.
    Of course, you can do math with these values.

  7. #7
    Registered User
    Join Date
    Aug 2012
    Posts
    33
    Quote Originally Posted by oogabooga View Post
    In future, please post your actual code instead of retyping it (incorrectly with initial capital letters!).

    I assume you're wondering why putting a -1 after them doesn't give an error. It's because, although useless, it isn't actually an error. printf and scanf return an int. You are subtracting one from that int, and then throwing the result away.

    If you turn up the warning level of your compiler, it should say something about the value not being used.
    I use GCC compiler it doesn't throw any warning like you said...

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    No you don't. GCC does not like exactly what you showed us.
    Code:
    C:\Users\Josh2>more veera.c
    Int a=5;
    Printf (" %d",a)-1;
    Scanf("%d",&a)-1;
    Printf (" %d",a)-1;
    
    C:\Users\Josh2>gcc veera.c
    veera.c:1:1: error: unknown type name 'Int'
    veera.c:2:9: error: expected declaration specifiers or '...' before string const
    ant
    veera.c:2:15: error: expected declaration specifiers or '...' before 'a'
    veera.c:3:7: error: expected declaration specifiers or '...' before string const
    ant
    veera.c:3:12: error: expected declaration specifiers or '...' before '&' token
    veera.c:4:9: error: expected declaration specifiers or '...' before string const
    ant
    veera.c:4:15: error: expected declaration specifiers or '...' before 'a'
    Which means you typed what you thought was the code from memory.

    And, even assuming the best code, turning warnings on produces the following:
    Code:
    C:\Users\Josh2>more veera.c
    #include <stdio.h>
    int main()
    {
      int a=5;
      printf (" %d",a)-1;
      scanf("%d",&a)-1;
      printf (" %d",a)-1;
      return 0;
    }
    
    C:\Users\Josh2>gcc -Wall veera.c
    veera.c: In function 'main':
    veera.c:5:19: warning: value computed is not used [-Wunused-value]
    veera.c:6:17: warning: value computed is not used [-Wunused-value]
    veera.c:7:19: warning: value computed is not used [-Wunused-value]
    Many compilers, unfortunately, arent configured to be this stringent by default. I suspect vendors do this on purpose in order to make compiler options useful. Learning how to use your IDE or compiler options is important.
    Last edited by whiteflags; 08-12-2013 at 08:44 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Design flaw in C++?
    By @nthony in forum C++ Programming
    Replies: 24
    Last Post: 10-20-2008, 07:47 PM
  2. design flaw
    By Bleech in forum Windows Programming
    Replies: 2
    Last Post: 10-23-2007, 11:10 PM
  3. yet another unexplainable flaw
    By stormbreaker in forum C++ Programming
    Replies: 4
    Last Post: 06-01-2003, 12:56 PM
  4. MAJOR security flaw in xp!!
    By RoD in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 02-17-2003, 07:14 AM
  5. is this a design flaw
    By blight2c in forum C++ Programming
    Replies: 5
    Last Post: 03-20-2002, 12:33 AM