Computer Hardware & Networking (EC): Chapter 7

                                   C LANGUAGE

                                                        by Shivajirao Jadhav Mumbai 

                                                                                                  Marks: 10 (15)

Introduction:

C is a general-purpose programming language used for wide range of applications from Operating systems like Windows and UNIX to software that is used for creating 3D movies.

C programming is highly efficient. That’s the main reason why it’s very popular despite being more than 40 years old.

Uses of C language:

C programming language is used for developing system applications.  Below are some examples of C being used.

  1. Operating system development

  2. Data base system

  3. Word processors

  4. Spread sheets

  5. Compilers and assemblers

C shows both features of low level language and high level language, so C language is known as middle level language. C is very powerful programming language as most the part of UNIX is written in C. C is closely associated with Unix Operating system.

 

Tokens in C language:

When we write a paragraph, the individual words and punctuation marks are called tokens. In same way C program has got tokens. These tokens are small units. C tokens, Identifiers and Keywords are the basics in a C program. 

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

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

C tokens are of six types. They are,

  1. Keywords               (eg: int, while),

  2. Identifiers               (eg: main, total),

  3. Constants              (eg: 10, 20),

  4. Strings                    (eg: “total”, “hello”),

  5. Special symbols  (eg: (), {}),

  6. Operators              (eg: +, /,-,*)


Keywords in C language:

  1. Keywords are those words whose meaning is already defined by Compiler

  2. Each keyword is meant to perform a specific function in a C program.

  3. Cannot be used as Variable Name

  4. There are 32 Keywords in C

  5. C Keywords are also called as Reserved words .

auto

double

int

struct

break

else

long

switch

case

enum

register

typedef

char

extern

return

union

const

float

short

unsigned

continue

for

signed

void

default

goto

sizeof

volatile

do

if

static

while



C Constants:

Constants have the fixed values that do not change during the execution of a program. A "constant" is a number, character, or character string that can be used as a value in a program.

Types of C constants:

http://generalnote.com/C-Tutorial/Introduction-to-C/Types-of-C-Constants.JPG

Integer and real constants are numeric constants.

Integer Constants: It has sequence of digits. It must have at least one digit e. g. 5, 87, 145 etc.

Real Constants: Number having decimal point e.g. 0.35, 5.6, -78.976 etc.

Single character constants: A single character enclosed in single quote e.g. ‘M’, ‘S’ etc

String constants: It has sequence of character enclosed in double quote e.g. “Ajay”, “Ram” etc.


C variables:

Variables are used to store the value during the execution of a program. The name itself means, the value of variable can be changed hence the name “Variable“. The variables are stored in Main Memory i.e. RAM.

Types of C variables:

  • Integer variable

  • Float variable

  • Character variable

The type of a variable cannot be changed. Suppose we declared an integer type variable so we cannot store character or a decimal number in that variable.

 The rules for constructing C variable:

  1. The 1st letter should be alphabet.

  2. Variables can be combination of alphabets and digits.

  3. are Case Sensitive.

  4. No Spaces allowed between Characters.

  5. Variable name should not make use to the C Reserved Keywords.

  6. Underscore (_) is the only special character allowed.

  7. Variables can be written in both Uppercase and Lowercase or combination of both.

  8. Variables Variable name should not start with a number.

Data types in C:

Data types specify how we enter data into our programs and what type of data we enter. C language has some predefined set of data types to handle various kinds of data that we can use in our program. These data types have different storage capacities.

1. Primary data types:

These are fundamental data types in C namely integer(int), floating point(float), character(char) and void.

2. Derived data types:

Derived data types are nothing but primary data types grouped together like array, structureunion and pointer

 

Primary data types:

Integer type

Integers are used to store whole numbers.

Size and range of Integer type data:

Type

Size(bytes)

Range

int or signed int

2

-32,768 to 32767

unsigned int

2

0 to 65535

short int or signed short int

1

-128 to 127

unsigned short int

1

0 to 255

long int or signed long int

4

-2,147,483,648 to 2,147,483,647

unsigned long int

4

0 to 4,294,967,295


Floating point type:

Floating types are used to store real numbers.

Size and range of floating type data:

Type

Size(bytes)

Range

Float

4

3.4E-38 to 3.4E+38

double

8

1.7E-308 to 1.7E+308

long double

10

3.4E-4932 to 1.1E+4932


Character type

Character types are used to store characters value.

Size and range of character type data:

Type

Size(bytes)

Range

char or signed char

1

-128 to 127

unsigned char

1

0 to 255

 

void type

