My Tips and Tricks for C#, C, C++, MatLAB, Java, LaTeX, Python and more!
Showing posts with label OS X. Show all posts
Showing posts with label OS X. Show all posts
Tuesday, July 16, 2013
The static keyword in C/C++ programming
I will use a very simple project made by 2 files: main.c and extra.h and their contents are reported below:
/* ****** extra.h ************* */
#ifndef EXTRA_H
#define EXTRA_H
/* Declaration of the static variable with file scope */
static int FileVariable=100;
void SetToOne()
{
FileVariable=1;
}
void SetToTwo()
{
FileVariable=2;
}
int GetValue()
{
return FileVariable;
}
int CheckCounter()
{
/* Declaration of the static variable within the function body */
static int nCounter=0;
nCounter++;
return nCounter;
}
#endif
/* *** *** *** main.c *** *** *** */
#include <stdio.h>
#include "extra.h"
int main()
{
printf("Testing the static keyword... \n");
SetToOne();
printf("FileVariable is %d \n", GetValue());
SetToTwo();
printf("FileVariable is %d \n", GetValue());
IncreaseCounter();
IncreaseCounter();
printf("Counter is %d \n", IncreaseCounter());
return 0;
}
On Linux/Mac OS X, to compile the program with gcc and run it, use the command:
cc -o main.c; ./a.out
If you made everything properly, the output will be:
FileVariable is 1
FileVariable is 2
nCounter is 3
What happened?
In C/C++, the static keyword has two basic uses.
1) To limit the visibility of a function or variable to the current file.
This is the case for the variable FileVariable which is visible only to the functions defined in extra.h, i.e. SetToOne, SetToTwo, GetValue and IncreaseCounter.
Just to be clearer, if you already know what global varibles are, well, static variables are just like global variables but only within the file they are defined.
If you attept to to do use or modify FileVariable within the main.c, you'll get an ERROR!!!
Try it!
FileVariable=100; /* does not work! */
The only way to allow the previous expression is to remove the keyword static. Doing so, FileVariable becomes a global variable and you can modify it freely both in main.c and extra.h
2) To let a variable to survive even if the function having it terminates.
Is it clear to you why nCounter is 3?
Within main.c, we called function 3 times! Check it out!
Without the keyword static, the variable Counter would have been created and re-set to zero at each function call.
Try it!
Remove the keyword static and re-run the program.
NOTE:
In C++ the static keyword can also be used within a class, as a counter. In this case, the static variable must be public and must be initialized outside the class.
class MyClass
{
private:
int m_nVal; // just one integer variable
float m_fVal; // just one float variable
public:
MyClass(int nVal, float fVal) // constructor for this class
{
m_nVal=nVal;
m_fVal=fVal;
}
static int Counter; // Our static counter
};
// Initialization of the static variable...
static MyClass::Counter=0;
Monday, July 15, 2013
C++ and gcc on Linux/Mac: How to set your custom directory to include header files
Your profile:
You are using OSX or Linux and you are writing some code in C/C++ compiling it with gcc...
how can you modify the path in which gcc looks for your header files?
Simply enough you have to modify two environment variables: C_INCLUDE_PATH and CPLUS_INCLUDE_PATH, for C and C++ respectively.
Example:
Suppose you want to add the directory /opt/local/include for a C++ project in this case:
export CPLUS_INCLUDE_PATH=${CPLUS_INCLUDE_PATH}:/opt/local/include
in a similar way you can modify the C_INCLUDE_PATH variable.
NOTE
To make this modification permament for every new terminal session, the export line must be put into the hidden .bash_profile file in your home directory.
You are using OSX or Linux and you are writing some code in C/C++ compiling it with gcc...
how can you modify the path in which gcc looks for your header files?
Simply enough you have to modify two environment variables: C_INCLUDE_PATH and CPLUS_INCLUDE_PATH, for C and C++ respectively.
Example:
Suppose you want to add the directory /opt/local/include for a C++ project in this case:
export CPLUS_INCLUDE_PATH=${CPLUS_INCLUDE_PATH}:/opt/local/include
in a similar way you can modify the C_INCLUDE_PATH variable.
NOTE
To make this modification permament for every new terminal session, the export line must be put into the hidden .bash_profile file in your home directory.
Tuesday, May 5, 2009
Mac OS X: How to clear the terminal as in Windows
How do you clear the terminal in Mac OS X in a similar way as you can do in Windows with the command cls?
If you use clear in the Mac OS X terminal (and also on Linux) what you get is a simple page scrolling. On the other hand, in the windows terminal the command cls works actually cleaning all the previously entered commands!
To clean the Mac-terminal the Windows-way you have to use the keyboard shortcut Command+K.
That's it! Enjoy!
If you use clear in the Mac OS X terminal (and also on Linux) what you get is a simple page scrolling. On the other hand, in the windows terminal the command cls works actually cleaning all the previously entered commands!
To clean the Mac-terminal the Windows-way you have to use the keyboard shortcut Command+K.
That's it! Enjoy!
Subscribe to:
Posts (Atom)