technextgen
technextgen

Reputation: 87

Why is the C program giving weird output using Scanf?

I'm currently learning C programming and I have come across this weird output.

// Program will try functionalities of the scanf function
#include<stdio.h>
#include<stdlib.h>

int main(int argc, char const *argv[])
{
    char array[40];
    scanf("Enter your address: %39s",array);   //39 + 1/o null character at the end of the character array.
    printf("The address is : %s", array);
    return 0;
}

It should accept input address and output the same.

PS C:\Users\G\Documents\C programs> gcc scanff.c -o scanff -Wall -pedantic -std=c99
PS C:\Users\G\Documents\C programs> ./scanff
jhgjhg
The address is : ■   a

What is that black block? Could someone explain! Thanks.

Upvotes: 3

Views: 649

Answers (4)

Krishna Kanth Yenumula
Krishna Kanth Yenumula

Reputation: 2567

From man page of scanf:

int scanf(const char *format, ...);

Reason for undefined output:

Each conversion specification in format begins with either the character '%' or the character sequence "%n$"

Solution : Change scanf("Enter your address: %39s",array); to scanf("%39s",array);

Upvotes: 1

Hans-Martin Mosner
Hans-Martin Mosner

Reputation: 846

You should look at the documentation of scanf again. The format string should contain format specifications and non-format characters which are to be matched literally. You seem to assume that you can prompt using scanf(), that's not correct.

You might have more success with this (untested) code:

int main(int argc, char const *argv[])
{
    char array[40];
    printf("Enter your address: ");
    scanf("%39s",array);   //39 + 1/o null character at the end of the character array.
    printf("The address is : %s\n", array);
    return 0;
}

It is also good practive to check the result of the scanf() call to see whether the expected number of values was read. In your case, the value was probably 0 because the literal characters were not matched in your input. When a format specification isn't used, the value of the destination variable is unchanged (in this case undefined, as the array contains the bytes that happen to be on the stack where the variable is allocated.)

Upvotes: 2

Deepak Tatyaji Ahire
Deepak Tatyaji Ahire

Reputation: 5309

Just use the format specifier correctly for the scanf() function and display the custom message using printf().

Have a look at the following code:

// Program will try functionalities of the scanf function
#include<stdio.h>
#include<stdlib.h>

int main(int argc, char const *argv[])
{
    char array[40];
    printf("Enter your address: ");
    scanf("%39s",array);   //39 + 1/o null character at the end of the character array.
    printf("The address is : %s", array);
    return 0;
}

Upvotes: 2

tdao
tdao

Reputation: 17678

Two problems:

  • You did not initialise array properly. It should be:

    char array[40] = "";

  • You mixed up scanf and printf with this statement:

    scanf("Enter your address: %39s",array);

    It should be broken into:

    printf( "Enter your address: " );

    scanf ("%39s", array);

Upvotes: 1

Related Questions