Friday, August 9, 2024

4.1. Introduction to C-Language Programming

 

4. C- Programming

4.1. Introduction to C-Language Programming

Structured programming

The programming that follows a top- down approach, on which developers separate the overall program structure into different sub section, is called structured programming.

 

Advantages of structured programming

a)     Reduces complexity and easy to code.

b)     It takes less time to program and easy to debug.

c)     Reuse of modules and flow of control is clear.

 

C language
C Language is a high-level structured programming language which is used to develop system software.

Dennis Ritchie at Bell telephone laboratory developed C-language in early 1970s. 

C is called middle level language because it combines elements of high level language with some features of assembler.

Data types used in C
Data type are means to identify the type data and associated operation of handling it.

Data types used in C language

a) char       b) int   c) float                         d) double         e) void

155.         Why C is called a middle level language?

C is called middle level language because it combines elements of high level language with some features of assembler

 

 

Features of C language

a)     C is a Structured Programming Language. which break down a program in several small modules and makes the programmer easier to manage and debug the code.

b)     C combines elements of a high level language with some features of assembler which helps to manipulates memory address.

c)     C is a case-sensitive programming language. It understands the capital alphabets and small alphabets as different values.

d)     C is many time faster and efficient than BASIC due to the variety of data type and a list of powerful operators, programs written in C language.

 

Limitations of C language

a)     There is no runtime checking. It has poor errors detection system.

b)     It does not support object oriented programming.

c)     C language has only 32 Keywords

d)     There is no strict type checking int data type to float variables.

 

Application of C Language

a) Operating System

b) Language Compilers/Interface

c) Assemblers

d) Text Editors

e) DBMS

f) Utilities etc.

 

 

C Token

C tokens are the basic buildings blocks in C language which are constructed together to write a C program.

Each and every smallest individual unit in a C program are known as C tokens

 

C tokens are of six types.

a) Keywords (eg: int, while),

b) Identifiers (eg: main, total),

c) Constants (eg: 10, 20),

d) Strings (eg: “total”, “hello”),

e) Special symbols (eg: (), {}),

f) Operators (eg: +, /,-,*)

C keywords (reserved word)

Keyword is a set of special words which are already defined for some tasks.

C has only a set of 32 keywords, which have their predefined meaning and cannot be used as a variable name.

E.g. auto, double, int struct, break, else, long, switch, case, enum, register, typedef, char, extern, return, union, continue, for, signed, void, do, if, static, while, default, goto, sizeof, volatile, const, float, short, unsigned

 

C Character set

Character Set is a group of valid characters and symbols supported by a programming language. Alphabets, Digits and Special characters.

 

Identifiers

Identifiers are the names given to the entities such as variables, functions, arrays structures and unions.

E.g. int price; float total; Here, price and total are called identifiers.

 

Rules for naming Identifiers:

i) The Identifier must start with an alphabet or an under score (_).

ii) Identifier can be made from the combination of alphabets, digits and under score.

iii) No any C keyword can be used as an Identifier.

iv) Identifier must be unique and can be used for a single purpose only.

 

Format Specifier

The format specifier is used during input and output operation. It tells the compiler what type of data is stored in a variable during the input and output operation such as taking data from keyboard and display data on the screen. Some examples are %c, %d, %f, etc.

Data Type

Format Specifier

short int

%hd

unsigned short int

%hu

unsigned int

%u

int

%d

long int

%ld

unsigned long int

%lu

char

%c

float

%f

double

%lf

 

C Header Files

stdio.h - Used for standard input and output (I/O) operations. [printf( ), scanf( ), getchar( )]

conio.h -  Contains declaration for console I/O functions. [clrscr( ), exit( )]

math.h - D e c l a r e s m a t h e m a t i c a l functions and macros. [pow(), squr(), cos(), tan(), sin(), log() ]

string.h - Used for manipulating strings. [strlen(), strcpy(), strcmp(), strcat(), strlwr(), strupr(), strrev() ]

Escape Sequence

Escape Sequence is a pair of character that is used with printf() function to display non-printing character or a special character on the screen.

Some Examples of Escape Sequence:

\n - new line

\t - tab

\b - backspace

\o - null character

