Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #41365

Re: why printf("%d", arg) works with arg of type int, short, char

From jgk@panix.com (Joe keane)
Newsgroups comp.lang.c
Subject Re: why printf("%d", arg) works with arg of type int, short, char
Date 2014-03-05 04:07 +0000
Organization Public Access Networks Corp.
Message-ID <lf67ti$hi4$1@reader1.panix.com> (permalink)
References <fc5dad84-08d7-4c7b-bd56-5ce49b177b1b@googlegroups.com> <20140302125139.273@kylheku.com> <lf36ej$rg5$1@reader1.panix.com> <20140303162419.577@kylheku.com>

Show all headers | View raw


In article <20140303162419.577@kylheku.com>,
Kaz Kylheku  <kaz@kylheku.com> wrote:
>%f takes double.

how about this?

@ cat bars.c
#include <stdio.h>


int main(int argc, char **argv)
{
    char ac = 1;
    short as = 1;
    int ai = 1;
    long int al = 1;
    long long int aq = 1;
    float af = 1;
    double ad = 1;
    long double ax = 1;

    printf("c%d\n", sizeof (ac + ac));
    printf("s%d\n", sizeof (as + as));
    printf("i%d\n", sizeof (ai + ai));
    printf("l%d\n", sizeof (al + al));
    printf("q%d\n", sizeof (aq + aq));
    printf("f%d\n", sizeof (af + af));
    printf("d%d\n", sizeof (ad + ad));
    printf("x%d\n", sizeof (ax + ax));
    return 0;
}
@ cc -o bars bars.c
@ ./bars
c4
s4
i4
l4
q8
f4
d8
x12
@ cat bar.c
#include <stdio.h>


void test(void)
{
    int ai = 1;
    long int al = 1;
    long long int aq = 1;
    float af = 1;
    double ad = 1;
    long double ax = 1;

    printf("%d", ai);
    printf("%ld", ai);		/* line 14 */
    printf("%d", al);		/* line 15 */
    printf("%ld", al);
    printf("%Ld", al);		/* line 17 */
    printf("%ld", aq);		/* line 18 */
    printf("%Ld", aq);
    printf("%f", af);
    printf("%lf", af);		/* OK */
    printf("%f", ad);		/* OK */
    printf("%lf", ad);
    printf("%Lf", ad);		/* line 24 */
    printf("%lf", ax);		/* line 25 */
    printf("%Lf", ax);
}
@ cc --version
cc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
Copyright (C) 2011 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

@ cc -c bar.c
bar.c: In function ‘test’:
bar.c:14:5: warning: format ‘%ld’ expects argument of type ‘long int’, but argument 2 has type ‘int’ [-Wformat]
bar.c:15:5: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long int’ [-Wformat]
bar.c:17:5: warning: format ‘%Ld’ expects argument of type ‘long long int’, but argument 2 has type ‘long int’ [-Wformat]
bar.c:18:5: warning: format ‘%ld’ expects argument of type ‘long int’, but argument 2 has type ‘long long int’ [-Wformat]
bar.c:24:5: warning: format ‘%Lf’ expects argument of type ‘long double’, but argument 2 has type ‘double’ [-Wformat]
bar.c:25:5: warning: format ‘%lf’ expects argument of type ‘double’, but argument 2 has type ‘long double’ [-Wformat]

Back to comp.lang.c | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

why printf("%d", arg) works with arg of type int, short, char jononanon@googlemail.com - 2014-03-01 01:29 -0800
  Re: why printf("%d", arg) works with arg of type int, short, char jononanon@googlemail.com - 2014-03-01 01:42 -0800
    Re: why printf("%d", arg) works with arg of type int, short, char jononanon@googlemail.com - 2014-03-01 01:54 -0800
      Re: why printf("%d", arg) works with arg of type int, short, char Barry Schwarz <schwarzb@dqel.com> - 2014-03-01 07:21 -0800
        Re: why printf("%d", arg) works with arg of type int, short, char gazelle@shell.xmission.com (Kenny McCormack) - 2014-03-01 15:51 +0000
          Re: why printf("%d", arg) works with arg of type int, short, char Richard <rgrdev_@gmail.com> - 2014-03-01 17:50 +0100
            Re: why printf("%d", arg) works with arg of type int, short, char gazelle@shell.xmission.com (Kenny McCormack) - 2014-03-01 18:36 +0000
  Re: why printf("%d", arg) works with arg of type int, short, char Melzzzzz <mel@zzzzz.invalid> - 2014-03-01 11:22 +0100
    Re: why printf("%d", arg) works with arg of type int, short, char jononanon@googlemail.com - 2014-03-01 02:39 -0800
    Re: why printf("%d", arg) works with arg of type int, short, char Keith Thompson <kst-u@mib.org> - 2014-03-01 19:44 -0800
  Re: why printf("%d", arg) works with arg of type int, short, char Keith Thompson <kst-u@mib.org> - 2014-03-01 02:34 -0800
    Re: why printf("%d", arg) works with arg of type int, short, char jononanon@googlemail.com - 2014-03-01 02:49 -0800
      Re: why printf("%d", arg) works with arg of type int, short, char James Kuyper <jameskuyper@verizon.net> - 2014-03-01 09:10 -0500
    Re: why printf("%d", arg) works with arg of type int, short, char <william@wilbur.25thandClement.com> - 2014-03-01 15:38 -0800
      Re: why printf("%d", arg) works with arg of type int, short, char Keith Thompson <kst-u@mib.org> - 2014-03-01 19:47 -0800
        Re: why printf("%d", arg) works with arg of type int, short, char <william@wilbur.25thandClement.com> - 2014-03-01 21:12 -0800
        Re: why printf("%d", arg) works with arg of type int, short, char Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2014-03-02 07:47 -0700
          Re: why printf("%d", arg) works with arg of type int, short, char Kaz Kylheku <kaz@kylheku.com> - 2014-03-02 20:53 +0000
            Re: why printf("%d", arg) works with arg of type int, short, char jgk@panix.com (Joe keane) - 2014-03-04 00:23 +0000
              Re: why printf("%d", arg) works with arg of type int, short, char Kaz Kylheku <kaz@kylheku.com> - 2014-03-04 00:33 +0000
                Re: why printf("%d", arg) works with arg of type int, short, char glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2014-03-04 00:45 +0000
                Re: why printf("%d", arg) works with arg of type int, short, char jgk@panix.com (Joe keane) - 2014-03-05 04:07 +0000
                Re: why printf("%d", arg) works with arg of type int, short, char Barry Schwarz <schwarzb@dqel.com> - 2014-03-05 10:07 -0800
  Re: why printf("%d", arg) works with arg of type int, short, char "BartC" <bc@freeuk.com> - 2014-03-01 11:12 +0000
    Re: why printf("%d", arg) works with arg of type int, short, char Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-03-01 10:54 -0500
      Re: why printf("%d", arg) works with arg of type int, short, char glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2014-03-01 18:55 +0000

csiph-web