943,945 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2852
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Dec 15th, 2005
0

Doubts in C

Expand Post »
Plz answer my these C doubts-
Q. 1- If i have 3 source files and i declare a static variable in the source file s2.c then which statement is true:-
That static variable is created when the program starts executing and is destroyed when the whole program finishes executing.
OR
That static variable is created when the source file s2.c starts executing and is destroyed when the file s2.c finishes executing.
(I dont want to know abt the scope of the static variable)

Q. 2- Header contains the declarations of functions & variables and in C it is not necessary(even though its safer) to give a prototype of a function before its use in the program. So does it mean that if we dont include a header file which declares a function X(), even then we can use that function(bcoz the header file contains only the prototype of the function, which is not necessary in C).

Q. 3- What do the .obj and .lib files contain?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
akshayabc is offline Offline
15 posts
since Jun 2005
Dec 15th, 2005
0

Re: Doubts in C

Q1: Alternative 1
Q2: If the function X returns an integer, uses standard calling convention (and possibly is not dynamically linked, I'm not sure about the actual meaning of __declarespec(import)).
Q3: .obj is most oftenly compiled code, all functions and variables from a compiled source file. .lib I think contains either multiple such object files in a kind of archive, or link information for dynamic linkage (thought I'm not quite sure of this since it mostly just work for me).
Reputation Points: 29
Solved Threads: 4
Junior Poster in Training
perniciosus is offline Offline
78 posts
since Nov 2005
Dec 15th, 2005
0

Re: Doubts in C

>Plz answer my these C doubts-
"Doubt" is sometimes synonymous with "question", but not in this case. It's very common to ignorantly use doubt in place of question, and it's usually wrong. What you have a C questions, not C doubts.

>I dont want to know abt the scope of the static variable
Obviously, since your question has nothing to do with scope and everything to do with storage duration. There's no need to qualify your question as such because if someone starts talking about scope, you know that they're not qualified to answer it correctly.

>in C it is not necessary(even though its safer) to give a
>prototype of a function before its use in the program.
Wrong on two counts. First, the latest C standard removed that dangerous feature. Second, even in C89, the implicit function declaration doesn't always work depending on the arguments and return value of the function in question.

>What do the .obj and .lib files contain?
It depends heavily on the compiler and linker in question. The generic answer is that object files contain object code, which is the primary output of the compiler. The linker then uses the object code to piece together an executable file. Library files are basically independent collections of object code and links.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 17th, 2007
0

Re: Doubts in C

Click to Expand / Collapse  Quote originally posted by akshayabc ...
Plz answer my these C doubts-
Q. 1- If i have 3 source files and i declare a static variable in the source file s2.c then which statement is true:-
That static variable is created when the program starts executing and is destroyed when the whole program finishes executing.
OR
That static variable is created when the source file s2.c starts executing and is destroyed when the file s2.c finishes executing.
(I dont want to know abt the scope of the static variable)

http://www.daniweb.com/techtalkforum...s/solution.gifA)second statement is true

Q. 2- Header contains the declarations of functions & variables and in C it is not necessary(even though its safer) to give a prototype of a function before its use in the program. So does it mean that if we dont include a header file which declares a function X(), even then we can use that function(bcoz the header file contains only the prototype of the function, which is not necessary in C).

http://www.daniweb.com/techtalkforum...s/solution.gifA)function prototype is necessary only in C++ as its one of the syntax that u should give function definion b4 u use it in main() as function prototype.
In C its not needed 2 declare the prototype of a function.
The answer 4 ur question is yes if u r working with a c compiler.

Q. 3- What do the .obj and .lib files contain?
http://www.daniweb.com/techtalkforum...s/solution.gifA) .obj and .lib are the files with extension obj and lib.

.obj refer to the files related to have object code contained in them.
.lib refer to files related to library functions.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
see2penc is offline Offline
1 posts
since Mar 2007
Mar 17th, 2007
0

Re: Doubts in C

Click to Expand / Collapse  Quote originally posted by Narue ...
>Plz answer my these C doubts-
"Doubt" is sometimes synonymous with "question", but not in this case. It's very common to ignorantly use doubt in place of question, and it's usually wrong. What you have a C questions, not C doubts.
I think this is something taught in India. The only posters that have 'doubts' seem to be from there...
Moderator
Reputation Points: 3278
Solved Threads: 894
Posting Sage
WaltP is online now Online
7,741 posts
since May 2006
Jun 21st, 2010
-1
Re: Doubts in C
#include<iostream.h>
#include<conio.h>
int &maxref(int &a, int &b)
{
if (a>b)
return a;
else
return b;
}
void main()
{
int x=20,y=30, max=0;
clrscr();
maxref(y,y)=-1;
cout<<"\n value of x is :"<<x;
cout<<"\n value of y is :"<<y;
getch();
}

This program output is
value of xis 20
value of y is : -1
but how to get this output?
help me.
Reputation Points: 9
Solved Threads: 0
Newbie Poster
raja.mdu is offline Offline
2 posts
since Jun 2010
Jun 21st, 2010
-1
Re: Doubts in C
#include<iostream.h>
#include<conio.h>
void main()
{
int x[5]={5,2,3,4,5}, y[5]={1,4,3,2,1};
int result[5]={0,0,0,0,0};
int i=0;
clrscr();
while(i++<5)
result[i]=x[i]-y[i];
cout<<"\n The contents of the array are:\n";
i=0;
do
{
cout<<'\t'<<x[i]<<'\t'<<y[i]<<'\t'<<result[i]<<'\n';
i++;
}
while(i<5);
getch();
}
Q:
How to work this program ? Help me.
Reputation Points: 9
Solved Threads: 0
Newbie Poster
raja.mdu is offline Offline
2 posts
since Jun 2010
Jul 29th, 2010
-1
Re: Doubts in C
int a=1;
printf("%d%d%d",a,++a,a++);

the doubt is the output is not 122
its 331.can any one explain it
Reputation Points: 10
Solved Threads: 0
Newbie Poster
reddy55 is offline Offline
1 posts
since Jul 2010
Jul 30th, 2010
0
Re: Doubts in C
Hi first you need to study operator precedence well. Here you are using ++ operator post fix and prefix. ++ directly charge the value in variable address.

post fix (a++)- print first and increment
perfix (++a)- increment first and print the value
[CODE][
printf("%a%a%a", a, ++a, a++);
1 //and then increment a = 2;
printf("%a%a%a", a, ++a, a++);
3 1 //its prefix so increment a = 3 an print
printf("%a%a%a", a, ++a, a++);
3 3 1
/CODE]
Reputation Points: 10
Solved Threads: 2
Light Poster
ganesh_IT is offline Offline
39 posts
since Jul 2010
Jul 30th, 2010
1
Re: Doubts in C
Quote originally posted by ganesh_IT; ...
Hi first you need to study operator precedence well. Here you are using ++ operator post fix and prefix. ++ directly charge the value in variable address.

post fix (a++)- print first and increment
perfix (++a)- increment first and print the value
[CODE][
printf("%a%a%a", a, ++a, a++);
1 //and then increment a = 2;
printf("%a%a%a", a, ++a, a++);
3 1 //its prefix so increment a = 3 an print
printf("%a%a%a", a, ++a, a++);
3 3 1
/CODE]
First you need to study side effects and sequence points. The snippet exhibits undefined behavior, which means that the output can be anything at all. Further, the output isn't even required to be consistent. You can run the program twice and completely different results should be expected with undefined behavior.

This thread has become a wealth of bad examples...
Last edited by Narue; Jul 30th, 2010 at 8:05 am.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC