History of C-programming

C-programming is a procedural programming language originally developed by Dennis Ritchie in the time 1972 at Bell Laboratories of AT&T Labs. It was substantially developed as a system programming language to write the UNIX operating system.

C Programming
C Programming

Structure Of C Program

The structure of a C program means the specific structure to start the programming in the C language. Without a proper structure, it becomes delicate to dissect the problem and the result. It also gives us a reference to write more complex programs.

Debugging Techniques

Debugging is the process of finding and fixing crimes, or” bugs,” in computer law. Then are some debugging ways that can be helpful when programming in C

  1. Print Statements
  2. Debugger
  3. Memory Debugging Tools
  4. Code Reviews
  5. Breakpoints
  6. Assertion Statements
  7. Test Cases

Data Types and Sizes


In C programming language, there are several built-in data types that can be used to store different kinds of data. The most common data types and their sizes are:

  1. Integer types:
    • char: 1 byte
    • short: 2 bytes
    • int: 2 or 4 bytes
    • long: 4 bytes
    • long long: 8 bytes
  2. Floating-point types:
    • float: 4 bytes
    • double: 8 bytes
    • long double: 10 or 16 bytes
  3. Boolean type:
    • bool: 1 byte
  4. Void type:
    • void: has no size

It is important to note that the size of a data type may vary depending on the machine architecture and the compiler used. For example, on some machines, an int may be 2 bytes, while on others, it may be 4 bytes.

Declaration of variables

In C programming variables must be declared before they can be used. To declare a variable in C, you need to specify the data type and variable name. Here are some examples of variable declarations in C:

// Integer variable
int num;

// Floating-point variable
float pi;

// Character variable
char letter;

// Boolean variable (not directly supported in C)
// Instead, we use an integer variable to represent a boolean value
int isValid;

you are also initialize a variable during its declaration by assigning a value to it

// Integer variable initialized to 5
int num = 5;

// Floating-point variable initialized to 3.14159
float pi = 3.14159;

// Character variable initialized to 'a'
char letter = 'a';

// Boolean variable initialized to true (represented by 1)
int isValid = 1;

Modifiers

Modifiers are keywords in c which changes the meaning of introductory data type in c. It specifies the quantum of memory space to be allocated for a variable. Modifiers are prefixed with introductory data types to modify the memory allocated for a variable. There are five data type modifiers in C Language.

Types of modifier

  • long
  • short
  • signed
  • unsigned
  • long long

Identifiers and keywords

Identifiers

In C programming, identifiers are used to name variables, functions, and other program rudiments. An identifier is a sequence of letters, integers, and underscores(,) that begins with a letter or underscore. Identifiers are case-sensitive, which means that uppercase and lowercase letters are considered different. Then are some exemplifications of valid identifiers in C

int num;
float pi_value;
double _x;
char student_name[20];

keywords

C also has a set of reserved keywords that cannot be used as identifiers because they are used by the language itself. These keywords have a special meaning and are used to define the syntax and structure of the program. Here are the keywords in C:

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

Symbolic Constants

Symbolic Constant in C is a name that backups for a sequence of characters or a numeric constant, a character constant, or a string constant. When the program is collected each circumstance of a emblematic constant is replaced by its matching character sequence. A emblematic constant can be defined as a constant that’s represented by a name( symbol) in a program. Like a nonfictional constant, a emblematic constant can not suffer changes. Whenever the constant’s value is demanded in the program, the name of the constant is used in the same way as the name of a variable is used to pierce its value.

There are 2 types to defining a symbolic constants

  • Using the #define directive
  • Using the const keyword

Storage Classes

Storage classes in C are used to determine the continuance, visibility, memory position, and original value of a variable.

There are four types of storehouse classes in C.

  1. Automatic = Automatic variables are allocated memory automatically at runtime. The visibility of the automatic variables is limited to the block in which they are defined. The scope of the automatic variables is limited to the block in which they are defined.
  2. Static = The variables defined as static specifier can hold their value between the multiple function calls. Static local variables are visible only to the function or the block in which they are defined. A same static variable can be declared many times but can be assigned at only one time.
  3. Ecternal = The external storage class is used to tell the compiler that the variable defined as extern is declared with an external linkage elsewhere in the program. The variables declared as extern are not allocated any memory. It is only declaration and intended to specify that the variable is declared elsewhere in the program.
  4. Register = The variables defined as the register is allocated the memory into the CPU registers depending upon the size of the memory remaining in the CPU. We can not dereference the register variables, i.e., we can not use &operator for the register variable.

Enumerations

Enumeration is a stoner defined datatype in C language. It’s used to assign names to the integral constants which makes a program easy to read and maintain.
The keyword “ enum ” is used to declare an recitation.

enum State {Working = 1, Failed = 0};

The keyword ‘ enum ’ is used to declare new recitation types in C and C. Following is an illustration of enum protestation.

Command Line Parameters

Command line arguments are the arguments specified after the program name in the operating system’s command line. These argument values are passed to your program during prosecution from your operating system. To use this conception in your program, you have to understand the complete protestation of how the main() function works with the command- line argument to cost values that before took no arguments with it ( main() without any argument).

Macros

A macro is a segment of code which is replaced by the value of macro. Macro is defined by #define directive. There are two types of macros:

  1. Object-like Macros
  2. Function-like Macros

The C Preprocessor

In C programming, the C preprocessor is a tool that is used to perform text manipulation on the source code before it is compiled. The preprocessor is a separate component of the compiler that handles preprocessor directives, which are special commands that start with a hash (#) character.

Here are some of the most common preprocessor directives in C:

  • #Include
  • #define
  • #ifdef and #ifndef
  • #if , #elif, and #else
  • #error
    • #pragma

Operators

An driver is simply a symbol that’s used to perform operations. There can be numerous types of operations like computation, logical, bitwise,etc.

there are following types of operators to perform a different types of operations in c program

  1. Arithmetic Operators
  2. Relational Operators
  3. Shift Operators
  4. Logical Operators
  5. Bitwise Operators
  6. Ternary or Conditional Operators
  7. Assignment Operator
  8. Misc Operator

Unary Operators

In this section, we will bandy the unary driver in the C programming language. Drivers are the special symbols used to perform fine and logical operations to the given figures or operands and returns results grounded on passed driver between the operands.

A unary driver is an driver used to operate on a single operand to return a new value. In other words, it’s an driver that updates the value of an operand or expression’s value by using the applicable unary drivers. In Unary Operator, drivers have equal precedence from right to left side associativity.

Types of the Unary Operator

  1. Unary Minus (-)
  2. Unary Plus (+)
  3. Increment (++)
  4. Decrement (–)
  5. Logical Negation (!)
  6. Address Operator (&)
  7. Size of () operator

Arithmetic & logical Operator in C

Arithmetic Operator

Drivers are the special symbols in the C programming language, which is used to perform colorful fine and logical operations on the given operands to return the applicable results. There are colorful drivers in the C programming language similar as computation Drivers, Relational Drivers, Shift Drivers, Logical Drivers, Bitwise Drivers, Ternary or tentative Drivers, and Assignment Drivers. But then, we will understand only the computation Operator in the C programming language.

logical Operator

n driver can be defined as a symbol that’s used for performing different operations. In a programming language, there are colorful types of drivers similar as computation drivers, relational drivers, logical drivers, assignment driver, proliferation/ diminishment drivers, tentative drivers, bitwise drivers, and shift drivers.

Bitwise Operator

The bitwise drivers are the drivers used to perform the operations on the data at the bit- position. When we perform the bitwise operations, also it’s also known as bit- position programming. It consists of two integers, either 0 or 1. It’s substantially used in numerical calculations to make the computations briskly.


Assignment operators and expressions

Assignment Operator

There are different kinds of the drivers, similar as computation, relational, bitwise, assignment,etc., in the C programming language. The assignment driver is used to assign the value, variable and function to another variable. Let’s bandy the colorful types of the assignment drivers similar as = , = ,- = ,/ = , * = and = .

Expressions

Expressions in C are combinations of values, variables, and operators that can be evaluated to produce a result. An expression can be a simple value, such as a variable or a constant, or a more complex combination of values and operators.

Conditional expressions

Conditional expressions, also known as ternary drivers, are a longhand way of writing an if- additional statement in numerous programming languages. They allow you to snappily estimate a condition and return one of two values grounded on whether the condition is true or false.

Precedence and Order of Evaluation

The priority and associativity of C drivers affect the grouping and evaluation of operands in expressions. An driver’s priority is meaningful only if other drivers with advanced or lower priority are present. Expressions with advanced- priority drivers are estimated first. Priority can also be described by the word” list.” Drivers with a advanced priority are said to have tighter list.

The following table summarizes the priority and associativity ( the order in which the operands are estimated) of C drivers, listing them in order of priority from loftiest to smallest. Where several drivers appear together, they’ve equal priority and are estimated according to their associativity. The drivers in the table are described in the sections beginning with Postfix Drivers. The rest of this section gives general information about priority and associativity.

Control Statements

Control statements enable us to specify the inflow of program control — that is, the order in which the instructions in a program must be executed. They make it possible to make opinions, to perform tasks constantly or to jump from one section of law to another.

Types of Control Statements

  1. Decision making statements (if, if-else)
  2. Selection statements (switch-case)
  3. Iteration statements (for, while, do-while)
  4. Jump statements (break, continue, goto)

If-else

the if- additional statement is used to perform two operations for a single condition. The if- additional statement is an extension to the if statement using which, we can perform two different operations, i.e., one is for the correctness of that condition, and the other is for the incorrectness of the condition. Then, we must notice that if and additional block can not be executed simiulteneously. Using if- additional statement is always preferable since it always invokes an else case with every if condition

switch

In switch statement allows a variable to be tested for equivalency against a list of values. Each value is called a case, and then the variable being switched on is checked for each switch case.

Syntax

The syntax for a switch statement in C programming language

switch(expression) {

   case constant-expression  :
      statement(s);
      break; /* optional */
	
   case constant-expression  :
      statement(s);
      break; /* optional */
  
   /* you can have any number of case statements */
   default : /* Optional */
   statement(s);
}

Break

In C programming, break is a keyword that’s used to exit a circle, similar as a for circle, while circle, or do- while circle. When break is encountered in a circle, the circle is terminated incontinently, and control is passed to the statement following the circle.

The basic syntax of break statement is:

break;

Continue

In C programming, continue is a keyword that is used to skip the current iteration of a loop, such as a for loop, while loop, or do-while loop. When continue is encountered in a loop, the current iteration is immediately terminated, and control is passed to the next iteration.

The basic syntax of continue statement is:

Continue;

The Comma Operator

In C programming, the comma operator is an operator that allows you to evaluate multiple expressions, separated by commas, and return the value of the last expression. The comma operator has the lowest precedence of any operator in C, which means that it is evaluated last in an expression.

The basic syntax of the comma operator is:

expr1, expr2, ..., exprN

Go to Statement

The goto statement is known as jump statement in C. As the name suggests, goto is used to transfer the program control to a predefined marker. The goto statment can be used to repeat some part of the law for a particular condition. It can also be used to break the multiple circles which can not be done by using a single break statement. still, using goto is avoided these days since it makes the program less readable and compleicated.

Syntax:

label:   
//some part of the code;   
goto label;  

Loops

The looping can be defined as repeating the same process multiple times until a specific condition satisfies. There are three types of circles used in the C language. In this part of the tutorial, we’re going to learn all the aspects of C circles.

For

The for circle is used in the case where we need to execute some part of the law until the given condition is satisfied. The for circle is also called as a per- tested circle. It’s better to use for circle if the number of replication is known in advance.

While

The while circle in c is to be used in the script where we do not know the number of duplications in advance. The block of statements is executed in the while circle until the condition specified in the while circle is satisfied. It’s also called apre-tested circle.).

