You are on page 1of 14

Que:1- Describe various features of the C Programming language.

Ans:

Features of C Language
C is the widely used language. It provides many features that are given below.

1. Simple
2. Machine Independent or Portable
3. Mid-level programming language
4. structured programming language
5. Rich Library
6. Memory Management
7. Fast Speed
8. Pointers
9. Recursion
10. Extensible

1) Simple
C is a simple language in the sense that it provides a structured approach (to break the problem
into parts), the rich set of library functions, data types, etc.

2) Machine Independent or Portable


Unlike assembly language, c programs can be executed on different machines with some
machine specific changes. Therefore, C is a machine independent language.

3) Mid-level programming language


Although, C is intended to do low-level programming. It is used to develop system
applications such as kernel, driver, etc. It also supports the features of a high-level language.
That is why it is known as mid-level language.

4) Structured programming language


C is a structured programming language in the sense that we can break the program into parts
using functions. So, it is easy to understand and modify. Functions also provide code reusability.

5) Rich Library
C provides a lot of inbuilt functions that make the development fast.

6) Memory Management
It supports the feature of dynamic memory allocation. In C language, we can free the allocated
memory at any time by calling the free() function.

7) Speed
The compilation and execution time of C language is fast since there are lesser inbuilt functions
and hence the lesser overhead.

8) Pointer
C provides the feature of pointers. We can directly interact with the memory by using the
pointers. We can use pointers for memory, structures, functions, array, etc.

9) Recursion
In C, we can call the function within the function. It provides code reusability for every
function. Recursion enables us to use the approach of backtracking.
10) Extensible
C language is extensible because it can easily adopt new features.

Que:2- explain various branching statements in C examples.

Ans:-

Branching Statements-

Overview
A branch is an instruction in a computer program that can cause a computer
to begin executing a different instruction sequence and thus deviate from its
default behavior of executing instructions in order.[1] Common branching
statements include break, continue, return, and goto.

Discussion
Branching statements allow the flow of execution to jump to a different part of
the program. The common branching statements used within other control
structures include: break, continue, return, and goto. The goto is
rarely used in modular structured programming. Additionally, we will add to
our list of branching items a pre-defined function commonly used in
programming languages of: exit.

Examples

break

The break is used in one of two ways; with a switch to make it act like a case
structure or as part of a looping process to break out of the loop. The following
gives the appearance that the loop will execute 8 times, but the break
statement causes it to stop during the fifth iteration.

counter = 0;
While counter < 8
Output counter
If counter == 4
break
counter += 1
continue

The following gives the appearance that the loop will print to the monitor 8
times, but the continue statement causes it not to print number 4.

For counter = 0, counter < 8, counter += 1


If counter == 4
continue
Output counter
return

The return statement exits a function and returns to the statement where the
function was called.

Function DoSometing
statements
Return <optional return value>
goto

The goto structure is typically not accepted in good structured programming.


However, some programming languages allow you to create a label with an
identifier name followed by a colon. You use the command
word goto followed by the label.

some lines of code;


goto label; // jumps to the label
some lines of code;
some lines of code;
some lines of code;
label: some statement; // Declared label
some lines of code;
exit

Although exit is technically a pre-defined function, it is covered here because


of its common usage in programming. A good example is the opening a file
and then testing to see if the file was actually opened. If not, we have an error
that usually indicates that we want to prematurely stop the execution of the
program. The exit function terminates the running of the program and in the
process returns an integer value back to the operating system. It fits the
definition of branching which is to jump to some other place in the program.

Key Terms
branching statements
Allow the flow of execution to jump to a different part of the program.
break
A branching statement that terminates the existing structure.
continue
A branching statement that causes a loop to stop its current iteration
and begin the next one.
exit
A predefined function used to prematurely stop a program and return to
the operating system.
goto
An unstructured branching statement that causes the logic to jump to a
different place in the program.
return
A branching statement that causes a function to jump back to the
function that called it.
Que:3- Define a function. List and explain the categories of user-defined
functions.
Ans:-
A function is a group of statements that together perform a task. Every C program has
at least one function, which is main(), and all the most trivial programs can define
additional functions.
You can divide up your code into separate functions. How you divide up your code
among different functions is up to you, but logically the division is such that each
function performs a specific task.
A function declaration tells the compiler about a function's name, return type, and
parameters. A function definition provides the actual body of the function.
The C standard library provides numerous built-in functions that your program can call.
For example, strcat() to concatenate two strings, memcpy() to copy one memory
location to another location, and many more functions.
A function can also be referred as a method or a sub-routine or a procedure, etc.