\? - question mark

\\ - slash

\' - single quote

\” - double quote

 

Format Identifier

We need to include format identifier to tell the data type in the area of format string of printf() function.

Variable Type

Format Identifier

char

%c

int

%d

long int

%ld

float

%f

 

Variable

A variable is a location in the memory of a computer which holds data in a program and its value may change during the execution of program.

 

Constant

Constant are the values that do not change during the execution of a program.

 

String Constant

 String constant is a message to be displayed along with the other values stored in variables. It is enclosed within double quotation (" ").

 

Output Function in C

Output function is used to show the calculated result or output on the screen.

In C language, printf() is one of the output function defined in header file.

 

printf() function

In C Language, printf() function is used to print the valued on the screen.

It is defined in header file. So, the header file must be added to use this function.

 

Input Function in C

 Input function is used to ask data for processing.

In C language, scanf() is one of the input function defined in header file.

 

scanf() Function

scanf() is one of the most important functions of C Program.

This function is also defined in the header file and used to ask value from keyboard.

 

getch() function

getch() function is another input function of C language.

This function is defined in the header file .

This function is used to ask any one character from keyboard.

Arithmetic Calculations in C Program

There are basically four types of arithmetic operations:

 i) Addition

ii) Subtraction

iii) Multiply

iv) Division

 

List of Arithmetic Operators in C

+ (Plus) - Addition

- (Minus) – Subtraction

* (Asterisk) – Multiplication

/ (Slash) – Division

% (Percentage Symbol) – Modulus Division

++ (Plus Plus) – Increment Operator

- - ( M i n u s M i n u s ) – Decrement Operator

 

 

Operator 

Operators are special symbols that are meant for specific tasks or operators.

The different types of C operators are:- Arithmetic operators, Assignment operators, Unary operators,  Relational operators, Logical operators and Conditional operators.

Arithmetic operators are the operators that are used for manipulating arithmetic data and performs arithmetic operation. The arithmetic operators are:-   +,     - ,      * ,    / and   %

The operator that operate on one operand variable or constant is called urinary operator. E.g. ++, - -

 

C Expression

An expression consists of at least one operand with one or more operators. It is a legal combination of symbols that represents a value.

 

Logical Calculation in C

The calculation that is done based on one or more conditions is called logical calculations.

Several relational or comparison operators are used to check the condition which gives True or False as a calculated result.

 

Relational Operators in C

Relational Operator checks the relationship between two operands and returns either 1 (True) or 0 (False).

In C programming, relational operators can be used to take decisions and provide condition in looping statements.

 

List of Relational Operators in C

= = Equal to

> Greater than

< Less than

!= Not equal to

>= Greater than or equal to

<= Less than or equal to

Control structures in C

 

a) Sequence structure (straight line paths)

The statements are executed one after another sequentially from top to bottom without changing the flow of program.

 

b) Selection structure (one or many branches)

The control of the program is transferred from one part of the program to another on the basis of specified condition or without condition.

If statement and switch statement are the examples of selection structure.

 

c) Loop structure (repetition of a set of activities)

Looping is the process of repeating the execution of a statement or a block of statements guided by a condition. A loop is terminated when the given condition is satisfied.

 

Looping statements in C

C provides three kinds of loops: i) while loop ii) do loop iii) for loop

 

 

Give the name of any two header files used in C language

<stdio.h> and <conio.h>

 

Name the header/library file that handles mathematical functions in C Language.

- <math.h>

 

What will be the value of ‘y’ in given C expression, y=64%5?

The value of y will be 4.

 

What will be the value of x in given c expression x=55 % 10?

The value value of x will be 5.

 

How many keywords are in C language?

There are set of 32 keywords in C language.

 

What is an operator in C language?

Operator is a symbol that is used to perform mathematical operations.

 

Write the name of unary operators used in C-language

Increment (++) and decrement (--).

 

Write the name of two selection statement used in C language.

- if and switch statement

 

Write any two looping statements used in C language.

for and while loop

 

1List any two examples of format specifier (Conversion Character) used in C Language.

Any two examples of format specifier (Conversion Character) used in C Language are %d (int) and %f (float)

 

 

 

No comments:

Post a Comment