Do – While

The do- while circle continues until a given condition satisfies. It’s also called post tested circle. It’s used when it’s necessary to execute the circle at least formerly (substantially menu driven programs).

Functions

In c, we can divide a large program into the introductory structure blocks known as function. The function contains the set of programming statements enclosed by{}. A function can be called multiple times to give 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.

Built-in

These functions formerly live in the libraries and we need not declare and define these functions. We can directly call these functions by including the title lines in which these functions are present. For illustration, by usingstdio.h title train, we can perform scanf(), printf() functions, scanf() is used to take input from the stoner and printf() is used to publish the affair on the press screen.

User-defined

ser – defined functions are the functions that are defined by the stoner at the time of writing the program. Functions are made for lawre-usability and for saving time and space.

In C, main() is the stoner- defined function and first calling function in any program. main() is a special function that tells the compiler to start the prosecution of a C program from the morning of the function main().

Function Declaration

A function protestation in C provides information to the compiler on the name and system of calling a function. The function’s factual body can be specified singly.

A function declaration has the following parts −

return_type function_name( parameter list );
For the above-defined function max(), the function declaration is as follows −

int max(int num1, int num2);

Only their type is necessary when defining a function; hence, the following declaration is likewise acceptable:

int max(int, int);

Function call

A function call is a statement that invokes a function and passes arguments to it. When a function is called, the control is transferred to the function body, and the statements inside the function are executed. After the execution of the function is completed, the control is returned to the caller, and the program resumes its execution from the point where the function was called.

Parameter Passing

When a function gets executed in the program, the prosecution control is transferred from calling- function to called function and executes function description, and eventually comes back to the calling function. When the prosecution control is transferred from calling- function to called- function it may carry one or number of data values. These data values are called as parameters.

Call by Value

In call by value parameter end system, the dupe of factual parameter values are copied to formal parameters and these formal parameters are used in called function. The changes made on the formal parameters doesn’t prompt the values of factual parameters. That means, after the prosecution control comes back to the calling function, the factual parameter values remains same. For illustration consider the following program.

Call by Reference

In call by value parameter end system, the dupe of factual parameter values are copied to formal parameters and these formal parameters are used in called function. The changes made on the formal parameters doesn’t prompt the values of factual parameters. That means, after the prosecution control comes back to the calling function, the factual parameter values remains same. For illustration consider the following program.

Recursive Function

A recursive function performs the tasks by dividing it into the subtasks.this is a termination condition defined in the function which is satisfied by some specific subtask. After this, the recursion stops and the final result is returned from the function.

The case at which the function doesn’t recur is called the base case whereas the instances where the function keeps calling itself to perform a subtask, is called the recursive case. All the recursive functions can be written using this format.

Multi-file Programs

In C programming, a multi-file program is a program that is composed of multiple source files, each containing a part of the program’s code. Multi-file programs are useful for dividing a large program into smaller, more manageable parts, and for sharing code between different parts of the program.

To create a multi-file program, you typically start by defining the main function in one source file, which serves as the entry point for the program. Then, you can define other functions and data structures in separate source files, and declare them using header files.

Arrays

An array is defined as the collection of analogous type of data particulars stored at conterminous memory locales. Arrays are the deduced data type in C programming language which can store the primitive type of data similar as int, housekeeper, double, pier, etc. It also has the capability to store the collection of deduced data types, similar as pointers, structure, etc. The array is the simplest data structure where each data element can be aimlessly penetrated by using its indicator number.

Linear arrays

A direct array, is a list of finite figures of rudiments stored in the memory. In a direct array, we can store only homogeneous data rudiments. rudiments of the array form a sequence or direct list, that can have the same type of data. Each element of the array, is appertained by an indicator set.

multidimensional arrays

A multi-dimensional array can be nominated as an array of arrays that stores homogeneous data in irregular form. Data in multidimensional arrays are stored in row-major order.

Passing Array to Function

there are various general problems which requires passing more than one variable of the same type to a function. For ex, consider a function which sorts the 10 elements in ascending order. Such a function requires 10 numbers to be passed as the actual parameters from the main function. Here, instead of declaring 10 different numbers and then passing into the function, we can declare and initialize an array and pass that into the function. This will resolve all the complexity since the function will now work for any number of values.

Arrays and strings

A string in C is an array of characters terminated by a null character, which is represented by the ASCII value 0. For example, the following declaration creates a string containing the word “hello”.

A string in C is an array of characters terminated by a null character, which is represented by the ASCII value 0. For example, the following declaration creates a string containing the word “hello”.

