Some questions about C

H

Haskell Prelude

Hello Friends -

Can anyone answer these C questions?

1. What is the effect of making an internal (local) variable static?
2. What is the effect of making an external (non-local) variable static?
3. What, possibly including garbage, is output by the following code?

int a = 10;
int b = 12;

ftn2(int a) {
int c = 13;
printf("%d\n", a+b);
return ++a;
}

ftn(int b) {
b = a;
printf("%d\n", b);
return ftn2(a);
}

void main()
{
int x = ftn(b);
printf("%d\n", x);
}

Thanks to all!
 
R

Richard Heathfield

Haskell Prelude said:
Hello Friends -

Can anyone answer these C questions?

1. What is the effect of making an internal (local) variable static?

Assuming that you mean an object defined at function scope or more locally
than that, adding the 'static' storage-class qualifier gives the object
static storage duration, which means that "the object exists and retains
its last-stored value throughout the execution of the entire program"
(3.1.2.4).
2. What is the effect of making an external (non-local) variable static?

Assuming that you mean an object defined at file scope, adding the 'static'
storage-class qualifier has no effect on object lifetime since such an
object already has static storage duration by virtue of being at file
scope, but it does restrict visibility of the object to the translation
unit currently being translated.
3. What, possibly including garbage, is output by the following code?

The behaviour of "the following code" (snipped) is undefined for at least
two reasons.
 
C

CBFalconer

Haskell said:
1. What is the effect of making an internal (local) variable static?

If by local you mean scoped within a function, then it becomes a
non-auto variable and preserves its value between function calls.
2. What is the effect of making an external (non-local) variable static?

Pure foolishness, if non-local means file scope. static here
prevents exporting the variable.
3. What, possibly including garbage, is output by the following code?

int a = 10;
int b = 12;

ftn2(int a) {
int c = 13;
printf("%d\n", a+b);
return ++a;
}

ftn(int b) {
b = a;
printf("%d\n", b);
return ftn2(a);
}

These functions would be fine if you just defined their return type
and #included <stdio.h>. The default int type is no longer valid
for C99. ftn2 prints the value of (a + 13) and returns (a + 1).
ftn ignores the input value of b, prints '10', then 23, and returns
11.
void main()
{
int x = ftn(b);
printf("%d\n", x);
}

This is meaningless. main ALWAYS returns an int. You can always
return 0, or EXIT_FAILURE or EXIT_SUCCESS. The non-zero values
require #include stdlib.h. Lack of #include <stdio.h> makes all
the printf statements pure nonsense.

This homework like query would normally be ignored, but there are
too many built in errors to allow it to go uncriticized.
 
M

Mark McIntyre

Haskell said:
Hello Friends -

Can anyone answer these C questions?

1. What is the effect of making an internal (local) variable static?
2. What is the effect of making an external (non-local) variable static?
3. What, possibly including garbage, is output by the following code?

These all sound like homework questions. You should really read your C
book and lecture notes.
void main()

Don't Do That.
main() returns an int in C.
 
J

James Kuyper

CBFalconer wrote:
....
Pure foolishness, if non-local means file scope. static here
prevents exporting the variable.

Why is that foolishness? I've used precisely that technique for
encapsulation. File scope variables are something to avoided, in
general. However, when they are justified, in some cases the dangers of
using them can be reduced by declaring them static. This requires that
all functions which refer to the static file scope variable be defined
in the same translation unit, which may be quite feasible if the number
of such functions is small.
 
S

santosh

Haskell Prelude said:
Hello Friends -

Can anyone answer these C questions?

1. What is the effect of making an internal (local) variable static?

It retains it's value across execution into and out of it's scope.
2. What is the effect of making an external (non-local) variable
static?

The C term for "external" is file scope. And the effect is to reduce
it's visibility to the rest of the translation unit.
3. What, possibly including garbage, is output by the
following code?

int a = 10;
int b = 12;

