features and structure of a c program

20
SUB: COMPUTER PROGRAMMING AND UTILIZATION TOPIC: FEATURES AND STRUCTURE OF C- PROGRAM

Upload: vipul-mishra

Post on 11-May-2017

223 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Features and Structure of a C Program

SUB: COMPUTER PROGRAMMING AND UTILIZATION

TOPIC: FEATURES AND STRUCTURE OF C-PROGRAM

Page 2: Features and Structure of a C Program

INTRODUCTION• C is a general-purpose high level language that was originally developed by

Dennis Ritchie for the Unix operating system. It was first implemented on the Digital Equipment Corporation PDP-11 computer in 1972.• The Unix operating system and virtually all Unix applications are written in the C

language. C has now become a widely used professional language for various reasons :• Easy to learn.• Structured language.• It produces efficient programs.• It can handle low-level activities.• It can be compiled on a variety of computers.

Page 3: Features and Structure of a C Program

C HISTORY• Developed between 1969 and 1973 along with Unix.

• Due mostly to Dennis Ritchie.

• Designed for systems programming:• Operating systems.• Utility programs.• Compilers.• Filters.

• Evolved from B, which evolved from BCPL.

Page 4: Features and Structure of a C Program

FACTS ABOUT C• C was invented to write an operating system called UNIX.• C is a successor of B language which was introduced around

1970.• The language was formalized in 1988 by the American

National Standard Institute (ANSI).• By 1973 UNIX OS Was almost totally written in C.• Today C is the most widely used System Programming

Language.• Most of the state of the art software have been implemented

using C.

Page 5: Features and Structure of a C Program

• 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• Data Bases• Language Interpreters• Utilities

Page 6: Features and Structure of a C Program

SOME ADVANTAGES OF C LANGUAGE

• Currently, the most commonly-used language for embedded systems.• It Can Be Called As – “High-level assembly”.• Very portable: compilers exist for virtually every processor.• Easy-to-understand compilation. • Produces efficient code.• Fairly concise.

Page 7: Features and Structure of a C Program

STANDARD C• Standardized in 1989 by ANSI (American National Standards Institute)

known as ANSI C.

• International standard (ISO) in 1990 which was adopted by ANSI and is known as C89.

• As part of the normal evolution process the standard was updated in 1995 (C95) and 1999 (C99).

• C++ and C• C++ extends C to include support for Object Oriented Programming

and other features that facilitate large software development projects.• C is not strictly a subset of C++, but it is possible to write “Clean C”

that conforms to both the C++ and C standards.

Page 8: Features and Structure of a C Program

FEATURES OF C LANGUAGEModularity: Modularity is one of the important characteristics of C. we can split the C program into Number of modules instead of repeating the same logic statements (sequentially). It allows reusability of modules. Middle level language: As a middle level language C combines both the advantages of low level and high level languages. (arrays, pointers etc.). General purpose programming language: C can be used to implement any kind of applications such as math oriented, graphics oriented or business oriented applications.

Page 9: Features and Structure of a C Program

FEATURES CONTINUED

Portability: we can compile or execute C program in any operating System (Unix ,DOS, windows). Powerful programming language: C is very efficient and powerful programming language, it is best used for data structures and designing system software.

Page 10: Features and Structure of a C Program

CHARACTER SET OF C LANGUAGE• C is a case sensitive language.

• C compiler allows to use many no. of characters : Lower case letters : a b c … z Upper case letters : A B C….Z Digits : 0 1 2 3 …9 Other characters : + - * ( ) & % $ # { } [ ]’ “ : ; etc. White space characters : blank, new line, tab space etc.

Page 11: Features and Structure of a C Program

CONVERSION CHARACTERS ( FORMAT SPECIFIERS)

Conversion characters or format specifiers are used to provide the format for the value to be print.. it has a prefix ‘% and followed by a specifier. %d : prints integer %f : prints float and double %c : prints character %s : prints string %o : prints octal value %u : prints unsigned integer %x : prints Hexa decimal value

Page 12: Features and Structure of a C Program

STRUCTURE OF A C PROGRAM #include <stdio.h> void main (void) { printf(“\nHello World\n”); }

Preprocessor directive (header file)

Program statement

#include <stdio.h>#define VALUE 10

int global_var;

void main (void){

/* This is the beginning of the program */

int local_var;local_var = 5;global_var = local_var + VALUE;

printf (“Total sum is: %d\n”, global_var); // Print out the result}

} Preprocessor directive

Global variable declaration Comments

Local variable declarationVariable definition

Page 13: Features and Structure of a C Program

COMMENTS

• Explanations or annotations that are included in a program for documentation and clarification purpose.• Completely ignored by the compiler during compilation and

have no effect on program execution.• Starts with ‘/*’ and ends with ‘*/’• Some compiler support comments starting with ‘//’

Page 14: Features and Structure of a C Program

PREPROCESSOR DIRECTIVES

• The first thing to be checked by the compiler.• Starts with ‘#’.• Tell the compiler about specific options that it needs to be aware of

during compilation.• There are a few compiler directives. But only 2 of them will be discussed

here.• #include <stdio.h>

• Tell the compiler to include the file stdio.h during compilation• Anything in the header file is considered a part of the program

• #define VALUE 10• Tell the compiler to substitute the word VALUE with 10 during compilation

Page 15: Features and Structure of a C Program

PREPROCESSOR DIRECTIVES CONTINUED#define PI 3.141592654main() {

…..perimeter =

2*PI*radius;area = PI*radius*radius;…...

}main() {…..perimeter = 2* 3.141592654

*radius;area = 3.141592654

*radius*radius;…...

}

The result of the compilation is the same for both program.Which one is preferred (less typing)?Which more readable? The one with awful long series of digits the one with a meaningful name? Before compilation, the pre-processor will replace all PI with 3.141592654.

Page 16: Features and Structure of a C Program

BASIC DATA TYPES

• Three examples of basic data types:• int (used to declare numeric program variables of integer

type)• char (used to declare character variable)• double (used to declare floating point variable)

• In addition, there are float, void, short, long, etc.• Declaration: specifies the type of a variable.• Example: int local_var;

• Definition: assigning a value to the declared variable.• Example: local_var = 5;

Page 17: Features and Structure of a C Program

VARIABLES

• A variable is nothing but a name given to a storage area that our programs can manipulate.• A variable can be declared globally or locally.

• A globally declared variable can be accessed from all parts of the program.

• A locally declared variable can only be accessed from inside the function in which the variable is declared.

Page 18: Features and Structure of a C Program

STATEMENTS

• A specification of an action to be taken by the computer as the program executes.• In the previous example, there are 2 lines following variable

declaration and variable definition that terminate with semicolon ‘;’. • global_var = local_var + VALUE;• printf (“Total sum is: %d\n”, global_var);

• Each line is a statement.

Page 19: Features and Structure of a C Program

BASIC FUNCTIONS

• A C program consists of one or more functions that contain a group of statements which perform a specific task.• A C program must at least have one function: the

function main.•We can create our own function or use the functions

that has been created in the library, in which case we have to include the appropriate header file (example: stdio.h).

Page 20: Features and Structure of a C Program

END