-5

Here is the code I have written

#include<stdio.h>

main( )
{
    float a = 15.5 ;
    char ch = 'd' ;
    printit ( a, ch );
}
printit ( a, ch )
{
    printf ( "\n%f  %c ", a, ch ) ;
}

And the output is:

15.500000 ─

Here I am expecting the char d to be printed in place of -.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
Krish
  • 1
  • 3
  • 3
    1) Format your code properly. 2) You should get a warning already for a missing declaration (aka prototype) of `printit`. Also this is prehistoric C (aka K&R-C). **Never ever** use this. It is outdated since ca. 27 years. And the code invokes undefined behaviour. – too honest for this site Apr 21 '16 at 17:03
  • **Never** write C code in this way .. besides the problem of undefined behavor, this code is really hard to understand. – jboockmann Apr 21 '16 at 17:17
  • you didn't have `printit` prototype/declaration before it's used. And mind your indentation – phuclv Apr 21 '16 at 23:58

2 Answers2

4

TL;DR, your code invokes undefined behavior.

You're using a dangerous (thankfully, now non-standardRef) way of getting the variable types, that is, type defaults to int.

As your variables are missing datatype definitions, they default to int. So, inside printtit(), a and ch are of type int.

Now, by passing a as the argument for %f, you invoke UB already. The program (and it's output) can neither be trusted nor be justified in any way.

Note : Enable compiler warnings and pay attention to them!


Ref: Quoting from C11 (available in C99 also),

Major changes in the second edition included:

. . . .

— remove implicit int

Community
  • 1
  • 1
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
0

Although the question has been solved, I wanted to add a correctly rewritten version of your code:

#include<stdio.h>

void printit (float, char); // function declaration

int main(void)
{
    float a = 15.5 ;
    char ch = 'd' ;
    printit (a, ch);
    return (0);
}
void printit (float a, char ch)
{
    printf("\n%f  %c ", a, ch) ;
}
jboockmann
  • 1,011
  • 2
  • 11
  • 27