--> Sayadasite: INTRODUCTION TO C PROGRAMMING

Multiple Ads

Search

Menu Bar

INTRODUCTION TO C PROGRAMMING

OVER VIEW AND HISTORY OF C

Languages are a means of communication. Normally people interact with each other through a language.

C language is still very relevant and widely used although there are many other new languages like C++, Java, Python, C#, etc. in the market.

C language has evolved from three different structured languages ALGOL, BCPL, and B Language. It uses many concepts from these languages while introduced many new concepts such as data types, structure, pointer, etc.

BCPL was developed by Martin Richards, based on which the B language was created by Ken Thompson. And then the B language was the language using which the C language was created.

In 1989, the language was formalized by American National Standard Institute (ANSI).

In 1990, a version of the C language was approved by the International Standard Organisation (ISO), and that version of C is also referred to as C89.

C is a high level language. It is both general purpose and specific purpose programming language. Now a days, C has become a common programming language for every application developer. It was developed at Bell & T Laboratory, USA in 1972. It is the outcome of the efforts of Dennis Ritchie and Brian Kernighan.

CHARACTERISTICS/ FEATURES OF C

C has become popular programming language because of its many features. The important characteristics of C are:

1.               C is a general purpose programming language

2.               C is a structured programming language

3.               Helps in development of System Software

4.               It has rich set of operators

5.               It provides compact representation for expressions

6.               It allows manipulation of internal processor registers

7.               No rigid format. Any number of statements can be typed in a single line.

8.               Portability: any C program can be run on different machines with little or not modification.

9.               Supports a rich set of data types

10.     Very less number of reserved words

11.     Pointer arithmetic and pointer manipulation

12.     Ability to extend itself by adding functions to its library

BASIC STRUCTURE OF A C PROGRAM :

Every programming languages have their own format of coding. The complete structure of C program as shown below.

preprocessor statements

global declarations;

void main( )

{

declaration;

statements; (eg:

/* comments */

}

user defined function

1 Preprocessor Statements :

These statements begin with # symbol and also called preprocessor directives. These statements direct the C preprocessor to include header files and also symbolic constants into a C program.

Some of the preprocessor statements are :

Example

# include <stdio.h>

#include <math.h>

# include <conion.h> etc.,

2 Global Declaration :

Variables or functions whose existence is known in the main( ) function and other user defined functions, are called the global variables and their declarations are called the global declarations. This declaration should be made before main( ) function.

Example

 int x, y = 10; // declaration and initialization of global variables here

3 main( ) function :

This is the main function of every C program. Execution of C program starts from main(). No C program is executed without main() function. It should be written in lowercase letters and should not be terminated by a semicolon. It calls other library functions and user defined functions.

4 Braces :

Every C program uses a p[air of curly braces i.e., { and }. The left brace indicates the beginning of main( ) function. On the other hand, the right indicates the end of the main( ) function. The braces can also be used to indicate the beginning and end of user-defined functions and compound statements.

Example

{

}

5 Declaration : (Variable declaration)

It is a part of the C program where all the variables, arrays, functions etc., used in the C program are declared and may be initialized with their basic data types.

Example

 a=10,b=20,sum;

6 Statements :

These are instructions to the computer to perform specific operations. They may be input- output statements, arithmetic statements, control statements and other statements. input- output statements

Example

scanf()

printf()

getchar()

putchar()

gets()

puts()

arithmetic statements

Example

sum=a+b;

average=sum/2;

control statements

If()

Switch()

7 Comments :

Comments are explanatory note on some instructions. Comment statements are not compiled and executed.

The first kind is used when multi-line comments are

Example

/* My first C program*/

Where the comment statement starts with /* and ends with */. Any thing between those marks is a comment and will be ignored by the compiler.

The second type of comments is a single line comment. It starts with // and stops at the end of the line.

Example

// My first C program

8 User-defined functions :

These are subprograms. Generally, a subprogram is a function. The user defined functions contain a set of statements to perform a specific task. These are written by the user, hence the name user-defined functions. They may be written before or after the main( ) function.

Let’s consider the below-given example of an addition program and understand the structure of a function.

#include <stdio.h>

void sum();  //function declaration

void main()

{

sum();   //function call

}

void sum()   //function definition

{

int a=5, b=10,c;

c=a+b;

printf("addition of %d and %d is: %d",a,b,c);

}

Output:

Addition of 5 and 10 is 15

A Simple C Program is given below

Write a C program to find out the total and average of 4 subject marks

#include<stdio.h>

#include<conio.h>

void main()

{

clrscr();

int kan,eng,his,eco,sum,avg;

printf(“Enter 4 subject marks”);

scanf(“%d%d%d%d”,&kan,&eng,&his,&eco);

sum=kan+eng+his+eco;

ave=sum/4;

printf(“The sum of 4 subject marks = %d”,sum);

printf(“The average of 4 subject marks =%d,avg);

getch();

}

Output

The sum of 4 subject marks =335 (70,80,90,95)

The average of 4 subject marks =83%

Creating and Executing A C Program

Starting C Program Editor

1 Click Start menu & Select Run option

2 Type command & Press Enter key

3 Press Alt+Enter key to view DOS in Full Screen

4 Type cd\tc\bin & Press Enter Key

5 Type tc & Press Enter Key

Compiling a C program means translating it into machine language. ‘C’ compilers are used for this purpose. The process of generating outputs of the program on the screen is called Execution. Press Ctrl + F9 to compile the program. Press Alt + F5 to run the program.

Compilation process in C

The compilation process in C involves four steps: pre-processing, compiling, assembling, and linking them, we run the obtained executable file to get an output on the screen.

The compilation process in C is converting an understandable human code into a Machine understandable code and checking the syntax and semantics of the code to determine any syntax errors or warnings present in our C program.

Source code (Written in C language)

Completion

Machine code(1001100)

 



 

No comments: