Print and use array element in C -


so i'm trying input array , using elements. eg:

#include <stdio.h> #include <string.h>   int main() { int s[100],i;  for(i=0; < strlen(s); ++i) {scanf("%d",&s[i]);}  printf("s[1] = %d",s[1];  } 

if input 12345, want return s[1], 2. know how print whole array, want 1 or more elements, , seems can't figure seemingly easy problem.

for(i=0; < strlen(s); ++i) 

using uninitialised (indeterminate) variable can have surprising results. anyway, want element count here: sizeof s/sizeof *s.

{scanf("%d",&s[i]);} 

always test how many elements assigned in scanf. second condition abort btw.
also, if want convert 1 digit, use field length: "%1d"

if input 12345, want return s[1], 2. know how print whole array, want 1 or more elements, , seems can't figure seemingly easy problem.

that looks wanted read string of digits, , not numbers. use this:

char digits[100]; if(scanf("%99[0-9]", digits) != 1)     abort(); 

above can read string of 99 digits, aborting if no digits found.


Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -