The Programming Language-1 (6622) tutorial and Problem solved video tutorial, SEO tutorial for Online Earning.

Showing posts with label C programming Language. Show all posts
Showing posts with label C programming Language. Show all posts

The C Programming Language

C as a mother language

language is considered as the mother language of all the modern languages because most of the compilers, JVMs, Kernals etc. are written in C language and most of languages follows c syntax e.g. C++, Java etc. It provides the core concepts like array, functions, file handling etc. that is being used in many languages like C++, java, C# etc.

C as a system programming language

A system programming language is used to create system softwares. C language is a system programming language because it can be used to do low level programming (e.g. driver and kernel). It is generally used to create hardware devices, OS, drivers, kernels etc. For example, linux kernel is written in C. It can?t be used in internet programming like java, .net, php etc.

C as a procedural language

A procedure is known as function, method, routine, subroutine etc. A procedural language specifies a series of steps or procedures for the program to solve the problem. A procedural language breaks the program into functions, data structures etc. C is a procedural language. In C, variables and function prototypes must be declared before being used.

C as a structured programming language

A structured programming language is a subset of procedural language. Structure means to break a program into parts or blocks so that it may be easy to understand. In C language, we break the program into parts using functions. It makes the program easier to understand and modify.

C as a mid-level programming language

C is considered as a middle level language because it supports the feature of both low-level and high level language. C language program is converted into assembly code, supports pointer arithmetic (low level), but it is machine independent (feature of high level). Low level language is specific to one machine i.e. machine dependent. It is machine dependent, fast to run. But it is not easy to understand. High Level language is not specific to one machine i.e. machine independent. It is easy to understand.

Source : javatpoind.com

Features of C Language

C is the widely used language. It provides a lot of 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 structured approach (to break the problem into parts), rich set of library functions, data types etc.

2) Machine Independent or Portable

Unlike assembly language, c programs can be executed in many machines with little bit or no change. But it is not platform-independent.

3) Mid-level prorgramming language

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

4) Structured prorgramming 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.

5) Rich Library

C provides a lot of inbuilt functions that makes 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.

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.

10) Extensible

C language is extensible because it can easily adopt new features.


Source : javatpoind.com

History of C Language

History of C language is interesting to know. Here we are going to discuss brief history of c language.
C programming language was developed in 1972 by Dennis Ritchie at bell laboratories of AT&T (American Telephone & Telegraph), located in U.S.A. Dennis Ritchie is known as the founder of c language. It was developed to overcome the problems of previous languages such as B, BCPL etc. Initially, C language was developed to be used in UNIX operating system. It inherits many features of previous languages such as B and BCPL.
Let's see the programming languages that were developed before C language.
LanguageYearDeveloped By
Algol1960International Group
BCPL1967Martin Richard
B1970Ken Thompson
Traditional C1972Dennis Ritchie
K & R C1978Kernighan & Dennis Ritchie
ANSI C1989ANSI Committee
ANSI/ISO C1990ISO Committee
C991999Standardization Committee

Source :javatpoind.com

Variables + data type with input output operation in C

A variable is a name of memory location. It is used to store data. Its value can be changed and it can be reused many times.
It is a way to represent memory location through symbol so that it can be easily identified.
Let's see the syntax to declare a variable:


  1. type variable_list;  
The example of declaring variable is given below:
  1. int a;  
  2. float b;  
  3. char c;  
Here, a, b, c are variables and int,float,char are data types.
We can also provide values while declaring the variables as given below:
  1. int a=10,b=20;//declaring 2 variable of integer type  
  2. float f=20.8;  
  3. char c='A';  
There are many types of variables in c:
  1. local variable
  2. global variable
  3. static variable
  4. automatic variable
  5. external variable

Local Variable

A variable that is declared inside the function or block is called local variable.
It must be declared at the start of the block.
  1. void function1(){  
  2. int x=10;//local variable  
  3. }  
