DEV Community

Cover image for C Programming

C Programming

bhargavanandanavanam profile image Bhargava-Nandanavanam ・7 min read

C (Programming Language)
C is a general-purpose, procedural computer programming language supporting structured programming, lexical variable scope, and recursion, with a static type system. By design, C provides constructs that map efficiently to typical machine instructions. It has found lasting use in applications previously coded in assembly language. Such applications include operating systems and various application software for computer architectures that range from supercomputers to PLCs and embedded systems.

Data Types:
In the C programming language, data types constitute the semantics and characteristics of storage of data elements. They are expressed in the language syntax in form of declarations for memory locations or variables. Data types also determine the types of operations or methods of processing of data elements.
The C language provides basic arithmetic types, such as integer and real number types, and syntax to build array and compound types. Headers for the C standard library, to be used via include directives, contain definitions of support types, that have additional properties, such as providing storage with an exact size, independent of the language implementation on specific hardware platforms.
Alt Text

Fundamental Data Types:
A fundamental or primitive type is a data type where the values that it can represent have a very simple nature (a number, a character or a truth-value); the primitive types are the most basic building blocks for any programming language and are the base for more complex data types.There are five types of fundamental data types

  1. Integer
  2. Floating
  3. Character
  4. Void
  5. Boolean

Integers are used to store whole numbers.
Alt Text

Floating types are used to store real numbers.
Alt Text

Character types are used to store characters value.
Alt Text

void type means no value. This is usually used to specify the type of functions which returns nothing. We will get acquainted to this datatype as we start learning more advanced topics in C language, like functions, pointers etc.

In C, Boolean is a data type that contains two types of values, i.e., 0 and 1. Basically, the bool type value represents two types of behavior, either true or false. Here, '0' represents false value, while '1' represents true value.

Syntax :
Alt Text

Derived Data Types:
The functions that we create in a program are known as user defined functions or in other words you can say that a function created by user is known as user defined function. These are five types:

  1. Functions
  2. Arrays
  3. Pointers
  4. Unions
  5. Structures Unions and Structures are classified as User Defined Data Types.

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.
We 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.
C functions can be classified into two categories,

  1. Library functions
  2. User-defined functions Alt Text Library functions are those functions which are already defined in C library, example printf(), scanf(), strcat() etc. You just need to include appropriate header files to use these functions. These are already declared and defined in C libraries. User-defined functions on the other hand, are those functions which are defined by the user at the time of writing program. These functions are made for code reusability and for saving time and space.

Defining a Function:
The general form of a function definition in C programming language is as follows −
Alt Text
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.
Alt Text

Function Arguments:
If a function is to use arguments, it must declare variables that accept the values of the arguments. These variables are called the formal parameters of the function.
Formal parameters behave like other local variables inside the function and are created upon entry into the function and destroyed upon exit.
While calling a function, there are two ways in which arguments can be passed to a function

  1. Call by Value
  2. Call by Reference

Call by Value:
The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument.
By default, C programming uses call by value to pass arguments. In general, it means the code within a function cannot alter the arguments used to call the function.
Alt Text

Call by Reference:
The call by reference method of passing arguments to a function copies the address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. It means the changes made to the parameter affect the passed argument.
To pass a value by reference, argument pointers are passed to the functions just like any other value.
Alt Text

Arrays are referred to as structured data types. An array is defined as finite ordered collection of homogenous data, stored in contiguous memory locations.
Here the words,
•finite means data range must be defined.
•ordered means data must be stored in continuous memory addresses.
•homogenous means data must be of similar data type.

Declaring Arrays:
To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows
Alt TextAlt Text
Here int is the data type, arr is the name of the array and 10 is the size of array. It means array arr can only contain 10 elements of int type.
Index of an array starts from 0 to size-1 i.e first element of arr array will be stored at arr[0] address and the last element will occupy arr[9].
Alt Text

Two dimensional Arrays:
C language supports multidimensional arrays also. The simplest form of a multidimensional array is the two-dimensional array. Both the row's and column's index begins from 0.
Two-dimensional arrays are declared as follows,
Alt TextAlt Text
Alt Text

A Pointer in C language is a variable which holds the address of another variable of same data type.

Pointers are used to access memory and manipulate the address.

Pointers are one of the most distinct and exciting features of C language. It provides power and flexibility to the language. Although pointers may appear a little confusing and complicated in the beginning, but trust me, once you understand the concept, you will be able to do so much more with C language.

Address in C:
Whenever a variable is defined in C language, a memory location is assigned for it, in which its value will be stored. We can easily check this memory address, using the & symbol.

If var is the name of the variable, then &var will give it's address.

Alt Text

User-Defined Data Types:
A user-defined data type (UDT) is a data type that derived from an existing data type. You can use UDTs to extend the built-in types already available and create your own customized data types.There are two types if UDT’s in C Language:

It is a collection of variables of different datatypes in the same memory location.We can define a union with many members, but at a given point of time only one member can contain a value.Unions can be very handy when you need to talk to peripherals through some memory mapped registers.

Syntax for Declaring a C union:
Alt Text

Note : Size of the union is the the size of its largest field because sufficient number of bytes must be reservedto store the largest sized field.

To access the fields of a union, use dot(.) operator i.e., the variable name followed by dot operator followed byfield name.
Alt Text

Structure is a user-defined datatype in C language which allows us to combine data of different types together. Structure helps to construct a complex data type which is more meaningful. It is somewhat similar to an Array, but an array holds data of similar type only. But structure on the other hand, can store data of any type, which is practical more useful.

Defining a structure:

struct keyword is used to define a structure. struct defines a new data type which is a collection of primary and derived datatypes.

As you can see in the syntax above, we start with the struct keyword, then it's optional to provide your structure a name, we suggest you to give it a name, then inside the curly braces, we have to mention all the member variables, which are nothing but normal C language variables of different types like int, float, array etc.
Alt Text
After the closing curly brace, we can specify one or more structure variables, again this is optional.

Note: The closing curly brace in the structure type declaration must be followed by a semicolon(;).

Alt Text

Difference between Unions and Structures:

Alt Text


Editor guide