ftn2(int a) {

Implicit return of int is obsolete. Declare 'ftn2' to explicitly return
a value or void.

int ftn2(int a) { ...

Furthermore 'ftn2's 'a' will "shadow" the filescope 'a'. It's not often
a good idea.
int c = 13;
printf("%d\n", a+b);

This will print the sum of 'ftn2's argument and the current value of 'b'
(at this point 12).
return ++a;

Returns a value one more than it's argument.

Note that 'c' is not used.
}

ftn(int b) {

Same comments about shadowing and implicit return as above.

This references the filescope 'a'.
printf("%d\n", b);
return ftn2(a);
}

void main()

This is incorrect. In Standard C main must return an int. Also the empty
parenthesis signals to the compiler that main takes an unspecified
number and type of arguments, *not* no arguments, as in C++. To specify
the latter use the void keyword.

int main(void) { ...
{
int x = ftn(b);
printf("%d\n", x);
}

Thanks to all!

Output will be:

10
22
11
 
C

CBFalconer

James said:
CBFalconer wrote:
...

Why is that foolishness? I've used precisely that technique for
encapsulation. File scope variables are something to avoided, in
general. However, when they are justified, in some cases the
dangers of using them can be reduced by declaring them static.
This requires that all functions which refer to the static file
scope variable be defined in the same translation unit, which may
be quite feasible if the number of such functions is small.

Because if it is external it can't be static, or it won't be found.
 
R

Richard

CBFalconer said:
If by local you mean scoped within a function, then it becomes a
non-auto variable and preserves its value between function calls.


Pure foolishness, if non-local means file scope. static here
prevents exporting the variable.

I fail to see any foolishness. What are you talking about?
This homework like query would normally be ignored, but there are
too many built in errors to allow it to go uncriticized.

*blink*
 
B

Barry Schwarz

Hello Friends -

Can anyone answer these C questions?

You will find people much more willing to help if you make an effort
to do your homework first.


Remove del for email
 
P

pete

CBFalconer said:
Because if it is external it can't be static, or it won't be found.



/* BEGIN new.c */

#include <stdio.h>

static char string[] = "What do you mean?";

int main(void)
{
puts(string);
return 0;
}

/* END new.c */
 
P

pete

pete said:
Because if it is external it can't be static, or it won't be found.

/* BEGIN new.c */

#include <stdio.h>

static char string[] = "What do you mean?";

int main(void)
{
puts(string);
return 0;
}

/* END new.c */

By "external variable",
I mean an object that was declared in an "external declaration"
and defined in an "external definition",
rather than a variable with external linkage.

I interpreted OP's comment "(non-local)",
to mean that "file scope"
was the definition being used for "external".

N869
6.9 External definitions
Syntax
[#1]
translation-unit:
external-declaration
translation-unit external-declaration
external-declaration:
function-definition
declaration
Constraints
[#2] The storage-class specifiers auto and register shall
not appear in the declaration specifiers in an external
declaration.
[#3] There shall be no more than one external definition for
each identifier declared with internal linkage in a
translation unit. Moreover, if an identifier declared with
internal linkage is used in an expression (other than as a
part of the operand of a sizeof operator), there shall be
exactly one external definition for the identifier in the
translation unit.
Semantics
[#4] As discussed in 5.1.1.1, the unit of program text after
preprocessing is a translation unit, which consists of a
sequence of external declarations. These are described as
``external'' because they appear outside any function (and
hence have file scope). As discussed in 6.7, a declaration
that also causes storage to be reserved for an object or a
function named by the identifier is a definition.
[#5] An external definition is an external declaration that
is also a definition of a function or an object. If an
identifier declared with external linkage is used in an
expression (other than as part of the operand of a sizeof
operator), somewhere in the entire program there shall be
exactly one external definition for the identifier;
otherwise, there shall be no more than one.
 
J

James Kuyper

CBFalconer said:
Because if it is external it can't be static, or it won't be found.

If the phrase 'external ... variable' was meant to be a short form for
"variable with external linkage", then the question is meaningless, for
precisely that reason.

The questions were clearly not written in strictly correct standardese.
Therefore, some interpretation is required. My interpretation was that
by "external (non-local)" he meant file scope. My interpretation has the
advantage that it makes the question meaningful; but it might not be
correct.
 
C

CBFalconer

pete said:
CBFalconer wrote:
.... snip ...
Because if it is external it can't be static, or it won't be found.

/* BEGIN new.c */
#include <stdio.h>
static char string[] = "What do you mean?";

int main(void) {
puts(string);
return 0;
}
/* END new.c */

Note the total absence of the 'extern' keyword.
 
R

Richard Heathfield

CBFalconer said:
pete said:
CBFalconer wrote:
... snip ...
Because if it is external it can't be static, or it won't be found.

/* BEGIN new.c */
#include <stdio.h>
static char string[] = "What do you mean?";

int main(void) {
puts(string);
return 0;
}
/* END new.c */

Note the total absence of the 'extern' keyword.

Note the absence of the word 'extern' from the original question, too.

Quoth the Standard: "If the declaration of an identifier for an object has
file scope and an initializer, the declaration is an external definition
for the identifier." Statically qualified declarations are not excluded
from this.
 
J

Jack Klein

If by local you mean scoped within a function, then it becomes a
non-auto variable and preserves its value between function calls.


Pure foolishness, if non-local means file scope. static here
prevents exporting the variable.

Actually, defining a file scope object with the static keyword merely
changes the linkage of the identifier from external to internal. It
does nothing at all to the "variable", actually object, itself.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
 
J

Jack Klein

Because if it is external it can't be static, or it won't be found.

Your terminology is confused. The C standard defines any definition
at file scope as an external definition. Even if the definition uses
the static keyword. The fact that the static keyword changes the
linkage of the identifier from external to internal does not change
the fact that it is an external definition.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
 
P

pete

Richard said:
CBFalconer said:
pete said:
CBFalconer wrote:
... snip ...

Because if it is external it can't be static,
or it won't be found.

/* BEGIN new.c */
#include <stdio.h>
static char string[] = "What do you mean?";

int main(void) {
puts(string);
return 0;
}
/* END new.c */

Note the total absence of the 'extern' keyword.

Note the absence of the word 'extern' from the original question, too.

Quoth the Standard:
"If the declaration of an identifier for an object has
file scope and an initializer,
the declaration is an external definition
for the identifier.
" Statically qualified declarations are not excluded from this.

OP was clear that by "external" he meant "(non-local)",
which CBFalconer properly understood to mean "file scope",
just slightly before becoming confused.

"2. What is the effect of making an external (non-local) variable
static?"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top