CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Mystical long long datatype behaviour

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
DeniTCH



Joined: 09 Aug 2011
Posts: 6

View user's profile Send private message

Mystical long long datatype behaviour
PostPosted: Tue Aug 09, 2011 5:55 am     Reply with quote

Hi, I am currently experiencing quite strange behavior when using variables of the long long type.

My compiler version is: 4.104, I am using PIC18F6680 with the following configuration:

Code:

//------------------------------------------------------------------------------
// Configuration bits
//------------------------------------------------------------------------------
#pragma fuses HS         // Use external crystal as clock source
#pragma fuses NOWDT         // Disable watchdog timer
#pragma fuses NOPROTECT      // Don't read/write protect program memory
//------------------------------------------------------------------------------
// Compiler magic
//------------------------------------------------------------------------------
#pragma use rs232(BAUD=19200, BITS=8, STOP=1, PARITY=N, XMIT=PIN_TX, RCV=PIN_RX)
#pragma use spi(BITS=8)


The problem is summarized in the following code:

Code:

#include "system.h"
typedef unsigned long long DPULONG;

DPULONG testfunc()
{
   DPULONG ret = 0x69736544;
   printf("\r\nret=0x%LX",ret);
   return ret;
}

void main() {
   DPULONG i,j;
   unsigned long long cnt;
   init();
   setup_spi(SPI_MASTER | SPI_H_TO_L | SPI_XMIT_L_TO_H | SPI_CLK_DIV_4 );

   printf("%c[2J",0x1B);
   printf("DTUsat-PPL Test\r\n");
   i = testfunc();
   j = 0 | testfunc();
   printf("\r\ni=0x%LX,j=0x%LX,direct=0x%LX",i,j,testfunc());
   while(1);
}


The terminal output of this code is:
DTUsat-PPL Test

ret=0x69736544
ret=0x69736544
ret=0x69736544
i=0x34343434,j=0x69736544,direct=0x69730000

I am expecting i,j and the direct function result to print the same number, however it does not. Is there anything I am misunderstanding regarding the long long datatype? I am looking forward to any responses to this issue! Smile

Best regards
Denis
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Aug 09, 2011 8:19 pm     Reply with quote

You didn't post a compilable test program, so I added or commented out
whatever was required to make it compile. I installed vs. 4.104 and ran
it in MPLAB simulator. It works. This is the result in the Output window:
Quote:

ret=0x69736544
ret=0x69736544
ret=0x69736544
i=0x69736544,j=0x69736544,direct=0x69736544


Here is part of the build results and .LST file:
Code:

Executing: "C:\Program Files\PICC\Ccsc.exe" "PCH_Test.c" +FH +DF +LY -T -A +M -Z +Y=9 +EA -EW
      Memory usage:   ROM=1%      RAM=1% - 1%
      0 Errors,  0 Warnings.
Loaded C:\Program Files\PICC\Projects\PCH_Test\PCH_Test.cof.
BUILD SUCCEEDED: Tue Aug 09 19:16:23 2011



CCS PCH C Compiler, Version 4.104, xxxxx     09-Aug-11 19:16

               Filename: C:\Program Files\PICC\Projects\PCH_Test\PCH_Test.lst

               ROM used: 600 bytes (1%)
                         Largest free fragment is 64936
               RAM used: 20 (1%) at main() level
                         23 (1%) worst case
               Stack:    2 locations


This is the test program:
Code:

#include <18F6680.h>

//------------------------------------------------------------------------------
// Configuration bits
//------------------------------------------------------------------------------
#pragma fuses HS         // Use external crystal as clock source
#pragma fuses NOWDT         // Disable watchdog timer
#pragma fuses NOPROTECT      // Don't read/write protect program memory

#use delay(clock=20M)

//------------------------------------------------------------------------------
// Compiler magic
//------------------------------------------------------------------------------
#pragma use rs232(BAUD=19200, BITS=8, STOP=1, PARITY=N, XMIT=PIN_C6, RCV=PIN_C7)
#pragma use spi(BITS=8)


//#include "system.h"
typedef unsigned long long DPULONG;

DPULONG testfunc()
{
   DPULONG ret = 0x69736544;
   printf("\r\nret=0x%LX",ret);
   return ret;
}

void main() {
   DPULONG i,j;
   unsigned long long cnt;
//   init();
   setup_spi(SPI_MASTER | SPI_H_TO_L | SPI_XMIT_L_TO_H | SPI_CLK_DIV_4 );

   printf("%c[2J",0x1B);
   printf("DTUsat-PPL Test\r\n");
   i = testfunc();
   j = 0 | testfunc();
   printf("\r\ni=0x%LX,j=0x%LX,direct=0x%LX",i,j,testfunc());
   while(1);
}
DeniTCH



Joined: 09 Aug 2011
Posts: 6

View user's profile Send private message

PostPosted: Wed Aug 24, 2011 6:57 am     Reply with quote

I have found the error, which of course was on my side. Embarassed The length of the "long" datatype was redefined higher in the build system, which we are using. As it was not written by me I had no idea. I am sorry for the inconvenience.
Ttelmah



Joined: 11 Mar 2010
Posts: 19350

View user's profile Send private message

PostPosted: Wed Aug 24, 2011 9:19 am     Reply with quote

This is one reason I prefer to be 'explicit' with data type sizes. The forms, int8, int16, and int32, are self documenting, and avoid using constructs like 'long long', which may have different meanings in other compilers, and can give problems if re-defined.....
Glad you have found it.

Best Wishes
DeniTCH



Joined: 09 Aug 2011
Posts: 6

View user's profile Send private message

PostPosted: Fri Aug 26, 2011 1:26 am     Reply with quote

I have also decided to use the 'explicit' datatypes now, for exactly that reason. Trust no one, except yourself Wink
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group