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;
}
Answer: Option
Explanation:
str[6] = "BIX"; - Nonportable pointer conversion.
Discussion:
16 comments Page 1 of 2.
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);
}
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);
}
Saikrishna Reddy said:
1 decade 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.
George said:
1 decade ago
Hello,
The code in Linux works find and gives "India" as the answer. The reason is this: in the declaration :
char str[10]="India";
// the stringlength is 5 so the
// the string terminator will be at str[5]
// any operation after str[5] will be not
// valid.
The code in Linux works find and gives "India" as the answer. The reason is this: in the declaration :
char str[10]="India";
// the stringlength is 5 so the
// the string terminator will be at str[5]
// any operation after str[5] will be not
// valid.
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?
sttr[6]="bix"
printf"%s",str);
Here what will be output?
Can we initialize same string variable str, twice with different size?
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.
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.
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".
"Assignment makes integer from pointer without a cast". But there is a smooth compilation of the code with the output as "India".
(1)
Mitkari.sunil said:
1 decade ago
I thought that overwriting in strings is not possible by accessing directly.
Mani said:
1 decade ago
When i execute this in gcc compiler it gives the o/p as India.
Swathi said:
1 decade ago
Can any one explain non-portable conversion ?
Priya said:
10 years ago
So what will be the output? How is it error?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers