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

    Join Date
    May 2013
    Rep Power

    Trying to print an output .. what to modify?

    Hello there,
    I am trying to execute following program,

    #include <stdio.h>
    unsigned int gcd (unsigned int xMatch, unsigned yMatch);
    int main() 
     unsigned int x;
     unsigned int y;
     unsigned int gcDiv;
     printf("Enter 2 intiger number:\n");
     scanf("%u%u", &x, &y);
     gcDiv=gcd(x, y);
     printf("Greatest common divisor of %u and %u is %u\n", x, y, gcDiv);
     return 0;
    unsigned int gcd (unsigned int xMatch, unsigned int yMatch) {
     if(yMatch==0) {
      return xMatch;
     else {
      printf("yMatch = %u | xMatch= %u | xMatch%yMatch= %u", yMatch, xMatch, xMatch%yMatch );
      return gcd( yMatch, xMatch % yMatch);
    the programm works fine except when i try to get an out put from gcd funchtion ... i got an error that says
    "warning: unknown conversion type character y in format [-Wformat]

    how can i resolve this problem .. ??
    Thanks in advance ..
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Rep Power
    You are heeding warnings. Very highly commendable! Warnings are, after all, much more important than error messages.

    This is what it's having problems with (in red):
    printf("yMatch = %u | xMatch= %u | xMatch%yMatch= %u", yMatch, xMatch, xMatch%yMatch );

    printf sees that "%y" and thinks that that's a format descriptor, but it just cannot figure out what a "%y" is supposed to be. Also, printf thinks that you have a mismatch between format descriptors and arguments: 4 descriptors to 3 arguments.

    What has happened is that you inserted a percent sign into the string where it was supposed to be an actual percent sign, in this case representing the modulo operator. When you do that, you need to tell printf that it must not try to interpret that character, that you really do intend for that character to be there.

    The general term I've learned for doing that is called escaping and in the UNIX/C world, that is done with the back-slash ( \ ). A string is defined by the open and close double quotation marks, but what do you do when there needs to be a double quotes insides of the string? Eg:
    "This string has an "embedded quote"."
    would be misinterpreted and so you need to escape the quotes thus:
    "This string has an \"embedded quote\"."

    Now that I have taught you how to fish, I can freely give you a fish and know that you will be fed tomorrow. You need to escape that percent sign thus (again in red):
    printf("yMatch = %u | xMatch= %u | xMatch\%yMatch= %u", yMatch, xMatch, xMatch%yMatch );

    And keep up the good work of paying attention to those warnings!
  4. #3
  5. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Rep Power
    Actually, the escape to print a % using printf is %%, not \%
    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

IMN logo majestic logo threadwatch logo seochat tools logo