printf() Problem

Hi
I have run the following program:
main()
{
printf(3+"london");
}
output comes ==> don
that's understandable but the i am not getting one point that how this matter written in printf() can be syntactically correct????I think that printf takes format string only then how can it be right??

thanx


Comments

  • try this!

    printf("%d london", 3);
    that WILL work

    Thanks
    Leon Yuhanov
    GSCo. & MSP
    Melbourne, Australia
    Visit me @ http://home.iprimus.com.au/yuhanov/gsc/gschome01.html
    Email me @ yuhanov@iprimus.com.au

  • : Hi
    : I have run the following program:
    : main()
    : {
    : printf(3+"london");
    : }
    : output comes ==> don
    : that's understandable but the i am not getting one point that how this matter written in printf() can be syntactically correct????I think that printf takes format string only then how can it be right??
    :
    : thanx
    :
    :

    [blue]The format string (first argument to printf()) can be any text -- there is no limit to what the format string can contain. For example:[/blue]
    [code]
    double dollars = 18000000.00;
    printf("You have win %.2f dollars in the NY Lottery!", dollars);
    [/code]

    :

  • printf(3+"london");
    is very much syntactically different from printf("%d london", 3);
    how we can write 3+ outside " " in printf()...is this syntactically right in ur view???



    : try this!
    :
    : printf("%d london", 3);
    : that WILL work
    :
    : Thanks
    : Leon Yuhanov
    : GSCo. & MSP
    : Melbourne, Australia
    : Visit me @ http://home.iprimus.com.au/yuhanov/gsc/gschome01.html
    : Email me @ yuhanov@iprimus.com.au
    :
    :


  • printf(3+"london");
    is very much syntactically different.....
    how we can write 3+ outside " " in printf()...is this syntactically right in ur view???
    3 is not the text..but a integer number..

    : : Hi
    : : I have run the following program:
    : : main()
    : : {
    : : printf(3+"london");
    : : }
    : : output comes ==> don
    : : that's understandable but the i am not getting one point that how this matter written in printf() can be syntactically correct????I think that printf takes format string only then how can it be right??
    : :
    : : thanx
    : :
    : :
    :
    : [blue]The format string (first argument to printf()) can be any text -- there is no limit to what the format string can contain. For example:[/blue]
    : [code]
    : double dollars = 18000000.00;
    : printf("You have win %.2f dollars in the NY Lottery!", dollars);
    : [/code]
    :
    : :
    :
    :

  • [blue]You can find the Format Specification Fields for printf function here[/blue]
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt_format_specification_fields_.2d_.printf_and_wprintf_functions.asp

    :
    : printf(3+"london");
    : is very much syntactically different.....
    : how we can write 3+ outside " " in printf()...is this syntactically right in ur view???
    : 3 is not the text..but a integer number..
    :
    : : : Hi
    : : : I have run the following program:
    : : : main()
    : : : {
    : : : printf(3+"london");
    : : : }
    : : : output comes ==> don
    : : : that's understandable but the i am not getting one point that how this matter written in printf() can be syntactically correct????I think that printf takes format string only then how can it be right??
    : : :
    : : : thanx
    : : :
    : : :
    : :
    : : [blue]The format string (first argument to printf()) can be any text -- there is no limit to what the format string can contain. For example:[/blue]
    : : [code]
    : : double dollars = 18000000.00;
    : : printf("You have win %.2f dollars in the NY Lottery!", dollars);
    : : [/code]
    : :
    : : :
    : :
    : :
    :
    :

  • The syntax is correct because "london" is not really a string. C doesn't know the string-type, as you may know.
    "london" is actually an address somewhere in memory and that's an integer.
    Since you can add integers to integers, you can add 3 to "london", which gives you a new address. Printing from that address obviously gives you "don".
    When you code this
    [code]
    char *tst="london";
    printf("%s %d %d", tst, tst, 3+tst);
    [/code]
    You would get something like:
    [code]
    london 12345678 12345681
    [/code]


    : Hi
    : I have run the following program:
    : main()
    : {
    : printf(3+"london");
    : }
    : output comes ==> don
    : that's understandable but the i am not getting one point that how this matter written in printf() can be syntactically correct????I think that printf takes format string only then how can it be right??
    :
    : thanx
    :
    :
    :


    Greets,
    Eric Goldstein
    www.gvh-maatwerk.nl

  • [b][red]This message was edited by stober at 2003-6-30 6:24:6[/red][/b][hr]
    :
    : printf(3+"london");
    : is very much syntactically different.....
    : how we can write 3+ outside " " in printf()...is this syntactically right in ur view???
    : 3 is not the text..but a integer number..
    :
    [blue]yes, that is syntatically a correct statement. Its the same thing as this, which mearly advances the pointer by three characters.[/blue]
    [code]
    char *city = "london";
    city += 3;
    printf(city);
    [/code]


Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories