|
C Programming Tutorials
Basics of C:
Facts about C
Why to Use C
C Program File
C Compilers
Program Structure:
Simple C Program
C
Program Compilation
Basic DataTypes:
DataTypes
Modifiers
Qualifiers
Arrays
Variable Types:
Local Variable
Global
Variable
Storage Classes:
auto storage class
register storage
class
static storage
class
extern storage
class
Using Constants:
Defining Constants
The enum Data Types
Operator Types:
Arithmetic Operators
Logical Operators
Bitwise Operators
Assignment Operators
Misc
Operators
Control Statements:
Branching
Looping
Input and Output:
printf() function
scanf() function
Pointing to Data:
Pointers and Arrays
Pointer
Arithmetic
Pointer Arithmetic
with arrays
Functions:
Using functions
Declaration and
Definition
Strings:
Reading and Writing
Strings
String Manipulation Function
Structured DataTypes:
Structure
Pointer to Structure
Working with Files:
Files
Basic I/O
Bits:
Bits Manipulation
Bits Field
Pre-Processors:
Pre-Processors Examples
Parameterized Macros
Macro
Caveats
Useful Concepts
Built-in Library Functions:
String Manipulation
Function
Memory Management
Function
Buffer
Manipulation
Character
Functions
Error Handling
Functions
Soft Skills
Communication Skills
Leadership Skills
.........More
|
|
C Programming Tutorials
Variable Types
A variable is just a named area of storage that can hold a single value (numeric
or character). The C language demands that you declare the name of each variable
that you are going to use and its type, or class, before you actually try to do
anything with it.
The Programming language C has two main variable types
-
Local Variables
-
Global Variables
Local Variables
-
Local variables scope is confined within the block or
function where it is defined. Local variables must always be defined at the
top of a block.
-
When a local variable is defined - it is not initialized
by the system, you must initialize it yourself.
-
When execution of the block starts the variable is
available, and when the block ends the variable 'dies'.
Check following example's output
main()
{
int i=4;
int j=10;
i++;
if (j > 0)
{
/* i defined in 'main' can be seen */
printf("i is %d\n",i);
}
if (j > 0)
{
/* 'i' is defined and so local to this block */
int i=100;
printf("i is %d\n",i);
}/* 'i' (value 100) dies here */
printf("i is %d\n",i); /* 'i' (value 5) is now visable.*/
}
This will generate following output
i is 5
i is 100
i is 5 |
Here ++ is called incremental operator and it increase the
value of any integer variable by 1. Thus i++ is equivalent to i = i + 1;
You will see -- operator also which is called decremental operator and it
decrease the value of any integer variable by 1. Thus i-- is equivalent to i = i
- 1;
Global Variables
Global variable is defined at the top of the program file and it can be visible
and modified by any function that may reference it.
Global variables are initialized automatically by the system when you define
them!
| Data Type |
Initialser |
| int |
0 |
| char |
'\0' |
| float |
0 |
| pointer |
NULL |
If same variable name is being used for global and local
variable then local variable takes preference in its scope. But it is not a good
practice to use global variables and local variables with the same name.
int i=4;
/* Global definition */
main()
{
i++;
/* Global variable */
func();
printf( "Value of i = %d -- main function\n", i );
}
func()
{
int i=10;
/* Local definition */
i++;
/* Local variable */
printf( "Value of i = %d -- func() function\n", i );
}
This will produce following result
Value of i = 11 -- func() function
Value of i = 5 -- main function |
i in main function is global and will be incremented to 5. i
in func is internal and will be incremented to 11. When control returns to main
the internal variable will die and and any reference to i will be to the global.
NEXT >> Storage
Classes
Have a Question ?
post your questions here. It
will be answered as soon as possible.
Check
C Aptitude Questions
for more C Aptitude Interview Questions with Answers
Check
C Interview Questions
for more C Interview Questions with Answers
|