printf of the "%d" string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jorba101
    New Member
    • Jun 2008
    • 86

    printf of the "%d" string

    How to printf the string "%d" itself?

    I wonder how should I escape the %, since I see no escape char for % (in http://www.acm.uiuc.ed u/webmonkeys/book/c_guide/1.1.html)

    Thanks!
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    There is not escape code for % because it doesn't need one. However there is a format code for % because in the context of a printf it does need one.

    Read this: http://www.acm.uiuc.edu/webmonkeys/b...2.html#printf]

    Comment

    • gpraghuram
      Recognized Expert Top Contributor
      • Mar 2007
      • 1275

      #3
      To print % you should use % as the escape sequence.
      Code will be
      [code=c]
      printf("%%");
      [/code]
      will print %

      Raghu

      Comment

      • shivapadma
        New Member
        • Mar 2007
        • 40

        #4
        char a[3]="%d";
        printf("%s",a);

        by using above two lines u can print %d itself

        Comment

        • gpraghuram
          Recognized Expert Top Contributor
          • Mar 2007
          • 1275

          #5
          Originally posted by shivapadma
          char a[3]="%d";
          printf("%s",a);

          by using above two lines u can print %d itself

          I think the %% option would be much easier as u need not declare the local variable.

          Raghuram

          Comment

          • jorba101
            New Member
            • Jun 2008
            • 86

            #6
            Yep,

            In the end I did something like

            Code:
            printf("%%d");
            It was meant to construct the string to pass later on to an fscanf() while reading a file.

            Thanks,

            Comment

            • gpraghuram
              Recognized Expert Top Contributor
              • Mar 2007
              • 1275

              #7
              Originally posted by jorba101
              Yep,

              In the end I did something like

              Code:
              printf("%%d");
              It was meant to construct the string to pass later on to an fscanf() while reading a file.

              Thanks,


              Hi,
              You want to print % to console or have the % in a string to pass it to another function?

              Raghu

              Comment

              • jorba101
                New Member
                • Jun 2008
                • 86

                #8
                I pass it to another function. Actually I was using sprintf() for that.

                Comment

                • gpraghuram
                  Recognized Expert Top Contributor
                  • Mar 2007
                  • 1275

                  #9
                  Originally posted by jorba101
                  I pass it to another function. Actually I was using sprintf() for that.
                  If u are using sprintf then u can follow the same idea

                  [code=c]
                  char arr[100];
                  sprinf(arr,"%%" );

                  [/code]

                  Then u can pass arr to the function u are using.


                  Raghu

                  Comment

                  Working...