473,804 Members | 2,246 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Some questions about C

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!

Nov 17 '07
16 1552
pete wrote:
>
CBFalconer wrote:

James Kuyper wrote:
CBFalconer wrote:
...
>>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.
>
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.

/* 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.

--
pete
Nov 19 '07 #11
CBFalconer wrote:
James Kuyper wrote:
>CBFalconer wrote:
...
>>>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.
Why is that foolishness? I've used precisely that technique for
encapsulatio n. 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.
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.
Nov 19 '07 #12
pete wrote:
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.

--
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home .att.net>
Try the download section.

--
Posted via a free Usenet account from http://www.teranews.com

Nov 19 '07 #13
CBFalconer said:
pete wrote:
>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.

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Nov 19 '07 #14
On Sun, 18 Nov 2007 02:04:48 -0500, CBFalconer <cb********@yah oo.com>
wrote in comp.lang.c:
Haskell Prelude wrote:

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.
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.l earn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Nov 19 '07 #15
On Sun, 18 Nov 2007 09:44:18 -0500, CBFalconer <cb********@yah oo.com>
wrote in comp.lang.c:
James Kuyper wrote:
CBFalconer wrote:
...
>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.
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.
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.l earn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Nov 19 '07 #16
Richard Heathfield wrote:
>
CBFalconer said:
pete wrote:
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?"


--
pete
Nov 19 '07 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2015
by: Ross Micheals | last post by:
All I have some general .NET questions that I'm looking for some help with. Some of these questions (like the first) are ones that I've seen various conflicting information on, or questions that I'm not sure are specific anomolies that I'm having, or if they are specific known issues I'm facing. 1. In VB.NET, are arrays stored on the stack or the heap? Why must arrays of value types be boxed in order for them to be (effectively) passed by...
12
2335
by: Vibhajha | last post by:
Hi friends, My sister is in great problem , she has this exam of C++ and she is stuck up with some questions, if friends like this group provides some help to her, she will be very grateful. These are some questions:- 7. design and implement a class binsearch for a binary search tree.it includes search,remove and add options.make suitable assumption. 8.explai how pointers to functions can be declared in c++.under what conditions can...
1
1817
by: Tony Johansson | last post by:
Hello Experts! I have some questions about inheritance that I want to have an answer to. It says "Abstract superclasses define a behavioral pattern without specifying the implementation" I know that an abstract class doesn't have any implementaion even if a default implementatiion can be supplied for pure virtual methods. What does it actually mean with saying that an Abstract superclasses define a behavioral pattern?
162
14934
by: techievasant | last post by:
hello everyone, Iam vasant from India.. I have a test+interview on C /C++ in the coming month so plz help me by giving some resources of FAQS, interview questions, tracky questions, multiple choice questions.etc.. I'll be indebted to everyone.. Thanks in advance.. regards vasant shetty Bangalore
50
4387
by: Jatinder | last post by:
I 'm a professional looking for the job.In interview these questions were asked with some others which I answered.But some of them left unanswered.Plz help. Here are some questions on C/C++, OS internals? Q1 . What is the use of pointer to an array? Q2 . What is the use of array of pointers? Q3 . What is the use of pointer to function ? Q4 . How to print through serial port? What is Flow Control(Xon,Xoff) ?
24
1722
by: Kevin | last post by:
Hey guys. I'm looking to get together some VB programmers on Yahoo messenger. I sit at a computer and program all day. I have about 3 or 4 people already, but it would be really cool to have a much larger list of people (for all of us to benefit). I have found it to be an invaluable resource to answer those hard or weird programming questions that come up. If you are interested, please reply, or send me an email at imgroup@gmail.com
7
2361
by: changs | last post by:
Hi, all I have a asm code, I suspect it sort of socket programming. Can anyone here give some instructions on how to determine the function or give the psudo-code in C? Thanks in advance! Tony
3
1641
by: iKiLL | last post by:
Hi all I am building an Windows Mobile 5 forms control in C#, for a Windows Mobile 5 application. I am using CF2.0 and SQL Mobile 2005. The control is a Questions and answer control.
4
1591
by: Mr. X. | last post by:
Hello. I need some help, please. What can an employee ask (technical questions), if I am interviewed of Dot-net developer (also VB.NET and C#). (What are the most general questions, that I may be asked ?) Is there a site that has some questions & answers ? Thanks :)
30
2300
by: GeorgeRXZ | last post by:
Hi Friends, I have some questions related to C Language. 1What is the difference between the standard C language and Non standard C language ? 2which is better C Lanugage, C under Linux/ Unix or C under windows/ DOS ?
0
10096
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9174
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7638
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6866
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5534
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4311
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3834
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3002
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.