0

Build log:

warning: format '%ld' expects argument of type 'long int', but argument 2 has type 'int'

Program:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    printf("Hello world!\n");///for a new line
    printf("Hello world ");///simple
    printf("enter some values = %d %d %f",54432,54,54.76474)
    printf("%d \n",232);///integer in new line
    printf("%f \n",21.322432);///decimal in new line
    printf("%ld \n",3809);///large integer in new line
    printf("%lf \n",432758575375735.24);///large float in new line

    return 0;
}
clemens
  • 16,716
  • 11
  • 50
  • 65
Saurav Kumar
  • 39
  • 1
  • 1
  • 2
  • 3
    Integer literals (like `3809`) are by default `int`. On most modern systems `int` is a signed 32-bit type. The type of `long` *could* be signed 32 bits as well, but it's still a different type from `int`. Perhaps all you need is [a good beginners book or two](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list)? – Some programmer dude Nov 29 '17 at 04:43
  • 1
    Please refrain from posting profanity. Also, this error is fairly self-explanatory. Have you tried anything to solve the problem yourself? – elixenide Nov 29 '17 at 04:46
  • So, what is your question? – AnT stands with Russia Nov 29 '17 at 05:08
  • BTW: `432758575375735.24` is not a `float`, but a _floating constant_ of type `double`. – chux - Reinstate Monica Nov 29 '17 at 05:50

2 Answers2

2

This is undefined behavior. The compiler warns you and then the printf tries to process sizeof(long int) bytes where you have provided it with an integer literal which is of size sizeof(int) bytes.

It expects sizeof(long int) bytes and now if sizeof(int)==sizeof(long) It's alright else it's Undefined Behavior.

Don't think that format specifier are similar to variables. long variable can hold values of an int. This doesn't go with the format specifiers.

A casting would solve the problem

printf("%ld \n",(long)3809);

From standard §7.21.6.1 (Formatted input/output functions)

[7] If a conversion specification is invalid, the behavior is undefined. If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined.

user2736738
  • 30,591
  • 5
  • 42
  • 56
1

Integer constants have type int by default. The "%ld" format specifier expects a long int. Using the wrong format specifier invokes undefined behavior.

You need to add the L suffix for an integer literal to have type long:

printf("%ld \n",3809L);
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
dbush
  • 205,898
  • 23
  • 218
  • 273
  • "You need to add the L suffix for an integer literal to have type long" is almost on the money. `1234567890123` would be a `long` on a 32-bit `int`/64-bit `long` even without an `L`. Certainly an `L` is needed for constants in the `int` range. – chux - Reinstate Monica Nov 29 '17 at 05:41