| View previous topic :: View next topic | 
	
	
		| Author | Message | 
	
		| E_Blue 
 
 
 Joined: 13 Apr 2011
 Posts: 417
 
 
 
			    
 
 | 
			
				| Printf not working as expected [Solved] |  
				|  Posted: Wed Sep 23, 2020 9:06 am |   |  
				| 
 |  
				| I'm trying to print in a string but I see no changes after printf What I'm doing wrong?
 
 
 
 
  	  | Code: |  	  | char IndexBufferCmdMain;
 #define BufferCmdMainSize 15
 char BufferCmdMain[BufferCmdMainSize][12];
 
 #define AlarmRxBufferSize 16
 char AlarmRxBuffer[AlarmRxBufferSize];//128bits
 unsigned int16 EventoCid;
 
 #define Part 0x09
 #define User 0x0A
 
 printf(BufferCmdMain[IndexBufferCmdMain][0],"%4LX A%1X A%2X\r\n",EventoCid,AlarmRxBuffer[Part],AlarmRxBuffer[User]);
 
 | 
 _________________
 Electric Blue
 
 Last edited by E_Blue on Wed Sep 23, 2020 1:28 pm; edited 1 time in total
 |  | 
	
		|  | 
	
		| PCM programmer 
 
 
 Joined: 06 Sep 2003
 Posts: 21708
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Wed Sep 23, 2020 9:15 am |   |  
				| 
 |  
				| If you want to print to a buffer,  you should use sprintf, not printf. |  | 
	
		|  | 
	
		| temtronic 
 
 
 Joined: 01 Jul 2010
 Posts: 9589
 Location: Greensville,Ontario
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Wed Sep 23, 2020 9:24 am |   |  
				| 
 |  
				| You need to post a real small program that comments what is expected and shows us what IS printed. 
 If you're trying to fill 'BufferCmdMain[IndexBufferCmdMain][0]' with 'EventoCid,AlarmRxBuffer[Part],AlarmRxBuffer[User])' data then you need to use the SPRINTF() function.
 I'm thinking that's what you want to do. Look for Sprintf() in the manual, there is a small example listed.
 
 Jay
 |  | 
	
		|  | 
	
		| E_Blue 
 
 
 Joined: 13 Apr 2011
 Posts: 417
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Wed Sep 23, 2020 9:25 am |   |  
				| 
 |  
				| Changed 
  	  | Code: |  	  | printf(BufferCmdMain[IndexBufferCmdMain][0],"%4LX A%1X A%2X\r\n",EventoCid,AlarmRxBuffer[Part],AlarmRxBuffer[User]); | 
 
 to
 
  	  | Code: |  	  | sprintf(BufferCmdMain[IndexBufferCmdMain][0],"%4LX A%1X A%2X\r\n",EventoCid,AlarmRxBuffer[Part],AlarmRxBuffer[User]); | 
 
 doesn't work
 
 Changed to
 
 
  	  | Code: |  	  | sprintf(BufferCmdMain[IndexBufferCmdMain],"%4LX A%1X A%2X\r\n",EventoCid,AlarmRxBuffer[Part],AlarmRxBuffer[User]); 
 | 
 and worked ok.
 _________________
 Electric Blue
 |  | 
	
		|  | 
	
		| Ttelmah 
 
 
 Joined: 11 Mar 2010
 Posts: 19967
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Wed Sep 23, 2020 11:34 am |   |  
				| 
 |  
				| sprintf, needs to be given the address where it is to put the data. 
 BufferCmdMain[IndexBufferCmdMain][0]
 
 Is not an address.
 
 to put the data into BufferCmdMain, you would just use
 BufferCmdMain. If you want to put it to an offset location in this
 buffer, then you need to use &, and the index, so:
 
 &BufferCmdMain[IndexBufferCmdMain][0]
 |  | 
	
		|  | 
	
		|  |