-2

Can someone tell me what's wrong with this:

int main()
{
char a[100];
int i = 0;
printf("Enter a number: ");
scanf("%c", &a[i]);

printf("The number you've entered is: %d", a[i] - '0');
} 

Brief summary: I am trying to convert a number stored in a char array to its int equivalent. I know in C#, you use the intparse command, but because there isn't such one in C, I do not know what to do. When I input a two digit number, it is only outputting the first digit, of the input char.

l0b0
  • 55,365
  • 30
  • 138
  • 223
Anonymous Person
  • 1,437
  • 8
  • 26
  • 47

5 Answers5

3

strtol sample

#include <stdio.h>
#include <stdlib.h>

int main(){
    char str[16], *endp;
    int value;
    printf("Enter a number: ");
    fgets(str, sizeof(str), stdin);
    value = strtol(str, &endp, 0);
    if(*endp == '\n' || *endp == '\0'){
        printf("The number you've entered is: %d\n", value);
    } else {
        printf("invalid number format!");
    }
    return 0;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
  • Thank you for this code snippet, which might provide some limited short-term help. A proper explanation [would greatly improve](//meta.stackexchange.com/q/114762) its long-term value by showing *why* this is a good solution to the problem, and would make it more useful to future readers with other, similar questions. Please [edit] your answer to add some explanation, including the assumptions you've made. – Toby Speight May 16 '18 at 16:54
1

If you mean to print ASCII value of char the no need to do a[i] - '0'. Try this

 printf("The number you've entered is: %d", a[i]); 

If you are talking about string then first change your scanf statement to

scanf("%s", a);  

or better to use fgets library function instead of scanf;

fgets(a, sizeof(a), stdin);

and then use strtol function.

haccks
  • 104,019
  • 25
  • 176
  • 264
  • a multiple of 10 gives me 49, basically. this includes 1.. not sure whats going on – Anonymous Person Dec 25 '13 at 17:26
  • 1
    Lol (to downvoter)..... `%c` reads only a single character at a time not two (`10`) – haccks Dec 25 '13 at 17:27
  • %3d isn't of much help either – Anonymous Person Dec 25 '13 at 17:31
  • No. Its is out of context here. – haccks Dec 25 '13 at 17:32
  • 2
    scanf("%c", a): gives a garbage value – Anonymous Person Dec 25 '13 at 17:33
  • @AnonymousPerson; Oops! That was Typo. Its `scanf("%s", a); `. See the edit. – haccks Dec 25 '13 at 17:35
  • scanf("%c",a); Here u r trying to print the character format of address of array a, and hence address is generally a large value so for this big value no corresponding character and hence some unpredictable pattern will display on your screen. – asifaftab87 Dec 25 '13 at 17:42
  • Let c is a character then to print its value in integer format then just use ("%d",c); No need of doing any thing. – asifaftab87 Dec 25 '13 at 17:43
  • @asifaftab87: didn't work.. can you check if there is any other modification that needs to be done with the coding – Anonymous Person Dec 25 '13 at 17:47
  • 1
    I already said that that which character value you want to print in its int format then store it in a character variable then print it just by using %d format. Second thing that I think so that you want to print int format of '10' then it is wrong because '10' is not a character it will take may be first as char and left the remaining portion. So even if you want to print '10' int ascii then you to write some codes means you to write your own code to implement this and it is possible – asifaftab87 Dec 25 '13 at 17:52
  • 1
    ITT: Code posted in OP has nothing to do with question OP asks. –  Dec 25 '13 at 17:54
0

OP method of scanf("%c", &a[i]); only handles a 1 character input.

To read the entire line, suggest using fgets() to read.
The convert to a long using strtol().
Watch for potential errors.

#include <stdio.h>
#include <stdlib.h>

int main(){
  // use long to match strtol()
  long i;
  // size buffer to handle any legitimate input 
  char a[sizeof i * 3 + 3];
  char *endptr;
  printf("Enter a number: ");
  if (fgets(a, sizeof a, stdin) == NULL) {
    // If you would like to handle uncommon events ...
    puts("EOForIOError");
    return 1;
  }
  errno = 0;
  i = strtol(str, &endptr, 10);
  if(*endptr != '\n' /* text after number */ || 
      endp == a      /* no text */           || 
      errno          /* overflow */) {
    printf("Invalid number %s", a);
  }
  else {
    printf("The number you've entered is: %ld\n", i);
  }
  return 0;
}  
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
-1

Try

char myarray[5] = {'-', '4', '9', '3', '\0'};
int i;
sscanf(myarray, "%d", &i); // i will be -493

Edit (borrowed from [1]):

There is a wonderful question here on the site that explains and compares 3 different ways to do it - atoi, sscanf and strtol. Also, there is a nice more-detailed insight into sscanf (actually, the whole family of *scanf functions).

Community
  • 1
  • 1
herohuyongtao
  • 49,413
  • 29
  • 133
  • 174
-1
#include<stdio.h>
#include<stdlib.h>
int main()
{

char a[100];

int final=0,integer;

int i;

printf("Enter a number: ");

gets(a);

for(i=0;a[i]>='0'&&a[i]<='9';++i)

{

integer=a[i] - '0';

final=final*10+integer;

} 

    enter code here

printf("The integer is %d ", final);

}
  • 1
    It's best if you also explain your code, it makes it a lot easier to understand an answer rather than blindly copying it. – SuperBiasedMan May 18 '15 at 11:44
  • Thank you for this code snippet, which might provide some limited short-term help. A proper explanation [would greatly improve](//meta.stackexchange.com/q/114762) its long-term value by showing *why* this is a good solution to the problem, and would make it more useful to future readers with other, similar questions. Please [edit] your answer to add some explanation, including the assumptions you've made. – Toby Speight May 16 '18 at 16:53