void type means no value. This is usually used to specify the type of functions which returns nothing. 

 

C operators:

C language supports a rich set of built-in operators. An operator is a symbol that tells the compiler to perform a certain mathematical or logical operation. Operators are used in programs to manipulate data and variables. 

C operators can be classified into following types:

  1. Arithmetic operators

  2. Relational operators

  3. Logical operators

  4. Bitwise operators

  5. Assignment operators

  6. Conditional operators

  7. Special operators

Arithmetic operators:

Operator

Description

+

adds two operands

-

subtract second operands from first

*

multiply two operand

/

divide numerator by denominator

%

remainder of division

++

Increment operator - increases integer value by one

--

Decrement operator - decreases integer value by one

Relational operators:

Operator

Description

==

Check if two operand are equal

!=

Check if two operand are not equal.

>

Check if operand on the left is greater than operand on the right

<

Check operand on the left is smaller than right operand

>=

check left operand is greater than or equal to right operand

<=

Check if operand on left is smaller than or equal to right operand


Decision making in C:

Decision making is about deciding the order of execution of statements based on certain conditions or repeat a group of statements until certain specified conditions are met.

 

Decision making with if statement:

The if statement may be implemented in different forms depending on the complexity of conditions to be tested. The different forms are,

  1. Simple if statement

  2. if....else statement

  3. Nested if....else statement

  4. Using else if statement

 

Simple if statement:

Format:

if(expression)

{

   statement inside;

}

   statement outside;

If the expression returns true, then the statement-inside will be executed, otherwise statement-inside is skipped and only the statement-outside is executed.

 

 

 

 

Example: Program to find greater number

#include <stdio.h>

 

void main( )

{

    int x, y;

    x = 15;

    y = 13;

    if (x > y )

    {

        printf("x is greater than y");

    }

}

 

if...else statement:

  Format:

if(expression)

{

    statement block1;

}

else

{

    statement block2;

}

If the expression is true, the statement-block1 is executed, else statement-block1 is skipped andstatement-block2 is executed.

Example: Program to find greater number 

#include <stdio.h>

 

void main( )

{

    int x, y;

    x = 15;

    y = 18;

    if (x > y )

    {

        printf("x is greater than y");

    }

    else

    {

        printf("y is greater than x");

    }

}

else …if statement:

  Format:

if(expression1)

{

    statement block1;

}

else if(expression2) 

{

    statement block2;

}

else if(expression3) 

{

    statement block3;

}

else 

    default statement;

The expression is tested from the top(of the ladder) downwards. As soon as a true condition is found, the statement associated with it is executed.

   Example: program to find number divisible by 5 and 8

#include <stdio.h>

 

void main( )

{

    int a;

    printf("Enter a number...");

    scanf("%d", &a);

    if(a%5 == 0 && a%8 == 0)

    {

        printf("Divisible by both 5 and 8");

    }  

    else if(a%8 == 0)

    {

        printf("Divisible by 8");

    }

    else if(a%5 == 0)

    {

        printf("Divisible by 5");

    }

    else 

    {

        printf("Divisible by none");

    }

}

 

 

Loops in C:

In any programming language including C, loops are used to execute a set of statements repeatedly until a particular condition is satisfied. When looping condition becomes false then control moves to next statement immediately after the end of loop block.

while loop:

While is entry controlled loop statement. It tells the compiler to repeat the series of statements as long as certain condition is true. As condition becomes false computer stops repeating and goes to next statement.

Format:

variable initialization;

while(condition)

{

    statements;

    variable increment or decrement; }

 

  Example: Program to print first 10 natural numbers

#include<stdio.h>

 

void main( )

{

    int x;

    x = 1;

    while(x <= 10)

    {

        printf("%d\t", x);

        /* below statement means, do x = x+1, increment x by 1*/

        x++;

    }

}

 

do while loop:

These loops are exactly like while loops except that the test is performed at the end of the loops rather than beginning. In this loop computer centers into the body of do while, execute it and then checks the condition. If condition is true it goes back and continues till condition remains true. When condition becomes false it goes to next statement.  

Format:

do

{

    .....

    .....

}

while(condition)

Example: Program to print first 10 multiples of 5.

#include<stdio.h>

 

void main()

{

    int a, i;

    a = 5;

    i = 1;

    do

    {

        printf("%d\t", a*i);

        i++;

    } 

    while(i <= 10);

}

 

Difference between while and do while loop:

While loop

Do while loop

1. Condition is at top

1. Condition is at bottom

2. No semicolon at end of while

2. Semicolon is compulsory at end

3. It is used when condition is important.

