473,796 Members | 2,464 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 #1
16 1551
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.

--
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 17 '07 #2
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.
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.hmakes 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.

--
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 18 '07 #3
Haskell Prelude wrote:
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.

Nov 18 '07 #4
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.
Nov 18 '07 #5
In article <sl************ *******@nospam. com>, Haskell Prelude
<no*@avalid.ema il.comwrote on Sunday 18 Nov 2007 5:05 am:
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.
b = a;
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

Nov 18 '07 #6
CBFalconer <cb********@yah oo.comwrites:
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.
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*
Nov 18 '07 #7
On Sun, 18 Nov 2007 00:35:03 +0100 (CET), Haskell Prelude
<no*@avalid.ema il.comwrote:
>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
Nov 18 '07 #8
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.

--
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 18 '07 #9
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 */
--
pete
Nov 19 '07 #10

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
2334
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
14927
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
4380
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
1721
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
2360
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
1590
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
2299
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
9680
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10228
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10173
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10006
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
9052
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
7547
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
5573
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2925
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.