Introduction

C programming is a general-purpose, procedural, imperative computer programming language developed in 1972 by Dennis M. Ritchie at the Bell Telephone Laboratories to develop the UNIX operating system. C is the most widely used computer language. It keeps fluctuating at number one scale of popularity along with Java programming language, which is also equally popular and most widely used among modern software programmers.

Features

  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. Extensible
Get Started

There are many compilers available for c and c++. You need to download any one. Here, we are going to use Turbo C++. It will work for both C and C++. To install the Turbo C software, you need to follow following steps.

  1. Download Turbo C++
  2. Create turboc directory inside c drive and extract the tc3.zib inside c:\turboc
  3. Double click on install.exe file
  4. Click on the tc application file loacted inside c:\TC\BIN to write the c program
  5.                     
    #include<stdio.h>
    int main(){
        printf("Hellow World")
        return 0;
    }
                        
                        
  6. Now it time to compile you can compile by two ways:
    1. By clicking on the compile menu then compile sub menu to compile the c program
    2. By pressing ctrl+f9 keys compile and run the program directly.
  7. You are able to see output screen displaying
  8. 
    Hellow World
                        
In this basic program we have 4 lines of code lets discuss it:
  • #include<stdio.h>:- includes the standard input output library functions. The printf() function is defined in stdio.h .
  • int main():-The main() function is the entry point of every program in c language.
  • printf("Hellow World");:- It is function defind in stdio.h header file for printing data on console.
  • return 0;:-The return 0 statement, returns execution status to the OS. The 0 value is used for successful execution and 1 for unsuccessful execution.
Variable

A variable is a name of the 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:

int a;
float b;
char c;
                

C Rules for Defining variable

  • A variable can have alphabets, digits, and underscore.
  • A variable name can start with the alphabet, and underscore only. It can't start with a digit.
  • No whitespace is allowed within the variable name.
  • A variable name must not be any reserved word or keyword, e.g. int, float, etc.
Valid variable name:

int a;
int _ab;
int a20;
                 
Invalid variable name:

int 2;  
int a b;  
int long;
                 
Types of Variables in C
  1. local variable
  2. global variable
  3. static variable
  4. automatic variable
  5. external variable
Data Types
A data type specifies the type of data that a variable can store such as integer, floating, character, etc.
There are the following data types in C language.
  • 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 the basic data types may change according to 32 or 64-bit operating system.

Let's see the basic data types. Its size is given according to 32-bit architecture.
Data Types Memory Size Range
char 1 byte -128 to 127
short 2 byte -32,768 to 32,767
int 2 byte -32,768 to 32,767
unsigned int 2 byte 0 to 65,535
long int 4 byte -2,147,483,648 to 2,147,483,647
unsigned long int 4 byte 0 to 4,294,967,295
float 4 byte
double 8 byte
long double 10 byte
Operators

An operator is simply a symbol that is used to perform operations. There can be many types of operations like arithmetic, logical, bitwise, etc.

There are following types of operators to perform different types of operations in C language.

  • Arthmetic Operators
  • Relational Operators
  • Shift Operators
  • Logical Operators
  • BitwiseOperators
  • Ternary or Conditional Operators
  • Assignment Operator
  • Misc Operator

Precedence of Operators in C

The precedence of operator species that which operator will be evaluated first and next. The associativity specifies the operator direction to be evaluated; it may be left to right or right to left. Let's understand the precedence by the example given below:
Category Operator
Postfix () [] -> . ++ --
Unary + - ! ~ ++ -- (type)* & sizeof
Multiplicative * / %
Additive + -
Shift <<>>
Relational < <=> >=
Equality == !=
Bitwise AND &
Bitwise XOR ^
Bitwise OR |
Logical AND &&
Conditional ?:
Assignment = += -= *= /= %= >>= <<= &= ^= |=
Comma ,
C if else Statement

The if-else statement in C is used to perform the operations based on some specific condition. The operations specified in if block are executed if and only if the given condition is true.


There are the following variants of if statement in C language.
  • if statement
  • If-else statement
  • If else-if ladder
  • Nested if

If Statement

The if statement is used to check some given condition and perform some operations depending upon the correctness of that condition. It is mostly used in the scenario where we need to perform the different operations for the different conditions. The syntax of the if statement is given below.

                    
if(expression){  
//code to be executed  
}  
                    
                
Let's see a simple example of C language if statement.
                    
#include<stdio.h>    
int main(){    
int number=0;    
printf("Enter a number:");    
scanf("%d",&number);    
if(number%2==0){    
    printf("%d is even number",number);    
}    
return 0;  
}    
                    
                

Output


Enter a number:4
4 is even number
enter a number:5
                

If else statement

The if-else statement is used to perform two operations for a single condition. The if-else 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. Here, we must notice that if and else block cannot be executed simiulteneously. Using if-else statement is always preferable since it always invokes an otherwise case with every if condition. The syntax of the if-else statement is given below.

if(expression){  
//code to be executed if condition is true  
}else{  
//code to be executed if condition is false  
} 
                
Let's see the simple example to check whether a number is even or odd using if-else statement in C language.

#include<stdio.h>    
int main(){    
    int number=0;    
    printf("enter a number:");    
    scanf("%d",&number);     
    if(number%2==0){    
        printf("%d is even number",number);    
    }    
    else{    
        printf("%d is odd number",number);    
    }     
    return 0;  
}   
                
Output

enter a number:4
4 is even number
enter a number:5
5 is odd number
                

else-if ladder

The if-else-if ladder statement is an extension to the if-else statement. It is used in the scenario where there are multiple cases to be performed for different conditions. In if-else-if ladder statement, if a condition is true then the statements defined in the if block will be executed, otherwise if some other condition is true then the statements defined in the else-if block will be executed, at the last if none of the condition is true then the statements defined in the else block will be executed. There are multiple else-if blocks possible. It is similar to the switch case statement where the default is executed instead of else block if none of the cases is matched.

if(condition1){  
//code to be executed if condition1 is true  
}else if(condition2){  
//code to be executed if condition2 is true  
}  
else if(condition3){  
//code to be executed if condition3 is true  
}  
...  
else{  
//code to be executed if all the conditions are false  
}  
                
The example of an if-else-if statement in C language is given below.

#include<stdio.h>    
int main(){    
    int number=0;    
    printf("enter a number:");    
    scanf("%d",&number);     
    if(number==10){    
        printf("number is equals to 10");    
    }    
    else if(number==50){    
        printf("number is equal to 50");    
    }    
    else if(number==100){    
        printf("number is equal to 100");    
    }    
    else{    
        printf("number is not equal to 10, 50 or 100");    
    }    
    return 0;  
}   
                
Output

enter a number:4
number is not equal to 10, 50 or 100
enter a number:50
number is equal to 50
                
Loops in C

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

Types of 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

The do-while loop continues until a given condition satisfies. It is also called post tested loop. It is used when it is necessary to execute the loop at least once (mostly menu driven programs).

The syntax of do-while loop in c language is given below:

do{
    //code to be executed
}while(condition);
                

while loop in c

The while loop in c is to be used in the scenario where we don't know the number of iterations in advance. The block of statements is executed in the while loop until the condition specified in the while loop is satisfied. It is also called a pre-tested loop.

The syntax of while loop in c language is given below:

while(condition){
    //code to be executed
}
                

for loop in c

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

The syntax of for loop in c language is given below:

for(initializationl;condition;incr/decr){
    //code to be executed
}
                
Function in C

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.

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 fucntion aspects syntax
1 Function declaration eturn_type function_name(argrument list)
2 Function call funtion_name(argument_list)
3 Function definition return_type function_name(argument list){function body;}
Refrence