| HTML Tutorials |
|
|
| XML Tutorials |
|
|
| Browser Scripting |
|
|
| Server Scripting |
|
|
| .NET (dotnet) |
|
|
| Multimedia |
|
|
| Web Building |
|
|
| Java Tutorials |
|
|
| Programming Langauges |
|
|
| Soft Skills |
|
|
|
|
C has the following simple data types
|
|
|
Variables in C
|
|
Variables in C are memory locations with help of which we can be assigned values and are given names .
To store data in memory for later use,we use variables.
In C, a variable must have to be declared before it can be used.
You can declare Variables at the start of any block of code, but most are found at the start of each function.
Most local variables are destroyed on return from that function and created when the function is called.
A declaration begins with the type, followed by the name of one or more than one variables.
|
|
The following code describe the declaration of variable:
|
int main()
{
int a;
char b;
return 0;
}
|
|
|
C keeps a small set of keywords for its own use only.These keywords are given below:
|
| auto |
break |
case |
char |
const |
continue |
default |
do |
| double |
else |
enum |
extern |
float |
for |
goto |
if |
| int |
long |
register |
return |
short |
signed |
sizeof |
static |
| struct |
switch |
typedef |
union |
unsigned |
void |
volatile |
while |
|
|
Identifiers in C
|
|
Identifiers" or "symbols" are the names you supply for variables, types,labels, and functions in your program.
Identifier names must differ in case and spelling from any keywords.
You cannot use keywords as the identifiers; they are reserved for special use only. You create an identifier by specifying it in the declaration of a variable,function,or type.
In this example which is given below result is an identifier for an integer variable, and printf and main are identifier names for functions.
Identifiers provide names for the given language elements:
- Functions
- Function parameters
- Macros and macro parameters
- Type definitions
- Objects
- Labels
- Enumerated types and enumerators
- Structure and union names
|
#include <stdio.h>
int main()
{
int result;
if ( result != 0 )
printf_s( "Bad file handle\n" );
}
|
|
|
Variable declarations
|
|
Variables are of three different types which are as follows:
- Global Variable
- Local Variable
- Static Variable
Global Variable:
These variables can be accessed by any function of the program.
By associating memory locations with variable names,they are implemented.
If the function is recalled,they do not get recreated .
Declare it outside of all the functions if you want to declare a global variable.
The function will use the variable that was declared within it and ignore the global one,if a variable of the same name is declared both within a function and outside of it.
Local Variable:
Inside the specific function that creates them,these variables only exist.
They are unknown to to the main program and to the other functions.
In this case,they are normally implemented using a stack.
Local variables cease to exist once if the function that created them is completed.
Each time a function is executed or called,they are recreated.
Variable declarations show up in three places:
-
Outside a function. These declarations declare
global variables which are visible throughout the
program (i.e. they have global scope). Use of
global variables is always a big mistake.
-
In the header of a function in the argument list .
These variables are the parameters to the function.
They are only visible inside the function body and their local scope),
exist only from when the function is called to when the function
returns (bounded extent---note that this is
different from what happens in some garbage-collected languages like
Scheme), and get their initial values from the arguments to the
function when it is called.
-
At the start of any block delimited by curly
braces only. Such variables are exist only when the containing function is active
(bounded extent) and visible only within the block (local
scope again). The convention in C is generally to declare all
such local variables at the top of a function; this
is different from the convention in C++ or Java, which encourage
variables to be declared when they are first time used
|
|
The following program demonstrate the use of global and local variables.
|
#include <stdio.h>
int counter = 0; /* global because we are outside all blocks.*/
int func(void);
main()
{
counter++; /* global because it has not been declared within this block */
printf("counter is %2d before the call to func\n", counter);
func(); /* call a function. */
printf("counter is %2d after the call to func\n", counter);
}
int func(void)
{
int counter = 10; /* local variable because it has declared within this block */ */
printf("counter is %2d within func\n", counter);
}
|
|
|
Using static variables
|
|
Another important feature of the variable scoping is the static variable.
In a local function scope,a static variable exists only , but it does not lose its value when program execution leaves this scope.
Consider the example which is given below:
|
#include <stdio.h>
main()
{
Test();
}
function Test()
{
int a = 0;
printf("a is %d within func\n", a)
a++;
}
|
|
|
Since every time the function is called it sets a to 0 and prints "0",this function is quite useless .
The a++ which increments the variable serves no purpose since as soon as the function exits then a variable disappears.
The a variable is declared static to make a useful counting function which will not lose track of the current count:
|
#include <stdio.h>
main()
{
Test();
}
function Test()
{
static int a = 0;
printf("a is %d within func\n", a)
a++;
}
|
|
|
Basic types
|
|
There are 4 basic types of variable in C; they are: char, int, double and float
|
| Type name |
Meaning |
| char |
The most basic unit addressable by the machine; typically a
single octet. This is an integral type. |
| int |
The most natural size of integer for the machine; typically
a whole 16, 32 or 64 bit addressable word. |
| float |
A single-precision floating point value. |
| double |
A double-precision floating point value. |
|
Share And Enjoy:These icons link to social bookmarking sites where readers can share and discover new web pages.
Keywords:
c static variables, c variables character, c variables values,
c variables array
|
|
| HTML Quizes |
|
|
| XML Quizes |
|
|
| Browser Scripting Quizes |
|
|
| Server Scripting Quizes |
|
|
| .NET (dotnet) Quizes |
|
|
| Multimedia Quizes |
|
|
| Web Building Quizes |
|
|
| Java Quizes |
|
|
| Programming Langauges Quizes |
|
|
| Soft Skills Quizes |
|
|
|