Defining a Function
The general form of a function definition in C programming language is as follows −
return_type function_name( parameter list ) {
body of the function
}
A function definition in C programming consists of a function header and a function
body. Here are all the parts of a function −
 Return Type − A function may return a value. The return_type is the data type
of the value the function returns. Some functions perform the desired operations
without returning a value. In this case, the return_type is the keyword void.
 Function Name − This is the actual name of the function. The function name and
the parameter list together constitute the function signature.
 Parameters − A parameter is like a placeholder. When a function is invoked, you
pass a value to the parameter. This value is referred to as actual parameter or
argument. The parameter list refers to the type, order, and number of the
parameters of a function. Parameters are optional; that is, a function may contain
no parameters.
 Function Body − The function body contains a collection of statements that
define what the function does.

Example
Given below is the source code for a function called max(). This function takes two
parameters num1 and num2 and returns the maximum value between the two −
/* function returning the max between two numbers */
int max(int num1, int num2) {

/* local variable declaration */


int result;

if (num1 > num2)


result = num1;
else
result = num2;

return result;
}

Types of User-defined Functions in C 


1. Function with no arguments and no return value
2. Function with no arguments and a return value
3. Function with arguments and no return value
4. Function with arguments and with return value

1. Function with No Arguments and No Return Value

Functions that have no arguments and no return values. Such functions can
either be used to display information or to perform any task on global variables. 
Syntax:
// void return type with no arguments
void function_name()
{
// no return value
return;
}

 C

// C program to use function with

// no argument and no return values


#include <stdio.h>

void sum()

  int x, y;

  printf("Enter x and y\n");

  scanf("%d %d", &x, &y);

  printf("Sum of %d and %d is: %d", x, y, x + y);

// Driver code

int main()

  // function call

  sum();

  return 0;

Output:
Enter x and y
2 3
Sum of 2 and 3 is: 5
In the above program, function sum does not take any arguments and has no
return values. It takes x and y as inputs from the user and prints them inside the
void function.

2. Function with No Arguments and With Return Value

Functions that have no arguments but have some return values. Such functions
are used to perform specific operations and return their value.
Syntax:
return_type function_name()
{
// program
return value;
}

 C

// C program to use function with

// no argument and with return values

#include <stdio.h>

int sum()

  int x, y, s = 0;

  printf("Enter x and y\n");


  scanf("%d %d", &x, &y);

  s = x + y;

  return s;

// Driver code

int main()

  // function call

  printf("Sum of x and y is %d",

          sum());

  return 0;

Output:
Enter x and y
3 2
Sum of x and y is 5
In the above program, function sum does not take any arguments and has a
return value as an integer type. It takes x and y as inputs from the user and
returns them.

3. Function With Arguments and No Return Value

Functions that have arguments but no return values. Such functions are used to
display or perform some operations on given arguments.
Syntax:
void function_name(type1 argument1, type2 argument2,...typeN
argumentN)
{
// Program
return;
}

 C

// C program to use function with

// argument and no return values

#include <stdio.h>

void sum(int x, int y)

  printf("Sum of %d and %d is: %d",

          x, y, x + y);

// Driver code

int main()

  int x, y;
  printf("Enter x and y\n");

  scanf("%d %d", &x, &y);

  // function call

  sum(x, y);

  return 0;

Output:
Enter x and y
3 2
Sum of 3 and 2 is: 5
In the above program, function sum does take x and y as arguments and has no
return value. The main function takes x and y as inputs from the user and calls
the sum function to perform the print operation on the given arguments.

4. Function With Arguments and With Return Value

Functions that have arguments and some return value. These functions are
used to perform specific operations on the given arguments and return their
values to the user.
Syntax:
return_type function_name(type1 argument1, type2 argument2,...typeN
argumentN)
{
// program
return value;
}

 C

// C program to use function with

// argument and with return values

#include <stdio.h>

int sum(int x, int y)

  return x + y;

// Driver code

int main()

  int x, y;

  printf("Enter x and y\n");

  scanf("%d %d", &x, &y);

  // function call

  printf("Sum of %d and %d is: %d",


          x, y, sum(x, y));

  return 0;

Output:
Enter x and y
3 2
Sum of 3 and 2 is: 5
In the above program, function sum takes two arguments as x and y, and has a
return value as an integer type. The main function takes input x and y from the
user and calls the sum function to perform a specific operation on the given
arguments and returns the value.

You might also like