C Programming - Declarations and Initializations - Discussion
Discussion Forum : Declarations and Initializations - General Questions (Q.No. 2)
2.
What are the types of linkages?
Answer: Option
Explanation:
External Linkage-> means global, non-static variables and functions.
Internal Linkage-> means static variables and functions with file scope.
None Linkage-> means Local variables.
Internal Linkage-> means static variables and functions with file scope.
None Linkage-> means Local variables.
Discussion:
95 comments Page 1 of 10.
RaviTeja.BSN said:
1 decade ago
In one interview TR asked me to write a program that prints its source code, after that I got that program in c programing app in play store but i can't understand the program.
Print Source Code - /* Write a program that prints its own code as the output */
#include<stdio.h>
int main() {
FILE *fp;
char c;
fp = fopen(__FILE__,"r");
do{
c= getc(fp);
putchar(c);
}
while(c != EOF);
fclose(fp);
getch();
return 0;
}
And in the app he showed same program as output including the #include everything how is it possible anyone please explain?
Print Source Code - /* Write a program that prints its own code as the output */
#include<stdio.h>
int main() {
FILE *fp;
char c;
fp = fopen(__FILE__,"r");
do{
c= getc(fp);
putchar(c);
}
while(c != EOF);
fclose(fp);
getch();
return 0;
}
And in the app he showed same program as output including the #include everything how is it possible anyone please explain?
Prathyusha said:
1 decade ago
//SATHISH explanation was correct!..
int main()
{
printf(" %d",root());
printf(" %d",root());
}
int root()
{
static int i;
i++;
return i;
}
BY DEFAULT ,ANY INTEGER OR STATIC INT VARIABLE VALUE IS ZERO.
BUT WHEN EACH TIME THE FUNCTION WAS CALLED ,THE DIFFERENCE OF NORMAL INT AND STATIC INT CAN BE SEEN.
IF THE "i" HERE IS NORMAL INTEGER IT WILL JUST RETURN ALWAYS VALUE "1" ,EVERY TIME IT IS CALLED .
BUT THE STATIC VARIABLE WILL HOLD PREVIOUS VALUES OF "i".EVERY TIME WHEN THE FUNCTION IS CALLED IT WILL THE PREVIOUS VALUES ARE RESTORED.
int main()
{
printf(" %d",root());
printf(" %d",root());
}
int root()
{
static int i;
i++;
return i;
}
BY DEFAULT ,ANY INTEGER OR STATIC INT VARIABLE VALUE IS ZERO.
BUT WHEN EACH TIME THE FUNCTION WAS CALLED ,THE DIFFERENCE OF NORMAL INT AND STATIC INT CAN BE SEEN.
IF THE "i" HERE IS NORMAL INTEGER IT WILL JUST RETURN ALWAYS VALUE "1" ,EVERY TIME IT IS CALLED .
BUT THE STATIC VARIABLE WILL HOLD PREVIOUS VALUES OF "i".EVERY TIME WHEN THE FUNCTION IS CALLED IT WILL THE PREVIOUS VALUES ARE RESTORED.
Saikiran said:
1 decade ago
What Prathyusha said is correct. That can be easily understood using this program.
#include<stdio.h>
int main()
{
printf("\n%d",funi());
printf("\n%d",funi());
printf("\n%d",funi());
printf("\n%d",funj());
printf("\n%d",funj());
printf("\n%d",funj());
//printf("\ni=%d\nj=%d",i,j);
return 0;
}
int funi()
{
int i;
i++;
return i;
}
int funj()
{
static int j;
j++;
return j;
}
output:
1
1
1
1
2
3
#include<stdio.h>
int main()
{
printf("\n%d",funi());
printf("\n%d",funi());
printf("\n%d",funi());
printf("\n%d",funj());
printf("\n%d",funj());
printf("\n%d",funj());
//printf("\ni=%d\nj=%d",i,j);
return 0;
}
int funi()
{
int i;
i++;
return i;
}
int funj()
{
static int j;
j++;
return j;
}
output:
1
1
1
1
2
3
Saiba Murmu said:
1 decade ago
It gives result according to the compiler.
GCC compiler gives Result: 9.
TCC compiler gives Result: 8.
Explanation:
In GCC compiler, variables are stored in stack memory area. First value 4 is stored after incrementing by the pre-increment operator then second value 5 is stored after increment. So the Result is 9.
But in case of TCC compiler first value 4 is stored in stack then before increment the second value of x=4 is stored. So the Result is 8.
GCC compiler gives Result: 9.
TCC compiler gives Result: 8.
Explanation:
In GCC compiler, variables are stored in stack memory area. First value 4 is stored after incrementing by the pre-increment operator then second value 5 is stored after increment. So the Result is 9.
But in case of TCC compiler first value 4 is stored in stack then before increment the second value of x=4 is stored. So the Result is 8.
Jatin said:
1 decade ago
Its too late and ignore if not but it shouldn't be like this:
If x = 2.
z = x++ + ++x // here because x = 2, value 2 is assign to x (value 2 stored to perform overall function) n stored after that it'll increment by 1.
=> z = (2) ++.
That will 3. Till z = x++ value of x is 3.
After that z = 3 + ++x //till here x = 3.
So further by pre-increment 3 will incremented by 1 value will be 4. So z = 2 + 4 = 6.
If x = 2.
z = x++ + ++x // here because x = 2, value 2 is assign to x (value 2 stored to perform overall function) n stored after that it'll increment by 1.
=> z = (2) ++.
That will 3. Till z = x++ value of x is 3.
After that z = 3 + ++x //till here x = 3.
So further by pre-increment 3 will incremented by 1 value will be 4. So z = 2 + 4 = 6.
Sravani said:
1 decade ago
Hey guys x++ + ++x though we are making changes to x that will be updated in register because of optimization it treats both x as same value and makes z=x+x;
x++=value is incremented after the statement completes and x=2.
After ++x= pre inc so x in inc and x value is 3 and is stored in only one place now addition happens z=x+x,z=3+3=6;
But if you use volatile in front of int x; then it will give z=5;
x++=value is incremented after the statement completes and x=2.
After ++x= pre inc so x in inc and x value is 3 and is stored in only one place now addition happens z=x+x,z=3+3=6;
But if you use volatile in front of int x; then it will give z=5;
Keerthi said:
1 decade ago
Apple,U said right. but easiest way for shwetha
Here
z=(++x)+(++x)
For 1st ++x,the value of x is first increased about one and then assign ,that means the value of x changes from 5 to 6.
Its clear in the same way the 2 nd ++x, x changes from 6 to 7
So, final value of x is 7
That means z=x+x
For 1st and 2nd x's the value is same that is 7
Therefore
Z=7+7
=14.
Here
z=(++x)+(++x)
For 1st ++x,the value of x is first increased about one and then assign ,that means the value of x changes from 5 to 6.
Its clear in the same way the 2 nd ++x, x changes from 6 to 7
So, final value of x is 7
That means z=x+x
For 1st and 2nd x's the value is same that is 7
Therefore
Z=7+7
=14.
Alok said:
1 decade ago
@Sonali see this one.
z=(++x)+(++x)
For 1st ++x, the value of x is first increased about one and then assign, that means the value of x changes from 5 to 6.
Its clear in the same way the 2 nd ++x, x changes from 6 to 7.
So, final value of x is 7.
That means z = x+x.
For 1st and 2nd x's the value is same that is 7.
Therefore Z = 7+7.
z=(++x)+(++x)
For 1st ++x, the value of x is first increased about one and then assign, that means the value of x changes from 5 to 6.
Its clear in the same way the 2 nd ++x, x changes from 6 to 7.
So, final value of x is 7.
That means z = x+x.
For 1st and 2nd x's the value is same that is 7.
Therefore Z = 7+7.
Prabhat said:
7 years ago
Pre-increment and post-increment behaviour depends on the compiler's implementation of it.
Each compiler can give you the different result for the same operation with respect to pre and post increment and decrement. As there is no standard behaviour defined by ANSI C standard.
We shouldn't use these two operators in an expression.
Each compiler can give you the different result for the same operation with respect to pre and post increment and decrement. As there is no standard behaviour defined by ANSI C standard.
We shouldn't use these two operators in an expression.
Sathish narasimman said:
1 decade ago
static example..
int main()
{
printf(" %d",root());
printf(" %d",root());
}
int root()
{
static int i;
i++;
return i;
}
it will print the updated value every time example: o/p=1 2 3 4...
where as if we use int i in the place of static int i, in the root function.. always print 1 1 1 1
int main()
{
printf(" %d",root());
printf(" %d",root());
}
int root()
{
static int i;
i++;
return i;
}
it will print the updated value every time example: o/p=1 2 3 4...
where as if we use int i in the place of static int i, in the root function.. always print 1 1 1 1
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers