0% found this document useful (0 votes)
53 views36 pages

Features of C P

Uploaded by

Nura Mohamed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views36 pages

Features of C P

Uploaded by

Nura Mohamed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 36

Features of C Programming Language

C is a procedural programming language. It was initially developed by Dennis Ritchie in the year 1972. It
was mainly developed as a system programming language to write an operating system.

The main features of C language include low-level access to memory, a simple set of keywords, and a
clean style, these features make C language suitable for system programming like an operating system
or compiler development.

What are the Most Important Features of C Language?

Here are some of the most important features of the C language:

Procedural Language

Fast and Efficient

Modularity

Statically Type

General-Purpose Language

Rich set of built-in Operators

Libraries with Rich Functions

Middle-Level Language

Portability

Easy to Extend

Let discuss these features one by one:

1. Procedural Language

In a procedural language like C step by step, predefined instructions are carried out. C program may
contain more than one function to perform a particular task. New people to programming will think that
this is the only way a particular programming language works. There are other programming paradigms
as well in the programming world. Most of the commonly used paradigm is an object-oriented
programming language.

2. Fast and Efficient

Newer languages like Java, python offer more features than c programming language but due to
additional processing in these languages, their performance rate gets down effectively. C programming
language as the middle-level language provides programmers access to direct manipulation with the
computer hardware but higher-level languages do not allow this. That’s one of the reasons C language is
considered the first choice to start learning programming languages. It’s fast because statically typed
languages are faster than dynamically typed languages.

3. Modularity

The concept of storing C programming language code in the form of libraries for further future uses is
known as modularity. This programming language can do very little on its own most of its power is held
by its libraries. C language has its own library to solve common problems.

4. Statically Type

C programming language is a statically typed language. Meaning the type of variable is checked at the
time of compilation but not at run time. This means each time a programmer types a program they have
to mention the type of variables used.

5. General-Purpose Language

From system programming to photo editing software, the C programming language is used in various
applications. Some of the common applications where it’s used are as follows:

Operating systems: Windows, Linux, iOS, Android, OXS

Databases: PostgreSQL, Oracle, MySQL, MS SQL Server, etc.

6. Rich set of built-in Operators

It is a diversified language with a rich set of built-in operators which are used in writing complex or
simplified C programs.
7. Libraries with Rich Functions

Robust libraries and functions in C help even a beginner coder to code with ease.

beginner coder to code with ease.

8. Middle-Level Language

As it is a middle-level language so it has the combined form of both capabilities of assembly language
and features of the high-level language.

9. Portability

C language is lavishly portable as programs that are written in C language can run and compile on any
system with either no or small changes.

10. Easy to Extend

Programs written in C language can be extended means when a program is already written in it then
some more features and operations can be added to it.

Features of C++

C++ is a general-purpose programming language that was developed as an enhancement of the C


language to include an object-oriented paradigm. It is an imperative and compiled language. C++ has a
number of features, including:

Object-Oriented Programming

Machine Independent

Simple
High-Level Language

Popular

Case-sensitive

Compiler Based

Dynamic Memory Allocation

Memory Management

Multi-threading

features of C++

1. Object-Oriented Programming

C++ is an Object-Oriented Programming Language, unlike C which is a procedural programming


language. This is the most important feature of C++. It can create/destroy objects while programming.
Also, It can create blueprints with which objects can be created. We have discussed the Object-Orient
Programming Concepts in C++ in this article.

Concepts of Object-oriented programming Language:

Class

Objects

Encapsulation

Polymorphism

Inheritance

Abstraction

2. Machine Independent

A C++ executable is not platform-independent (compiled programs on Linux won’t run on Windows),
however, they are machine-independent. Let us understand this feature of C++ with the help of an
example. Suppose you have written a piece of code that can run on Linux/Windows/Mac OSx which
makes the C++ Machine Independent but the executable file of the C++ cannot run on different
operating systems.

3. Simple

It is a simple language in the sense that programs can be broken down into logical units and parts, has
rich library support and has a variety of data types. Also, the Auto Keyword of C++ makes life easier.

