0

When I run that, after the first iteration everthings looks ok, the for loop print "write char" twice and after wait for the input. From what I understand the values are not really assigned(not even in the first iteration). Some help?

#include <stdio.h>
int main(){
    int i;
    char a[100];
    for(i=1;i<=5;i++){
        printf("print char\n");
        scanf("%c",&a[i]);
    } 
} //this is my code 
pavi2410
  • 1,040
  • 1
  • 10
  • 16
  • 2
    With what exactly do you need help with? `print "write char" twice` vs `printf("print char\n");` - your code prints "print char" not "write char". Assuming `scanf` calls succeeded, the second element of `a` array `a[1]` is assigned to. – KamilCuk Jan 12 '20 at 14:58
  • 1
    If you type `a` and return, you've entered two characters, so `scanf()` can read two characters in two iterations. You've not printed any of the data; you've no idea whether it worked, but it probably did. You might be better off using `" %c"` in the format string; the leading blank skips white space. You should check that `scanf()` returns `1` each time (exiting the loop if it doesn't). – Jonathan Leffler Jan 12 '20 at 15:34

2 Answers2

2

Your scanf function is catching a carriage return (see Jonathan Leffler comment), using getchar() can solve this, like so:

#include<stdio.h> 

int main(){ 
    int i; char a[100]; 
    for(i = 0; i < 5; i++){    
        printf("print char\n");  
        scanf("%c",&a[i]);   
        getchar(); 
    } 
    a[i] = '\0';
    for(int i = 0; i < 5; i++){
        printf("%c", a[i]);
    }
} 

Also the loop for(i = 1; i <= 5; i++) doesn't make much sense since you will leave the first char in your array empty, if you need to get 5 characters you should use for(int i = 0; i < 5; i++), and for good measure, terminate it with '\0'.

anastaciu
  • 20,013
  • 7
  • 23
  • 43
  • 2
    Maybe take a look at this: [Using fflush(stdin)](https://stackoverflow.com/q/2979209/10871073) - possible (probable?) undefined behaviour. – Adrian Mole Jan 12 '20 at 15:05
  • 1
    Waddya mean about "from the `printf` string before"? The standard input and standard output are normally wholly unrelated. – Jonathan Leffler Jan 12 '20 at 15:34
  • 1
    It catches the newline (carriage return) from typing `a` (for example) and return — two key strokes, two characters in the input buffer. Please remove the `printf` bit, at minimum. – Jonathan Leffler Jan 12 '20 at 15:39
  • @JonathanLeffler A Carriage return is something different than a newline. https://stackoverflow.com/questions/1761051/difference-between-n-and-r – RobertS supports Monica Cellio Jan 12 '20 at 16:14
  • @RobertS-ReinstateMonica — yeah — the terminal probably mapped the carriage return that was typed into a newline, but I didn't want to get into the inner details of `stty icrnl` and `struct terminfo` in a 600-character comment. – Jonathan Leffler Jan 12 '20 at 16:15
1

You can use:

#include <stdio.h>
int main(){
    char a[5];
    for(int i=0;i<=5;i++){
        printf("\nPlease input one character for the %d.Element: ",i+1);
        scanf("%c %*c",&a[i]);
    } 
}

where %*c discards the newline character \n which is left in stdin after the first character consumption.