Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Why To Use C?: Tutorials Point

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

 Today, C is the most widely used and popular System Programming Language.

 Most of the state-of-the-art softwares have been implemented using C.

 Today's most ][popular Linux OS and RBDMS MySQL have been written in C.

Why to use C?
C was initially used for system development work, in particular the programs that make up
the operating system. C was adopted as a system development language because it
produces code that runs nearly as fast as code written in assembly language. Some
examples of the use of C might be:

 Operating Systems

 Language Compilers

 Assemblers

 Text Editors

 Print Spoolers

 Network Drivers

 Modern Programs

 Databases

 Language Interpreters

 Utilities

C Programs
A C program can vary from 3 lines to millions of lines and it should be written into one or
more text files with extension ".c"; for example, hello.c. You can use "vi", "vim" or any
other text editor to write your C program into a file.

This tutorial assumes that you know how to edit a text file and how to write source code
using any programming language.

TUTORIALS POINT
Simply Easy Learning Page 2
6
CHAPTER

C Variables

A variable is nothing but a name given to a storage area that our programs can

manipulate. Each variable in C has a specific type, which determines the size and layout of
the variable's memory; the range of values that can be stored within that memory; and the
set of operations that can be applied to the variable.

The name of a variable can be composed of letters, digits, and the underscore character. It
must begin with either a letter or an underscore. Upper and lowercase letters are distinct
because C is case-sensitive. Based on the basic types explained in previous chapter, there
will be the following basic variable types:

Type Description

Char Typically a single octet(one byte). This is an integer type.

Int The most natural size of integer for the machine.

Float A single-precision floating point value.

Double A double-precision floating point value.

Void Represents the absence of type.

C programming language also allows to define various other types of variables, which we
will cover in subsequent chapters like Enumeration, Pointer, Array, Structure, Union, etc.
For this chapter, let us study only basic variable types.

Variable Definition in C:
A variable definition means to tell the compiler where and how much to create the storage for the
variable. A variable definition specifies a data type and contains a list of one or more variables of
that type as follows:

type variable_list;

Here, type must be a valid C data type including char, w_char, int, float, double, bool or any user-
defined object, etc., and variable_list may consist of one or more identifier names separated by
commas. Some valid declarations are shown here:

TUTORIALS POINT
Simply Easy Learning Page 13
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

You might also like