delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2017/05/24/08:01:09

X-Recipient: archive-cygwin AT delorie DOT com
DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id
:list-unsubscribe:list-subscribe:list-archive:list-post
:list-help:sender:from:to:subject:date:message-id:content-type
:content-transfer-encoding:mime-version; q=dns; s=default; b=Gjv
nvJ003aMIDJq5KUbhbsStVyvwO0vg+UKTwqI1KTlLYwH9drGZ5jdcSE3hjvQLYJV
qOBCC/QJ9wN349HRCDTzxN5DcB8IhOAiKP72/4tdsoUHcJ1A9bpEXMn0aDUpILWc
Gx7SRDOAe9MsYmmO319BauABwJICfDVdU2SeNblU=
DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id
:list-unsubscribe:list-subscribe:list-archive:list-post
:list-help:sender:from:to:subject:date:message-id:content-type
:content-transfer-encoding:mime-version; s=default; bh=68rBRlF9w
IihRW6HaIhXnizLQ9o=; b=cbRI1BGlYb/UtlX8TvCHNaF2NP0qG08Vtn7pZrnEt
OMW4+5gmvWFnF8S95kDJM3GPbz+TYU64yS0vjBo+OGj1iH8YPyCrmAi3xDZvNY+t
YA3zWhSwdWHZd15VJnaH0scqW3+dxzhbr45LjEPoQuUukpUxK69dKclUHN23k/Sm
gM=
Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Id: <cygwin.cygwin.com>
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com
Authentication-Results: sourceware.org; auth=none
X-Virus-Found: No
X-Spam-SWARE-Status: No, score=3.1 required=5.0 tests=AWL,BAYES_40,KAM_LAZY_DOMAIN_SECURITY,LIKELY_SPAM_SUBJECT,RCVD_IN_DNSWL_NONE,RP_MATCHES_RCVD autolearn=no version=3.3.2 spammy=H*F:D*no, addressee, Via, H*M:ngi
X-HELO: mailgw1.ngi.no
From: Carl Fredrik Forsberg <Carl DOT Fredrik DOT Forsberg AT ngi DOT no>
To: "cygwin AT cygwin DOT com" <cygwin AT cygwin DOT com>
Subject: FW: Printing long int in C program under cygwin64
Date: Wed, 24 May 2017 12:00:16 +0000
Message-ID: <6f28f46906804c6f8f6b4b861e202492@CASMBX02.oslo.ngi.no>
x-ms-exchange-transport-fromentityheader: Hosted
MIME-Version: 1.0
X-MIME-Autoconverted: from quoted-printable to 8bit by delorie.com id v4OC17OU013288

I am experiencing problems printing long int values under cygwin64 installed on a Windows 10 machine.

Below is a test program followed by its output to demonstrate the problem. The program was initially written to demonstrate the output from lrint(), and developed further to demonstrate to myself how negative integers are tackled by printf type specifiers (e.g. %li, %ld etc).

My understanding is that lrint() should return a long int. However I am unable to get printf to print the correct number. Instead its output is treated as an unsigned integer.
Any  help or hints would be much appreciated.

Regards
Carl Fredrik

#include <stdio.h>      /* printf */
#include <math.h>       /* lrint */

int main ()
{
  char text[64];
  printf ( "int -2 = %i\n", -2 );
  printf ( "int -1 = %i\n", -1 );
  printf ( "int 0 = %i\n", 0 );
  printf ( "int 1 = %i\n", 1 );
  printf ( "long int -2 = %li\n", -2 );
  printf ( "long int -1 = %li\n", -1 );
  printf ( "type cast -1 = %li\n", (long int)-1 );
  printf ( "type cast lrint(-1.0) = %li\n", (long int)lrint(-1.0) );
  printf ( "lrint(-1.0) = %li\n", lrint(-1.0) );
  printf ( "lrint(1.0) = %li\n", lrint(1.0) );
  printf ( "long int 0 = %li\n", 0 );
  printf ( "long int 1 = %li\n", 1 );
  sprintf( text,"long int -1 = %li", -1 );
  printf ( "Via sprintf: %s\n", text);
  printf ( "size of long int: %i\n", sizeof(long int));
  printf ( "size of int: %i\n", sizeof(int));
  return 0;
}


compiled by:
gcc lrint_test.c -o lrint_test.exe

Output:

int -2 = -2
int -1 = -1
int 0 = 0
int 1 = 1
long int -2 = 4294967294
long int -1 = 4294967295
type cast -1 = -1
type cast lrint(-1.0) = 4294967295
lrint(-1.0) = 4294967295
lrint(1.0) = 1
long int 0 = 0
long int 1 = 1
Via sprintf: long int -1 = 4294967295
size of long int: 8
size of int: 4



gcc version:
gcc (GCC) 5.4.0
Copyright (C) 2015 Free Software Foundation, Inc.









The confidentiality or integrity of this message can not be guaranteed following transmission on the Internet. The addressee should be aware of this before using the contents of this message.

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple


- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019