• Please review our updated Terms and Rules here

does this program work?

jjzcp

Experienced Member
Joined
Sep 14, 2005
Messages
123
Location
on the bald a$$ prarie
Will this program work in standardized C compilers?

#include<stdio.h>
main()
{
float a;
int b;
a = 5;
b = 2;
printf("%f", a/b);
}

It should work right, because even though an interger, and float is used, the resulting number is a float. It works in my turbo c compiler, but not sure in other new compilers.
 
You might get a warning on some compilers, but the result should be cast to the appropriate type.
 
jjzcp wrote:

> Will this program work in standardized C compilers?

Code:
#include<stdio.h>
main()
{
float a;
int b;
a = 5;
b = 2;
printf("%f", a/b);
}

> It should work right, because even though an
> interger, and float is used, the resulting number is
> a float. It works in my turbo c compiler, but not
> sure in other new compilers.

It might work fine on some compilers, though others it might
be problematic - cause I don't recall printf having the
ability of doing calculations - certainally Borland C maybe
able to do this - though I don't recall.

Might be safer to have 3 variables. Call a & b intergers & c a
float. Divide a & b & put the answer into c - then printf the
contents of c (I'd be more inclined to do it like that).

CP/M User.
 
It is not printf that does any calculations; the expression will be evaluated and the result will be passed on the fly to the printf mechanism.

I agree with Erik, you may get some typecast warnings, but it should compile. Some compilers may also complain that you don't have a return type on the main() function, i.e.

int main() {
...
return 0;
}

or

void main() {
...
}

The old standard was to always assume int when nothing was specified, but modern compilers will complain.
 
carlsson wrote:

> It is not printf that does any calculations; the
> expression will be evaluated and the result will be
> passed on the fly to the printf mechanism.

Well you can blame my C teacher - cause I distinctly remember
them applying the calculation before the printf. In any event
it's messy use of the C language and one good reason as to why
it's misleading for anyone who has to read it!

CP/M User.
 
Dunno about messy use. If you have a need to pass the same value more than once, I'd consider allocating a variable and assign it the value of the expresssion. If you should allocate (or even reuse) a variable for every single calculation, function call or other data, it would get very complicated and hard to follow the code.
 
jjzcp wrote:

> So, is it ok to let there be calculations done in the
> printf function in all the popular, and up to date C
> compilers?

I think the main thing is to making sure it'll work on some
mainstream compiler - which is easily obtainable & free.
What's that Free GNU C compiler - think it works on a number
of platforms (except CP/M :-o), a number of C compilers were
written for CP/M though (if you want to focus on that OS).
Portability wise - I've seen C compilers choke on C code - it
ain't an impressive language when it comes to portability - at
best you have some C code found in mainstream C (sadily this
varies).

CP/M User.
 
jjzcp said:
So, is it ok to let there be calculations done in the printf function in all the popular, and up to date C compilers?
Yes, all C compilers that adhere to either the old or new (CX99 IIRC) standard should work. It is not only about printf, you should be able to use any expression as an argument to a function call:

Code:
#include <stdio.h>
 
int min(int a, int b) {
   if (a<b) return a;
   else return b;
}
 
int main(void) {
  int a,b,c;
  a=5;
  b=2;
  c=7;
  printf("The smallest number multiplied by 14 is: %d\n",
          min(min(a,b),c)*14);
  return 0;
}

It was a little while since I programmed in C, but I think this short program should be syntactically correct and in the end print 28.
 
Back
Top