0% found this document useful (0 votes)
13 views7 pages

Variables in C

Uploaded by

reaganottawa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views7 pages

Variables in C

Uploaded by

reaganottawa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

C Variables

• Variables are memory locations(storage area) in the C programming language.


• The primary purpose of variables is to store data in memory for later use.
Unlike constants which do not change during the program execution, variables
value may change during execution. If you declare a variable in C, that means
you are asking the operating system to reserve a piece of memory with that
variable name.

Outline:

# Variable Declaration in C
# Variable Declaration and Initialization
# Variable Assignment
# There are some rules on choosing variable names
# C Program to Print Value of a Variable

Variable Declaration in C
Syntax:
type variable_name;

or
type variable_name, variable_name, variable_name;

Variable Declaration and Initialization


Example:
int width, height=5;
char letter='A';
float age, area;
double d;

/* actual initialization */width = 10;


age = 26.5;

Variable Assignment

A variable assignment is a process of assigning a value to a variable.


Example:
int width = 60;
int age = 31;

There are some rules on choosing variable names


• A variable name can consist of Capital letters A-Z, lowercase letters a-z, digits 0-9, and
the underscore character.
• The first character must be a letter or underscore.
• Blank spaces cannot be used in variable names.
• Special characters like #, $ are not allowed.
• C keywords cannot be used as variable names.
• Variable names are case sensitive.
• Values of the variables can be numeric or alphabetic.
• Variable type can be char, int, float, double, or void.

C Program to Print Value of a Variable


Example:
#include<stdio.h>

void main()
{
/* c program to print value of a variable */
int age = 33;
printf("I am %d years old.\n", age);
}
Program Output:

I am 33 years old.
C Type Casting

Type Casting in C is used to convert a variable from one data type to another data type,
and after type casting compiler treats the variable as of the new data type.
Syntax:
(type_name) expression

Outline:

# Without Type Casting


# After Type Casting

Without Type Casting


Example:
#include <stdio.h>
main ()
{
int a;
a = 15/6;
printf("%d",a);
}
Program Output:

In the above C program, 15/6 alone will produce integer value as 2.

After Type Casting


#include <stdio.h>
main ()
{
float a;
a = (float) 15/6;
printf("%f",a);
}
Program Output:

After type cast is done before division to retain float value


2.500000.

It's best practice to convert lower data type to higher data type to avoid data loss.

C Variable Scope

A scope is a region of the program, and the scope of variables refers to the area of the
program where the variables can be accessed after its declaration.
In C every variable defined in scope. You can define scope as the section or region of a
program where a variable has its existence; moreover, that variable cannot be used or
accessed beyond that region.
In C programming, variable declared within a function is different from a variable
declared outside of a function. The variable can be declared in three places.

These are:
Position Type

Inside a function or local variables


a block.

Out of all functions. Global variables

In the function Formal parameters


parameters.
So, now let's have a look at each of them individually.

Outline:

# Local Variables
# Local Scope or Block Scope
# Global Variables
# Global Scope
# Global Variable Initialization

Local Variables

Variables that are declared within the function block and can be used only within the
function is called local variables.

Local Scope or Block Scope


A local scope or block is collective program statements put in and declared within a
function or block (a specific region enclosed with curly braces) and variables lying inside
such blocks are termed as local variables. All these locally scoped statements are
written and enclosed within left ({) and right braces (}) curly braces. There's a provision
for nested blocks also in C which means there can be a block or a function within
another block or function. So it can be said that variable(s) that are declared within a
block can be accessed within that specific block and all other inner blocks of that block,
but those variables cannot be accessed outside the block.
Example:
#include <stdio.h>

int main ()
{
/* local variable definition and initialization */ int x,y,z;

/* actual initialization */ x = 20;


y = 30;
z = x + y;

printf ("value of x = %d, y = %d and z = %d\n", x, y, z);

return 0;
}
Global Variables

Variables that are declared outside of a function block and can be accessed inside the
function is called global variables.

Global Scope
Global variables are defined outside a function or any specific block, in most of the
case, on the top of the C program. These variables hold their values all through the end
of the program and are accessible within any of the functions defined in your program.
Any function can access variables defined within the global scope, i.e., its availability
stays for the entire program after being declared.
Example:
#include <stdio.h>

/* global variable definition */int z;

int main ()
{
/* local variable definition and initialization */ int x,y;

/* actual initialization */ x = 20;


y = 30;
z = x + y;

printf ("value of x = %d, y = %d and z = %d\n", x, y, z);

return 0;
}

Global Variable Initialization


After defining a local variable, the system or the compiler won't be initializing any value
to it. You have to initialize it by yourself. It is considered good programming practice to
initialize variables before using. Whereas in contrast, global variables get initialized
automatically by the compiler as and when defined. Here's how based on datatype;
global variables are defined.
datatype Initial Default Value

int 0
char '\0'

float 0

double 0

pointer NULL

You might also like