Thread: atoi & scanf

  1. #1
    Registered User
    Join Date
    Feb 2018
    Location
    San Diego, CA
    Posts
    123

    atoi & scanf

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main(void)
    {
            char input[100];
            int z;
    
    
            printf("Insert string: ");
            scanf("%s", input);
    
    
            z = atoi(input);
    
    
            printf("%d", z);
    
    
            return 0;
    }
    Please, z is returning zero. Would love an explanation with a fix.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Feb 2018
    Location
    San Diego, CA
    Posts
    123
    Quote Originally Posted by stahta01 View Post
    T

    This actually isn't what I want. I wanted to write a program to convert CHAR into ASCII code.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    I suggest you post the input with the output you want because what you stated is not clear to me.
    To me char is already ASCII!

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Perhaps you want to print the value of each character?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(void)
    {
        char input[100];
    
        printf("Insert string: ");
        scanf("%99s", input);
    
        for(size_t i = 0; i < strlen(input); ++i)
            printf("%c = 0x%X = %d\n", input[i], (int)input[i], (int)input[i]);
    
        return 0;
    }

  6. #6
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    338

    Post

    convert CHAR into ASCII
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main( int argc, char *argv[] ) {
        
        char input[100];
    
        printf( "String: " ); scanf( "%s", input );
    
        for (int i=0; i<strlen(input); i++) {
    
            int z = (int)(input[i]);
    
            printf( "%i,", z );
    
        }
     
        printf( "\n" );   
        return 0;
    }
    C - Type Casting - Tutorialspoint
    Last edited by Structure; 06-14-2021 at 05:12 AM.
    "without goto we would be wtf'd"

  7. #7
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    338

    Post

    atoi & scanf
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main( int argc, char *argv[] ) {
        
      char number[128];
    
      printf( "Enter Number: "); 
      scanf( "%s", number);
      
      int z = atoi( number );
      printf( "%i \n", z );
    
      return 0;
    }
    "without goto we would be wtf'd"

  8. #8
    Registered User
    Join Date
    Feb 2018
    Location
    San Diego, CA
    Posts
    123
    Looks good, thanks to all those that helped! Great to be back on the forum!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. use of atoi.
    By mgracecar in forum C Programming
    Replies: 3
    Last Post: 03-06-2012, 09:02 PM
  2. C++.NET Can I use atoi ??
    By Swaine777 in forum C++ Programming
    Replies: 3
    Last Post: 09-18-2004, 06:54 PM
  3. Difference between using atoi and scanf
    By James00 in forum C Programming
    Replies: 4
    Last Post: 04-12-2003, 09:59 AM
  4. atoi()
    By Unregistered in forum C Programming
    Replies: 17
    Last Post: 07-13-2002, 02:09 AM
  5. atoi or scanf()
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 10-19-2001, 05:18 AM

Tags for this Thread