You must have to initialize the local variable before it is used.

Global Variable

A variable that is declared outside the function or block is called global variable. Any function can change the value of the global variable. It is available to all the functions.
It must be declared at the start of the block.
  1. int value=20;//global variable  
  2. void function1(){  
  3. int x=10;//local variable  
  4. }  

Static Variable

A variable that is declared with static keyword is called static variable.
It retains its value between multiple function calls.
  1. void function1(){  
  2. int x=10;//local variable  
  3. static int y=10;//static variable  
  4. x=x+1;  
  5. y=y+1;  
  6. printf("%d,%d",x,y);  
  7. }  
If you call this function many times, local variable will print the same value for each function call e.g, 11,11,11 and so on. But static variable will print the incremented value in each function call e.g. 11, 12, 13 and so on.

Automatic Variable

All variables in C that is declared inside the block, are automatic variables by default. By we can explicitly declare automatic variable using auto keyword.
  1. void main(){  
  2. int x=10;//local variable (also automatic)  
  3. auto int y=20;//automatic variable  
  4. }  

External Variable

We can share a variable in multiple C source files by using external variable. To declare a external variable, you need to use extern keyword.
myfile.h
  1. extern int x=10;//external variable (also global)  
program1.c
  1. #include "myfile.h"  
  2. #include <stdio.h>  
  3. void printValue(){  
  4.     printf("Global variable: %d", global_variable);  
  5. }  


  6. A C constant is usually just the written version of a number. For example 1, 0, 5.73, 12.5e9. We can specify our constants in octal or hexadecimal, or force them to be treated as long integers.
    • Octal constants are written with a leading zero - 015.
    • Hexadecimal constants are written with a leading 0x - 0x1ae.
    • Long constants are written with a trailing L - 890L.
    Character constants are usually just the character enclosed in single quotes; 'a', 'b', 'c'. Some characters can't be represented in this way, so we use a 2 character sequence as follows.
    '\n' newline
    '\t' tab
    '\\' backslash
    '\'' single quote
    '\0' null ( Usedautomatically to terminate character string )
    
    In addition, a required bit pattern can be specified using its octal equivalent.
    '\044' produces bit pattern 00100100.
    Character constants are rarely used, since string constants are more convenient. A string constant is surrounded by double quotes eg "Brian and Dennis". The string is actually stored as an array of characters. The null character '\0' is automatically placed at the end of such a string to act as a string terminator.
    A character is a different type to a single character string. This is important poing to note.

    Defining Constants

    ANSI C allows you to declare constants. When you declare a constant it is a bit like a variable declaration except the value cannot be changed.
    The const keyword is to declare a constant, as shown below:
    int const a = 1;
    const int a =2;
    
    Note:
    • You can declare the const before or after the type. Choose one an stick to it.
    • It is usual to initialise a const with a value as it cannot get a value any other way.
    The preprocessor #define is another more flexible (see Preprocessor Chapters) method to define constants in a program.
    #define TRUE  1
    #define FALSE  0
    #define NAME_SIZE 20
    
    Here TRUE, FALSE and NAME_SIZE are constant
    You frequently see const declaration in function parameters. This says simply that the function is not going to change the value of the parameter.
    The following function definition used concepts we have not met (see chapters on functions, strings, pointers, and standard libraries) but for completenes of this section it is is included here:
    void strcpy(char *buffer, char const *string)
    




    Data Types in C

    A data type specifies the type of data that a variable can store such as integer, floating, character etc.
    There are 4 types of data types in C language.

    TypesData Types
    Basic Data Type :int, char, float, double
    Derived Data Type :array, pointer, structure, union
    Enumeration Data Type:enum
    Void Data Type :void

    Basic Data Types

    The basic data types are integer-based and floating-point based. C language supports both signed and unsigned literals.
    The memory size of basic data types may change according to 32 or 64 bit operating system.
    Let's see the basic data types. It size is given according to 32 bit OS.

    Data TypesMemory SizeRange
    char1 byte−128 to 127
    signed char1 byte−128 to 127
    unsigned char1 byte0 to 127
    short2 byte−32,768 to 32,767
    signed short2 byte−32,768 to 32,767
    unsigned short2 byte0 to 32,767
    int2 byte−32,768 to 32,767
    signed int2 byte−32,768 to 32,767
    unsigned int2 byte0 to 32,767
    short int2 byte−32,768 to 32,767
    signed short int2 byte−32,768 to 32,767
    unsigned short int2 byte0 to 32,767
    long int4 byte
    signed long int4 byte
    unsigned long int4 byte
    float4 byte
    double8 byte
    long double10 byte






