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

Is there some problem with the following c code

When i compile and run this program my input string is not same as output.

#include<stdio.h>
int main()
{
    int n; char ch[100];
    scanf("%d : %5s", &n, ch);
    printf("%d : %s", n, ch); // there is some problem with output of the string
    return 0;
}

input:

45
asdf

output:

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

45 : ∟sëuⁿ■a

>Solution :

The scanf part will read until it hits the colon, but it will not read the colon itself, which means the following integer will not be read correctly and the rest of the string will be parsed incorrectly (if at all).

Try removing the colon (:) from the scanf

#include <stdio.h>
int main()
{
    int n; char ch[100];
    scanf("%d %5s", &n, ch);
    printf("%d : %s", n, ch);
    return 0;
}
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