Thread: Confusing printf/atoi statements

  1. #1
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243

    Confusing printf/atoi statements

    PROBLEM CLOSED: i copied and pasted (that is, exact same) the code from windows and used SSh to connect to a solaris server (at school), compiled, executed, and it gave the correct output. im just thankful i dont have to pay for this junk of an OS Windows XP (license from school--not pirated).

    first, this isnt homework, im working on a problem from a math/programming site (projecteuler.net if i may post it). the code is very basic, but ive cut it down quite a bit. from what i have so far, it will seem pointless and probably confusing, but here i go. the limits of the for loops will seem confusing, please try and ignore them.

    the inner for loop takes characters from the string at position k and converts it to an integer. it does this 5 times, multiplying the new digit each time (ie 1*digit1*digit2*...)

    i have two print statements in the inner for loop to verify im reading the correct character at position k, and it is properly converted to an int. if you run exactly what i have (at least for me), it works for the first few iterations.. then on the last iteration it reads in a '6' and converts it to integer '61'. and i have absolutely no clue why. ive looked it over and over, and i dont understand why it works for seemingly every case but this exact 6!

    following the code is output from running the program.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    int max(int a, int b)
    {
    	if (a >= b)
    		return a;
    	else
    		return b;
    }
    
    int main()
    {	
    	char num[100] = {"73167176531330624919225119"}; // no, it isnt 100 characters but it shouldnt matter
    
    	int curMax = 0;
    	int i, k;
    
    	for (i = 0; i < 5; i++)
    	{
    		int temp = 1;
    
    		for (k = 0 ; k < 5; k++)
    		{
    			char x = num[i+k];
    			printf("x = '%c'\n", x);
    			int digit = 0;
    			digit = atoi(&x);
    						
    			printf("digit= '%d'\n", digit);
    			temp *= digit;
    		}
    		printf("\n");
    		curMax = max(curMax, temp);
    	}
    	
    	printf("%d\n", curMax);
    }
    NOTE: x = 'i' means the character value, digit='i' means the integer value.
    Code:
    x = '7'
    digit= '7'
    x = '3'
    digit= '3'
    x = '1'
    digit= '1'
    x = '6'
    digit= '6'
    x = '7'
    digit= '7'
    
    x = '3'
    digit= '3'
    x = '1'
    digit= '1'
    x = '6'
    digit= '6'
    x = '7'
    digit= '7'
    x = '1'
    digit= '1'
    
    x = '1'
    digit= '1'
    x = '6'
    digit= '6'
    x = '7'
    digit= '7'
    x = '1'
    digit= '1'
    x = '7'
    digit= '7'
    
    x = '6'
    digit= '6'
    x = '7'
    digit= '7'
    x = '1'
    digit= '1'
    x = '7'
    digit= '7'
    x = '6'
    digit= '6'
    
    x = '7'
    digit= '7'
    x = '1'
    digit= '1'
    x = '7'
    digit= '7'
    x = '6'
    digit= '61'    ************* WHY IS THIS 6 == 61! 
    x = '5'
    digit= '5'
    
    14945
    im sure its hard to look past the seemingly confusing logic and pointless algorithm, but thanks for any input.

    EDIT: it seems that it works when i change the number before the number in question (the 7 before the 6) to something else! this cant be a solution, because i cant alter the number (its a 1000-digit number). also, initially i copied and pasted the number, but same problem after actually typing in the first, say 20, digits. i thought that could be a problem with having non-printable characters, but its a plain text file anyways so theyre arent any.
    Last edited by nadroj; 04-05-2008 at 10:50 PM.

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    The fact that the code does what you expect on Solaris doesn't necessarily mean it's right (it's not). If two compilers give different output, suspect your code, not the compiler!

    You're calling atoi() incorrectly. atoi() expects a string to be passed (really a pointer to a string, if you want to be pedantic). You're not passing a string. Instead, you're passing the address of a single character, which is not going to be a string (except in one corner case that doesn't matter here).

    If you're trying to convert a character to an integer--say, '5' to 5--which it appears you are, there's a simple method to doing it: subtract '0' from the digit:
    Code:
    #include <stdio.h>
    int main(void)
    {
      char x = '5';
      printf("%c: %d\n", x, x - '0');
      return 0;
    }
    atoi() is for converting a string to a number, not just a single digit; although it would work for a single digit if you turned that single digit into a string:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main(void)
    {
      char x = '5';
      char string[2];
      string[0] = x; string[1] = 0;
      printf("%c: %d\n", x, atoi(string));
      return 0;
    }

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    thanks alot, cas, makes a lot of sense.

    i didnt suspect the code was wrong, because it worked for every case except..the one....that didnt work. i remember the simple trick about converting a char to an int, now that you bring it up, though. thanks again

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > digit = atoi(&x);
    Because this expects a \0 terminated string and all you're running on at the moment is luck.

    Try something like
    Code:
    char x[2] = { 0, 0 };
    x[0] = num[i+k];
    digit = atoi(x);
    Or even the simple
    digit = num[i+k] - '0';

    Edit: beaten to it
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  2. newbie question - if statements without conditions
    By c_h in forum C++ Programming
    Replies: 2
    Last Post: 07-18-2008, 10:42 AM
  3. Explanation of switch statements
    By ammochck21 in forum C++ Programming
    Replies: 6
    Last Post: 11-04-2006, 02:59 PM
  4. Efficiency of case statements
    By Yasir_Malik in forum C Programming
    Replies: 26
    Last Post: 05-23-2006, 11:36 AM
  5. Replies: 3
    Last Post: 11-08-2005, 07:25 AM