Thread: sprintf()

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    20

    sprintf()

    Code:
    char str[32];
    int x=256;
    sprintf(str, "%d", x);
    gives: str=0
    is there any way around this?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I can't see anything wrong in the posted code. Can you post a COMPLETE working example of the code (that is, something that can be compiled locally and run on for example my machine). Generally, when this sort of thing happens, the code that has been removed to make the example small is actually part of the problem...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    20
    sorry, I was using a child process to pass the value of x back into a coordinator process and it seems that exit() can not handle integers above 256..... i think?
    Code:
    pid=fork();
    		if(pid==-1){
    			printf("Fork Error\n");
    		}
    		if(pid==0){
    			execlp("./worker.o", "./worker.o", x[0], x[1], NULL);
    		}
    		else{
    			child_pid=wait(&message);
    	
    			if(WIFEXITED(message)){
    				messageholder=WEXITSTATUS(message);
    				ans[ANSindex]=strdup(itoa(messageholder));
    			}
    			else						
    				printf("Error in subthread\n");
    			
    		}
    I wont need to be handling anything above single digit integers so there are no worries. Solved my own problem, thx Mats
    Last edited by BMathis; 02-16-2009 at 06:16 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sprintf overflows my buffer -- why?
    By Lasston in forum C Programming
    Replies: 26
    Last Post: 06-20-2008, 04:33 PM
  2. sprintf : garbage appended
    By yeller in forum C Programming
    Replies: 9
    Last Post: 12-17-2007, 10:21 AM
  3. sprintf in C and C++
    By usu_vlsi in forum C++ Programming
    Replies: 2
    Last Post: 03-14-2005, 04:14 AM
  4. sprintf and sscanf
    By tommy69 in forum C Programming
    Replies: 10
    Last Post: 04-22-2004, 08:00 PM
  5. Sprintf
    By Trauts in forum C++ Programming
    Replies: 10
    Last Post: 01-15-2003, 01:35 PM