Here the str array used in the main function and str used in input function are not same variables. They have different scopes and life times.
Then by using printf() function, why I am able to print the str array?
Here , There is no return type of the string from the function input.
There is only return type of number of characters which is stored in variable n.
Why I am able to print the str array, whose scope and life time are already over?
Here
the input : abcdefghijklmnopqrstuvwxyz
the output : 5abcde
What I have tried:
#include<stdio.h>
int input(char str[],int n)
{
int ch,i=0;
while((ch=getchar())!='\n')
if(i<n)
str[i++]=ch;
str[i]='\0';
return i;
}
int main()
{
char str[100];
int n=input(str, 5);
printf("%d %s",n,str);
return 0;
}