i is 10 and count is 0 The extern Storage Class The extern storage class is

I is 10 and count is 0 the extern storage class the

This preview shows page 2 - 5 out of 8 pages.

i is 10 and count is 0 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 () { count = 5 ; write_extern (); } Second File: support.c #include <stdio.h> extern int count ;
Image of page 2
void write_extern ( void ) { 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 support . c This will produce  a.out  executable program, when this program is executed, it produces the following result: 5 Functions in C A function is a module or block of program code which deals with a particular task. Making functions is a way of isolating one block of code from other independent blocks of code. Functions serve two purposes. They allow a programmer to say: `this piece of code does a specific job which stands by itself and should not be mixed up with anyting else', Second they make a block of code reusable since a function can be reused in many different contexts without repeating parts of the program text. A function can take a number of parameters, do required processing and then return a value. There may be a function which does not return any value. You already have seen couple of built-in functions like printf(); Similar way you can define your own functions in C language. Consider the following chunk of code int total = 10; printf("Hello World"); total = total + l; To turn it into a function you simply wrap the code in a pair of curly brackets to convert it into a single compound statement and write the name that you want to give it in front of the brackets: Demo() { int total = 10; printf("Hello World"); total = total + l; } curved brackets after the function's name are required. You can pass one or more paramenters to a function as follows: Demo( int par1, int par2) { int total = 10;
Image of page 3
printf("Hello World"); total = total + l; } By default function does not return anything. But you can make a function to return any value as follows: int Demo( int par1, int par2) { int total = 10; printf("Hello World"); total = total + l; return total; } A return keyword is used to return a value and datatype of the returned value is specified before the name of function. In this case function returns
Image of page 4
Image of page 5

You've reached the end of your free preview.

Want to read all 8 pages?

  • Winter '15
  • Sundararajan
  • Recursion, Return statement, Global variable

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture