You are on page 1of 4

C Programming Tutorial

The extern Storage Class


The extern storage class is used to give a reference of a global variable that is visible to
ALL the program files. When you use 'extern', the variable cannot be initialized as all it
does is point the variable name at a storage location that has been previously defined.

When you have multiple files and you define a global variable or function, which will be
used in other files also, then extern will be used in another file to give reference of defined
variable or function. Just for understanding, extern is used to declare a global variable or
function in another file.

The extern modifier is most commonly used when there are two or more files sharing the
same global variables or functions as explained below.

First File: main.c


#include <stdio.h>

int count ;
extern void write_extern();

main()
{
write_extern();
}

Second File: write.c


#include <stdio.h>

extern int count;

void write_extern(void)
{
count = 5;
printf("count is %d\n", count);
}

Here, extern keyword is being used to declare count in the second file where as it has its
definition in the first file, main.c. Now, compile these two files as follows:

$gcc main.c write.c

This will produce a.out executable program, when this program is executed, it produces
the following result:

TUTORIALS POINT
Simply Easy Learning Page 24
CHAPTER

10
Decision Making in C

D ecision making structures require that the programmer specify one or more

conditions to be evaluated or tested by the program, along with a statement or statements


to be executed if the condition is determined to be true, and optionally, other statements to
be executed if the condition is determined to be false.

Following is the general form of a typical decision making structure found in most of the
programming languages:

C programming language assumes any non-zero and non-null values as true, and if it is
either zero or null, then it is assumed as false value. C programming language provides
following types of decision making statements.

TUTORIALS POINT
Simply Easy Learning Page 35
while loop in C
A while loop statement in C programming language repeatedly executes a target
statement as long as a given condition is true.

Syntax
The syntax of a while loop in C programming language is:

while(condition)
{
statement(s);
}

Here, statement(s) may be a single statement or a block of statements.


The condition may be any expression, and true is any nonzero value. The loop iterates
while the condition is true.

When the condition becomes false, program control passes to the line immediately
following the loop.

Flow Diagram

TUTORIALS POINT
Simply Easy Learning Page 46

You might also like