Source : javatpoind.com

C Control Statements if-else, switch

C if else Statement

The if statement in C language is used to perform operation on the basis of condition. By using if-else statement, you can perform operation either condition is true or false.
There are many ways to use if statement in C language:
  • If statement
  • If-else statement
  • If else-if ladder
  • Nested if
  •  

If Statement

The single if statement in C language is used to execute the code if condition is true. The syntax of if statement is given below:
  1. if(expression){  
  2. //code to be executed  
  3. }  

If-else Statement

The if-else statement in C language is used to execute the code if condition is true or false. The syntax of if-else statement is given below:
  1. if(expression){  
  2. //code to be executed if condition is true  
  3. }else{  
  4. //code to be executed if condition is false  
  5. }  

If else-if ladder Statement

The if else-if statement is used to execute one code from multiple conditions. The syntax of if else-if statement is given below:
  1. if(condition1){  
  2. //code to be executed if condition1 is true  
  3. }else if(condition2){  
  4. //code to be executed if condition2 is true  
  5. }  
  6. else if(condition3){  
  7. //code to be executed if condition3 is true  
  8. }  
  9. ...  
  10. else{  
  11. //code to be executed if all the conditions are false  
  12. }  

C Switch Statement

The switch statement in C language is used to execute the code from multiple conditions. It is like if else-if ladder statement.
The syntax of switch statement in c language is given below:
  1. switch(expression){    
  2. case value1:    
  3.  //code to be executed;    
  4.  break;  //optional  
  5. case value2:    
  6.  //code to be executed;    
  7.  break;  //optional  
  8. ......    
  9.     
  10. default:     
  11.  code to be executed if all cases are not matched;    
  12. }    

Rules for switch statement in C language

1) The switch expression must be of integer or character type.
2) The case value must be integer or character constant.
3) The case value can be used only inside the switch statement.
4) The break statement in switch case is not must. It is optional. If there is no break statement found in switch case, all the cases will be executed after matching the case value. It is known as fall through state of C switch statement.
Let's try to understand it by the examples. We are assuming there are following variables.
  1. int x,y,z;  
  2. char a,b;  
  3. float f;  
Valid SwitchInvalid SwitchValid CaseInvalid Case
switch(x)switch(f)case 3;case 2.5;
switch(x>y)switch(x+2.5)case 'a';case x;
switch(a+b-2)
case 1+2;case x+2;
switch(func(x,y))
case 'x'>'y';case 1,2,3;



 
Source : javatpoind.com

C Control Statements-while loop, do while loop, for loop

C Loops

The loops in C language are used to execute a block of code or a part of the program several times.
In other words, it iterates a code or group of code many times.

Advantage of loops in C

1) It saves code.
2) It helps to traverse the elements of array (which is covered in next pages).


Types of C Loops

There are three types of loops in C language that is given below:

  1. do while
  2. while
  3. for

do-while loop in C

It iterates the code until condition is false. Here, condition is given after the code. So at least once, code is executed whether condition is true or false.
It is better if you have to execute the code at least once.
The syntax of do-while loop in c language is given below:
  1. do{  
  2. //code to be executed  
  3. }while(condition);  


