C Programming - Strings - Discussion

Discussion Forum : Strings - Find Output of Program (Q.No. 31)
31.
What will be the output of the program in Turbo C?
#include<stdio.h>

int main()
{
    char str[10] = "India";
    str[6] = "BIX";
    printf("%s\n", str);
    return 0;
}
India BIX
BIX
India
Error
Answer: Option
Explanation:

str[6] = "BIX"; - Nonportable pointer conversion.

Discussion:
16 comments Page 2 of 2.

Suma said:   1 decade ago
I got the warning.

"Assignment makes integer from pointer without a cast". But there is a smooth compilation of the code with the output as "India".
(1)

Anisha said:   10 years ago
str[10]="india"
sttr[6]="bix"
printf"%s",str);

Here what will be output?

Can we initialize same string variable str, twice with different size?

Priya said:   10 years ago
So what will be the output? How is it error?

Mahant said:   9 years ago
It's printing India in windows 16 also.

Suraj said:   8 years ago
When you will assign any value to a pointer variable then compiler will automatically convert given value in address of pointer type such automatic type conversion is known as non-portable pointer conversion. Non-portable pointer conversion is not cause of any compilation error but it is bad coding style. Hence compiler will send one warning message.

For example:

#include<stdio.h>
void main(){
int a =10;
int *p;
int **q;
p=a; //Nonportable pointer conversion
q=a; //Nonportable pointer conversion
printf("%d %d",*p,**q);
}

Ruku said:   7 years ago
Hello all,

Here data type for str[6]="BIX"; is not defined so it will not consider as a string.
It should be de char str[6]="BIX"; then it will print BIX.


Post your comments here:

Your comments will be displayed after verification.