Structure and Union

Structure

An array allows us to store a group of rudiments of the same data type together. In numerous cases, we need to store data particulars of different types as a group. Structures and Unions in C allows a set of rudiments of different types to be stored as a group.

Union

Union is also a stoner defined datatype. All the members of union share the same memory position. Size of union is decided by the size of largest member ofunion.However, union is the stylish for that, If you want to use same memory position for two or further members.

Unions are analogous to the structure. Union variables are created in same manner as structure variables. The keyword “ union ” is used to define unions in C language.

Self-Referential Structure

A self-referential structure is a structure in which one or more members of the structure point to the same type of structure. In other words, a structure member contains a pointer to the same type of structure as the one it is a member of.

Self-referential structures are useful for implementing complex data structures like trees and linked lists, where each node of the structure contains a pointer to the next node or child node. In these cases, the self-referential structure allows us to create a linked sequence of nodes that can be traversed and manipulated.

Address operator

The address-of operator & in C programming is used to obtain the memory address of a variable. When the address-of operator is applied to a variable, it returns the memory address of that variable.

Pointer To Pointer

A pointer to a pointer is a form of multiple indirection, or a chain of pointers. typically, a pointer contains the address of a variable. When we define a pointer to a pointer, the first pointer contains the address of the alternate pointer, which points to the position that contains the factual value as shown below.

Dynamic Memory Allocation

The conception of dynamic memory allocation in c language enables the C programmer to allocate memory at runtime. Dynamic memory allocation in c language is possible by 4 functions ofstdlib.h title train.

  1. malloc()
  2. calloc()
  3. realloc()
  4. free()

Malloc and Calloc Functions

In C programming, malloc and calloc are two functions used for dynamic memory allocation. These functions allow programs to allocate memory at runtime, which is useful when the required memory size is not known at compile-time.

Array of Pointers

The pointers and arrays are veritably nearly related to one another in the C language. The program considers an array as a pointer. In simpler words, an array name consists of the addresses of the rudiments. Before we understand what an array of pointers is, let us understand further about pointers and arrays independently.

Function of Pointers

Pointers are variables that store memory addresses. They are used to manipulate data indirectly by referring to the location of data in memory rather than by its value.

The functions of pointers in programming languages are numerous, but some of the most common uses are:

  1. Dynamic memory allocation
  2. Passing arguments to functions
  3. Returning values from functions
  4. Accessing arrays
  5. Implementing data structures
  6. Interacting with hardware

Programs

program of c identifiers

int main( )  {  
    int a=10;  
    int A=20;  
    printf("Value of a is : %d",a);  
    printf("\nValue of A is :%d",A);  
    return 0;  
}  

Output

Value of a is : 10
Value of A is :20  

The below affair shows that the values of both the variables,’ a’ and’ A’ are different. thus, we conclude that the identifiers are case sensitive.

Program Of Loops

1. for loop

#include <stdio.h>
int main()
 {
int num=10,sum=0,incr;
for(incr = 1; incr <= num; ++incr)
  {
    	sum += incr;
  }
printf("Welcome to Make query...\n\n");
printf("Sum of the first %d numbers = %d", num,sum);
  return 0;
}

Output

Welcome to Make query…
Sum of the first 10 numbers = 55

2. While loop

#include<stdio.h>  
int main()
{   
printf("Welcome to Make query...\n");
int i=1,number=10;    
while(i<=10){
printf("%d * %d = %d \n", number, i, (number*i));    
i++;    
}    
return 0;  
}

Output

Welcome to Make query …
10 * 1 = 10
10 * 2 = 20
10 * 3 = 30
10 * 4 = 40
10 * 5 = 50
10 * 6 = 60
10 * 7 = 70
10 * 8 = 80
10 * 9 = 90
10 * 10 = 100

3. Do while loop

#include<stdio.h>
int main()
{
int number = 10;
int factorial = 1;
printf("Welcome to Make query...\n\n");
do 
{
  factorial *= number--;
} while (number > 0);

printf("factorial of 10 is %d\n", factorial);  
}

Output

Welcome to Make query…
factorial of 10 is 3628800

Programs storage classes

C-programming