while loop in C

Like do while, it iterates the code until condition is false. Here, condition is given before the code. So code may be executed 0 or more times.
It is better if number of iteration is not known by the user.
The syntax of while loop in c language is given below:
  1. while(condition){  
  2. //code to be executed  
  3. }  


for loop in C

Like while, it iterates the code until condition is false. Here, initialization, condition and increment/decrement is given before the code. So code may be executed 0 or more times.
It is good if number of iteration is known by the user.
The syntax of for loop in c language is given below:
  1. for(initialization;condition;incr/decr){  
  2. //code to be executed  
  3. }  

Source : javatpoind.com

functions in C

The function in C language is also known as procedure or subroutine in other programming languages. To perform any task, we can create function. A function can be called many times. It provides modularity and code reusability.



Advantage of functions in C

There are many advantages of functions.

1) Code Reusability

By creating functions in C, you can call it many times. So we don't need to write the same code again and again.

2) Code optimization

It makes the code optimized, we don't need to write much code.
Suppose, you have to check 3 numbers (781, 883 and 531) whether it is prime number or not. Without using function, you need to write the prime number logic 3 times. So, there is repetition of code.
But if you use functions, you need to write the logic only once and you can reuse it several times.

Syntax to declare function in C

The syntax of creating function in c language is given below:
  1. return_type function_name(data_type parameter...){  
  2. //code to be executed  
  3. }  

Syntax to call function in C

The syntax of calling function in c language is given below:
  1. variable=function_name(arguments...);  
1) variable: The variable is not mandatory. If function return type is void, you must not provide the variable because void functions doesn't return any value.
2) function_name: The function_name is name of the function to be called.
3) arguments: You need to provide same number of arguments as defined in the function at the time of declaration or definition.


Source : javatpoind.com

C Array

C Array

Array in C language is a collection or group of elements (data). All the elements of c array are homogeneous (similar). It has contiguous memory location.
C array is beneficial if you have to store similar elements. Suppose you have to store marks of 50 students, one way to do this is allotting 50 variables. So it will be typical and hard to manage. For example we can not access the value of these variables with only 1 or 2 lines of code.
Another way to do this is array. By using array, we can access the elements easily. Only few lines of code is required to access the elements of array.


Advantage of C Array

1) Code Optimization: Less code to the access the data.
2) Easy to traverse data: By using the for loop, we can retrieve the elements of an array easily.
3) Easy to sort data: To sort the elements of array, we need a few lines of code only.
4) Random Access: We can access any element randomly using the array.

Disadvantage of C Array

1) Fixed Size: Whatever size, we define at the time of declaration of array, we can't exceed the limit. So, it doesn't grow the size dynamically like LinkedList which we will learn later.

Declaration of C Array

We can declare an array in the c language in the following way.
  1. data_type array_name[array_size];  
Now, let us see the example to declare array.
  1. int marks[5];  
Here, int is the data_type, marks is the array_name and 5 is the array_size.

Initialization of C Array

A simple way to initialize array is by index. Notice that array index starts from 0 and ends with [SIZE - 1].
  1. marks[0]=80;//initialization of array  
  2. marks[1]=60;  
  3. marks[2]=70;  
  4. marks[3]=85;  
  5. marks[4]=75;  
initialization of array in c language

Two Dimensional Array in C

The two dimensional array in C language is represented in the form of rows and columns, also known as matrix. It is also known as array of arrays or list of arrays.
The two dimensional, three dimensional or other dimensional arrays are also known as multidimensional arrays.

Declaration of two dimensional Array in C

We can declare an array in the c language in the following way.
  1. data_type array_name[size1][size2];  
A simple example to declare two dimensional array is given below.
  1. int twodimen[4][3];  
Here, 4 is the row number and 3 is the column number.

Initialization of 2D Array in C

A way to initialize the two dimensional array at the time of declaration is given below.
  1. int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};