3. It is used when process is important.

4. It is entry controlled loop.

4. it is exit controlled loop.

5. Computer executes body if condition is true.

5. Computer executes body if condition is false.

 

for loop:

for loop is used to execute a set of statements repeatedly until a particular condition is satisfied. We can say it is an open ended loop.. General format is,

Format:

for(initialization; condition; increment/decrement)

{

    statement-block;

}

In for loop we have exactly two semicolons, one after initialization and second after the condition. In this loop we can have more than one initialization or increment/decrement, separated using comma operator. But it can have only one condition.

Example: Program to print first 10 natural numbers

#include<stdio.h>


void main( )

{

    int x;

    for(x = 1; x <= 10; x++)

    {

        printf("%d\t", x);

    }

}

C functions:

C functions are basic building blocks in a program. All C programs are written using functions to improve re-usability, understandability and to keep track on them.

What is c function?

A large C program is divided into basic building blocks called C function. C function contains set of instructions enclosed by “{  }” which performs specific operation in a C program. Actually, Collection of these functions creates a C program.

Uses of c functions:

  1. C functions are used to avoid rewriting same logic/code again and again in a program.

  2. There is no limit in calling C functions to make use of same functionality wherever required.

  3. We can call functions any number of times in a program and from any place in a program.

  4. A large C program can easily be tracked when it is divided into functions.

  5. The core concept of C functions are, re-usability, dividing a big task into small pieces to achieve the functionality and to improve understandability of very large C programs.

How to call c functions in a program?

There are two ways that a C function can be called from a program. They are,

  1. Call by value

  2. Call by reference

1. Call by value:

  • In call by value method, the value of the variable is passed to the function as parameter.

  • The value of the actual parameter can not be modified by formal parameter.

  • Different Memory is allocated for both actual and formal parameters. Because, value of actual parameter is copied to formal parameter.

2. Call by reference:

  • In call by reference method, the address of the variable is passed to the function as parameter.

  • The value of the actual parameter can be modified by formal parameter.

  • Same memory is used for both actual and formal parameters since only address is used by both parameters.

 Program to add two integers:

 

#include <stdio.h>

int main()

{

   int num1, num2, sum;

   printf("Enter first number: ");

   scanf("%d", &num1);

   printf("Enter second number: ");

   scanf("%d", &num2);

   sum = num1 + num2;

   printf("Sum of the entered numbers: %d", sum);

   return 0;

}

    Output:

Enter first number: 20

Enter second number: 19

Sum of the entered numbers: 39

 





Questions

Marks wise Questions: 1M(2), 3M(3), 4M(1)

1. Fill in the blanks with given alternatives.

  1. -----is known as middle level language.

  2. The words and punctuation marks in paragraph are called ------------.

  3. ------ are specifications provides with variable or function name.

  4. -------are the symbols used to indicate the operations on the values.

  5. -------allows us to execute a statement multiple time.

  6. -------can store a fixed size sequential collection of elements of the same size.

  7. ------ is a group of statements that together perform task.

  8. ------language was evolved from ALGOL, BPCL and B.B.

  9. --------is not a C token.

  10. --------operator is used for modulo division.

  11. -------operator is used for left shift.

  12. In ---------C loop condition is given at top.

  13. In --------C loop condition is given at bottom.

  14. C language programs are converted into machine language with the help of ------.

  15. Size of --------integer type data is smallest in C.

  16. C language has --------types of token.

Answers-: 1. C 2. Tokens 3. Data types 4. Operators 5. Loop statement 6. Arrays

7. A function 8. C 9. Integer 10. % 11. << 12. while 13. do while

14. Compiler 15. Short/signed short 16. 6

 

2. Answer the following.

  1. State uses of C language.

  2. What is token? List types of tokens.

  3. List the types of constants and variables in C.

  4. What are variables? State the rules for constructing/naming variables.

  5. What is difference between constant and variable?

  6. List basic data types in C.

  7. List down keywords in C.

  8. Give the list of arithmetical and relational operators in C.

  9. What is use of a loop statement? State types of loops.

  10. Write the format of -(i) if statement (ii) if----else statement (iii) else---if statement        (iv) while loop (v) do while loop (vi) for loop

  11. Distinguish between while and do while loop.

  12. Write how to call C functions in program.

  13. Write a program in C to add two integers.

डॉ. आर व्ही शेजवळ ,  प्राचार्य , लाल बहादूर शास्त्री महाविद्यालय , सातारा  सहसचिव (प्रशासन ) श्री स्वामी विवेकानंद शिक्षण संस्था , कोल्हापूर...