Auto Keyword

The idea of the auto keyword was to form the C++ compiler to deduce the data type while compiling
instead of making you declare the data type every freaking time. Do keep in mind that you cannot
declare something without an initializer. There must be some way for the compiler to deduce your type.

Example:

// C++ program to demonstrate

// working of auto keyword

#include <bits/stdc++.h>

using namespace std;

// Driver Code

int main()

{
// Variables

auto an_int = 26;

auto a_bool = false;

auto a_float = 26.24;

auto ptr = &a_float;

// Print typeid

cout << typeid(a_bool).name() << "\n";

cout << typeid(an_int).name() << "\n";

return 0;

Output:

b
i

4. High-Level Language

C++ is a High-Level Language, unlike C which is a Mid-Level Programming Language. It makes life easier
to work in C++ as it is a high-level language it is closely associated with the human-comprehensible
English language.

5. Popular

C++ can be the base language for many other programming languages that supports the feature of
object-oriented programming. Bjarne Stroustrup found Simula 67, the first object-oriented language
ever, lacking simulations, and decided to develop C++.

6. Case-sensitive

It is clear that C++ is a case-sensitive programming language. For example, cin is used to take input from
the input stream. But if the “Cin” won’t work. Other languages like HTML and MySQL are not case-
sensitive languages.

7. Compiler Based

C++ is a compiler-based language, unlike Python. That is C++ programs used to be compiled and their
executable file is used to run them. C++ is a relatively faster language than Java and Python.

8. Dynamic Memory Allocation

When the program executes in C++ then the variables are allocated the dynamical heap space. Inside
the functions, the variables are allocated in the stack space. Many times, We are not aware in advance
how much memory is needed to store particular information in a defined variable and the size of
required memory can be determined at run time.

9. Memory Management

C++ allows us to allocate the memory of a variable or an array in run time. This is known as Dynamic
Memory Allocation.
In other programming languages such as Java and Python, the compiler automatically manages the
memories allocated to variables. But this is not the case in C++.

In C++, the memory must be de-allocated dynamically allocated memory manually after it is of no use.

The allocation and deallocation of the memory can be done using the new and delete operators
respectively.

Example:

// C++ Program to implement

// the Memory Management

#include <cstring>

#include <iostream>

using namespace std;

// Driver Code

int main()

int num = 5;

float* ptr;
// Memory allocation of

// num number of floats

ptr = new float[num];

for (int i = 0; i < num; ++i) {

*(ptr + i) = i;

cout << "Display the GPA of students:" << endl;

for (int i = 0; i < num; ++i) {

cout << "Student" << i + 1 << ": " << *(ptr + i)


<< endl;

// Ptr memory is released

delete[] ptr;

return 0;

Output:

Display the GPA of students:

Student1: 0

Student2: 1

Student3: 2

Student4: 3

Student5: 4

10. Multi-threading
Multithreading is a specialized form of multitasking and multitasking is a feature that allows your system
to execute two or more programs concurrently. In general, there are two sorts of multitasking: process-
based and thread-based.

Process-based multitasking handles the concurrent execution of programs. Thread-based multitasking


deals with the multiprogramming of pieces of an equivalent program.

A multithreaded program contains two or more parts that will run concurrently. Each part of such a
program is named a thread, and every thread defines a separate path of execution.

C++ doesn’t contain any built-in support for multithreaded applications. Instead, it relies entirely upon
the OS to supply this feature.

Example:

// C++ Program to implement

// the working of Multi-threading

#include <cstdlib>

#include <iostream>

#include <pthread.h>

using namespace std;

#define NUM_THREADS 5
// Function to print Hello with

// the thread id

void* PrintHello(void* threadid)

// Thread ID

long tid;

tid = (long)threadid;

// Print the thread ID

cout << "Hello World! Thread ID, "

<< tid << endl;

pthread_exit(NULL);

}
// Driver Code

int main()

// Create thread

pthread_t threads[NUM_THREADS];

int rc;

int i;

for (i = 0; i < NUM_THREADS; i++) {

cout << "main() : creating thread, "

<< i << endl;


rc = pthread_create(&threads[i],

NULL,

PrintHello,

(void*)&i);

// If thread is not created

if (rc) {

cout << "Error:unable to"

<< " create thread, "

<< rc << endl;


exit(-1);

pthread_exit(NULL);

Output:

This tutorial assumes that you are working on Linux OS and we are going to write a multi-threaded C++
program using POSIX. POSIX Threads or Pthreads provide API which is available on many Unix-like POSIX
systems such as FreeBSD, NetBSD, GNU/Linux, Mac OS X, and Solaris.

What Is the Difference Between C and C++?

Object-oriented programming (OOP): C++ integrates object-oriented programming features, while C


follows a procedural approach. This means C does not include features like classes, encapsulation,
inheritance, or polymorphism.

Memory management: When it comes to C vs C++ memory management, C++ offers a more
sophisticated approach, including constructors and destructors that are automatically called when
objects are created or destroyed. C relies on manual memory management with functions like malloc()
and free().
Containers: The C++ Standard Library includes a richer set of container classes than C, including vectors,
lists, sets, maps, and more.

Stricter type checking: C++ does not permit implicit violations of type safety versus C.

Exception handling: The C++ Standard Library includes support for exceptions, whereas the C Standard
Library does not include this.

Templates: C++ makes extensive use of templates to allow programmers to write generic code and
enhance code reuse, while C does not use these.

I/O Streams: The C++ Standard Library includes I/O stream classes to handle input and output, while the
C Standard Library uses a simple file-based I/O approach.

Performance: Generally, C is faster than C++ due to the overhead from features like virtual function or
exception handling.

Compatibility: C code can be used in C++ programs, but the opposite is not always true because C++
adds additional features and syntax that are not in C.

What Are the Similarities Between C & C++?

Seeing as C++ is an extension of the C programming language, there are a number of key similarities
between the two.

Syntax: When it comes to C vs C++ syntax, these are very similar, with many of the same basic constructs
like variables, operators, loops, and conditionals.

Control structures: C and C++ both support if-else statements, while and do-while loops, and for loops.

Data types: C and C++ share many data types, like int, float, double, and char.

Pointers: Both languages support pointers for direct memory access.

Standard libraries: C and C++ both have standard libraries that provide common functionality, such as
I/O, math functions, and string manipulation.

Preprocessor: Both languages use a preprocessor to handle directives like include, define, and
conditional compilation.

Low-level access: Both C and C++ provide access to system resources like hardware, memory, and I/O.

$cccvvcvv
In c, we can divide a large program into the basic building blocks known as function. The function
contains the set of programming statements enclosed by {}. A function can be called multiple times to
provide reusability and modularity to the C program. In other words, we can say that the collection of
functions creates a program. The function is also known as procedureor subroutinein other
programming languages.

Advantage of functions in C

There are the following advantages of C functions.

By using functions, we can avoid rewriting same logic/code again and again in a program.

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

We can track a large C program easily when it is divided into multiple functions.

Reusability is the main achievement of C functions.

However, Function calling is always a overhead in a C program.

Function Aspects

There are three aspects of a C function.

Function declaration A function must be declared globally in a c program to tell the compiler about the
function name, function parameters, and return type.

Function call Function can be called from anywhere in the program. The parameter list must not differ in
function calling and function declaration. We must pass the same number of functions as it is declared in
the function declaration.

Function definition It contains the actual statements which are to be executed. It is the most important
aspect to which the control comes when the function is called. Here, we must notice that only one value
can be returned from the function.
SN C function aspects Syntax

1 Function declaration return_type function_name (argument list);

2 Function call function_name (argument_list)

3 Function definition return_type function_name (argument list) {function body;}

The syntax of creating function in c language is given below:

return_type function_name(data_type parameter...){

//code to be executed

Types of Functions

There are two types of functions in C programming:

Library Functions: are the functions which are declared in the C header files such as scanf(), printf(),
gets(), puts(), ceil(), floor() etc.

User-defined functions: are the functions which are created by the C programmer, so that he/she can
use it many times. It reduces the complexity of a big program and optimizes the code.

C Function

Return Value

A C function may or may not return a value from the function. If you don't have to return any value from
the function, use void for the return type.

Let's see a simple example of C function that doesn't return any value from the function.

Example without return value:

void hello(){

printf("hello c");
}

If you want to return any value from the function, you need to use any data type such as int, long, char,
etc. The return type depends on the value to be returned from the function.

Let's see a simple example of C function that returns int value from the function.

Example with return value:

int get(){

return 10;

In the above example, we have to return 10 as a value, so the return type is int. If you want to return
floating-point value (e.g., 10.2, 3.1, 54.5, etc), you need to use float as the return type of the method.

float get(){

return 10.2;

Now, you need to call the function, to get the value of the function.

Different aspects of function calling

A function may or may not accept any argument. It may or may not return any value. Based on these
facts, There are four different aspects of function calls.

function without arguments and without return value

function without arguments and with return value

function with arguments and without return value

function with arguments and with return value


Example for Function without argument and return value

Example 1

#include<stdio.h>

void printName();

void main ()

printf("Hello ");

printName();

void printName()

printf("Javatpoint");

Output

Hello Javatpoint

Example 2

#include<stdio.h>

void sum();

void main()

printf("\nGoing to calculate the sum of two numbers:");

sum();
}

void sum()

int a,b;

printf("\nEnter two numbers");

scanf("%d %d",&a,&b);

printf("The sum is %d",a+b);

Output

Going to calculate the sum of two numbers:

Enter two numbers 10

24

The sum is 34

Example for Function without argument and with return value

Example 1

#include<stdio.h>

int sum();

void main()

int result;

printf("\nGoing to calculate the sum of two numbers:");


result = sum();

printf("%d",result);

int sum()

int a,b;

printf("\nEnter two numbers");

scanf("%d %d",&a,&b);

return a+b;

Output

Going to calculate the sum of two numbers:

Enter two numbers 10

24

The sum is 34

Example 2: program to calculate the area of the square

#include<stdio.h>

int sum();

void main()

printf("Going to calculate the area of the square\n");


float area = square();

printf("The area of the square: %f\n",area);

int square()

float side;

printf("Enter the length of the side in meters: ");

scanf("%f",&side);

return side * side;

Output

Going to calculate the area of the square

Enter the length of the side in meters: 10

The area of the square: 100.000000

Example for Function with argument and without return value

Example 1

#include<stdio.h>

void sum(int, int);

void main()

int a,b,result;

printf("\nGoing to calculate the sum of two numbers:");

printf("\nEnter two numbers:");


scanf("%d %d",&a,&b);

sum(a,b);

void sum(int a, int b)

printf("\nThe sum is %d",a+b);

Output

Going to calculate the sum of two numbers:

Enter two numbers 10

24

The sum is 34

Example 2: program to calculate the average of five numbers.

#include<stdio.h>

void average(int, int, int, int, int);

void main()

int a,b,c,d,e;

printf("\nGoing to calculate the average of five numbers:");

printf("\nEnter five numbers:");

scanf("%d %d %d %d %d",&a,&b,&c,&d,&e);
average(a,b,c,d,e);

void average(int a, int b, int c, int d, int e)

float avg;

avg = (a+b+c+d+e)/5;

printf("The average of given five numbers : %f",avg);

Output

Going to calculate the average of five numbers:

Enter five numbers:10

20

30

40

50

The average of given five numbers : 30.000000

Example for Function with argument and with return value

Example 1

#include<stdio.h>

int sum(int, int);

void main()

int a,b,result;
printf("\nGoing to calculate the sum of two numbers:");

printf("\nEnter two numbers:");

scanf("%d %d",&a,&b);

result = sum(a,b);

printf("\nThe sum is : %d",result);

int sum(int a, int b)

return a+b;

Output

Going to calculate the sum of two numbers:

Enter two numbers:10

20

The sum is : 30

Example 2: Program to check whether a number is even or odd

#include<stdio.h>

int even_odd(int);

void main()

int n,flag=0;

printf("\nGoing to check whether a number is even or odd");

printf("\nEnter the number: ");


scanf("%d",&n);

flag = even_odd(n);

if(flag == 0)

printf("\nThe number is odd");

else

printf("\nThe number is even");

int even_odd(int n)

if(n%2 == 0)

return 1;

else

return 0;

Output

Going to check whether a number is even or odd


Enter the number: 100

The number is even

C Library Functions

Library functions are the inbuilt function in C that are grouped and placed at a common place called the
library. Such functions are used to perform some specific operations. For example, printf is a library
function used to print on the console. The library functions are created by the designers of compilers. All
C standard library functions are defined inside the different header files saved with the extension .h. We
need to include these header files in our program to make use of the library functions defined in such
header files. For example, To use the library functions such as printf/scanf we need to include stdio.h in
our program which is a header file that contains all the library functions regarding standard
input/output.

The list of mostly used header files is given in the following table.

SN Header file Description

1 stdio.h This is a standard input/output header file. It contains all the library functions regarding
standard input/output.

2 conio.h This is a console input/output header file.

3 string.h It contains all string related library functions like gets(), puts(),etc.

4 stdlib.h This header file contains all the general library functions like malloc(), calloc(), exit(), etc.

5 math.h This header file contains all the math operations related functions like sqrt(), pow(), etc.

6 time.h This header file contains all the time-related functions.

7 ctype.h This header file contains all character handling functions.

8 stdarg.h Variable argument functions are defined in this header file.

9 signal.h All the signal handling functions are defined in this header file.

10 setjmp.h This file contains all the jump functions.

11 locale.h This file contains locale functions.

12 errno.h This file contains error handling functions.

13 assert.hThis file contains diagnostics functions.


← PrevNext →

Youtube For Videos Join Our Youtube Channel: Join Now

Feedback

Send your Feedback to [email protected]

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials

Splunk tutorial

Splunk

SPSS tutorial

SPSS

Swagger tutorial

Swagger

T-SQL tutorial

Transact-SQL

Tumblr tutorial

Tumblr
React tutorial

ReactJS

Regex tutorial

Regex

Reinforcement learning tutorial

Reinforcement Learning

R Programming tutorial

R Programming

RxJS tutorial

RxJS

React Native tutorial

React Native

Python Design Patterns

Python Design Patterns

Python Pillow tutorial

Python Pillow

Python Turtle tutorial


Python Turtle

Keras tutorial

Keras

Preparation

Aptitude

Aptitude

Logical Reasoning

Reasoning

Verbal Ability

Verbal Ability

Interview Questions

Interview Questions

Company Interview Questions

Company Questions

Trending Technologies

Artificial Intelligence
Artificial Intelligence

AWS Tutorial

AWS

Selenium tutorial

Selenium

Cloud Computing

Cloud Computing

Hadoop tutorial

Hadoop

ReactJS Tutorial

ReactJS

Data Science Tutorial

Data Science

Angular 7 Tutorial

Angular 7

Blockchain Tutorial

Blockchain
Git Tutorial

Git

Machine Learning Tutorial

Machine Learning

DevOps Tutorial

DevOps

B.Tech / MCA

DBMS tutorial

DBMS

Data Structures tutorial

Data Structures

DAA tutorial

DAA

Operating System

Operating System

Computer Network tutorial


Computer Network

Compiler Design tutorial

Compiler Design

Computer Organization and Architecture

Computer Organization

Discrete Mathematics Tutorial

Discrete Mathematics

Ethical Hacking

Ethical Hacking

Computer Graphics Tutorial

Computer Graphics

Software Engineering

Software Engineering

html tutorial

Web Technology

Cyber Security tutorial

Cyber Security
Automata Tutorial

Automata

C Language tutorial

C Programming

C++ tutorial

C++

Java tutorial

Java

.Net Framework tutorial

.Net

Python tutorial

Python

List of Programs

Programs

Control Systems tutorial

Control System
Data Mining Tutorial

Data Mining

Data Warehouse Tutorial

Data Warehouse

You might also like