Reputation: 33
I compiled this code using gcc (tdm-1) 5.1.0 and please tell me why the output doesn't contain "hello"
#include<stdio.h>
void main()
{
int i;
char st[20];
printf("Enter a string ");
scanf("%s",st);
for(i=0;i<20;i++)
{
printf("%c",st[i]);
}
}
Input:hello Output: @ @
Upvotes: 1
Views: 103
Reputation: 409472
You print all 20 elements of the array, but if the user entered a string smaller than that not all elements would be initialized. They would be indeterminate and seemingly random.
Remember that char
strings in C are really called null-terminated byte strings. That null-terminated bit is important, and mean you can easily find the end of the string by checking the current character agains '\0'
(which is the terminator character).
Or you could just use the strlen
function to get the length of the string instead:
for(i=0;i<strlen(st);i++) { ... }
Or use the "%s"
format to print the string:
printf("%s", st);
Also note that without any protection the scanf
function will allow you give longer input than is space for in the array, so you need to protect agains that, for example by limiting the amount of characters scanf
will read:
scanf("%19s",st); // Write at most 19 character (*plus* terminator) to the string
Now for why your input doesn't seem to be printed, it's because the indeterminate contents of the uninitialized elements. While you're not going out of bounds of your array, you still go out of bounds of the actual string. Going out of bounds leads to undefined behavior.
What's probably is happening is that some of the "random" indeterminate contents happens to be a carriage return '\r'
, which moves the cursor to the start of the line and the output already written will be overwritten by the uninitialized elements in your array.
Upvotes: 4
Reputation: 11931
Here
char st[20];
st
is a local variable & default array st
contents are garbage not zero. So if you scan less than 20
characters into st
, in that case remaining location of array st
contains garbage, hence it's printing some junk data like @ @
in case of
char st[20];
printf("Enter a string ");
scanf("%s",st);
for(i=0;i<20;i++) {
printf("%c",st[i]);
}
& it's a bad practice as if user entered few char lets say 5 char, then your loop rotates 20
times, internally it will do more operations or consume more CPU cycle.
So if you want to print a char array char by char, then you should rotate a loop until \0
char encounters, for e.g
for(i=0;st[i];i++) { /* this fails when \0 encounters */
printf("%c",st[i]);
}
Or
as others suggested you can print char array st
using single printf
by using %s
format specifier like
printf("%s\n",st); /*here printf starts printing from base address of st
and prints until \0 */
Also it's better to initialize char array st while declaring itself. for e.g
char st[20] ="";
Upvotes: 0
Reputation: 95
Here's a short example as Qubit already explained:
#include <stdio.h>
void main () {
char str1[20];
printf("Enter name: ");
scanf("%s", str1);
printf("Entered Name: %s", str1);
}
Upvotes: 1