How to take one character at a time as input from a string of characters without space in c? -


suppose,"5181 2710 9900 0012"- string of digits.i need take 1 single digit @ time input string of number without space make arithmatic operations . so, write that,

int a[20]; for(int i=0;i<16;i++) { scanf("%d",&a[i]);   } 

but didn't give me expected result. when use "%1d"instead of "%d",it gave me expected result. so, how works?

since scanf inverse of printf, verify printing number modifier (just little tip).*

in general, number before format 'width' modifier. in case means you're reading 1 byte number. if specify %d, may number of arbitrary length.

example:

#include <stdio.h>  int main() {     int a;     sscanf("1234", "%d", &a);     printf("%d\n", a); // prints 1234     sscanf("1234", "%1d", &a);      printf("%d\n"m a); // prints 1 } 


*) appears false particular case. makes sense numbers not truncated when specifiying %d format, though, since change meaning of number. however, many cases try printf predict scanf's behavior. of course, reading manual or docs on more helpful approach :)


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -