Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Hi! IAm a newbie to 'C' can anybody tell me how does the "scanf" function work?

include<stdio.h>

int main()

{

char str(100);

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

int i;

printf("Please enter your name")

scanf("%s",str);

printf("your name");

return 0 ;

}

This is the Code I have written,
but the Problem is The code runs but never gives the Output …
Until I manually stop the code.
how does the scanf("") function work?

>Solution :

Scanf reads formatted input from the standart input. There is nothing wrong with scanf itself. Your way of using it is also correct but there are two problems with your code:

The first is that arrays must be defined with []. Therefore,

char str(100);

should be

char str[100];

The second problem is that you are recieving the input but you are never printing it. To do so you can write the following code:

printf("%s", str);

The final code being:

#include<stdio.h>
int main()
{
    char str[100];
    int i;

    printf("Please enter your name");
    scanf("%s",str);
    printf("your name is: %s", str);

    return 0 ;
}

Also, don’t forget the semi-colons.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading