Definition: an algorithm is a finite sequence of instructions, a logic and explicit step-by-step procedure for solving a problem starting from a known beginning. – the number of instructions must be finite

flowchart is a type of diagram that represents an algorithm, workflow or process, showing the steps as boxes of various kinds, and their order by connecting them with arrows.


Identifiers are names for entities in a C program, such as variables, arrays, functions, structures, unions and labels. An identifier can be composed only of uppercase, lowercase letters, underscore and digits, but should start only with an alphabet or an underscore.An identifier is a string of alphanumeric characters that begins with an alphabetic character or an underscore character that are used to represent various programming elements such as variables, functions, arrays, structures, unions and so on.


A variable is nothing but a name given to a storage area that our programs can manipulate.


Constants refer to fixed values that the program may not alter during its execution. These fixed values are also called literals. Constants can be of any of the basic data types like an integer constant, a floating constant, a character constant, or a string literal.


  1. int - integer: a whole number.
  2. float - floating point value: ie a number with a fractional part.
  3. double - a double-precision floating point value.
  4. char - a single character.
  5. void
char1 byte-128 to 127 or 0 to 255
unsigned char1 byte0 to 255
signed char1 byte-128 to 127
int2 or 4 bytes-32,768 to 32,767 or -2,147,483,648 to 2,147,483,647
float4 byte1.2E-38 to 3.4E+386 decimal places
double8 byte2.3E-308 to 1.7E+30815 decimal places
long double10 byte3.4E-4932 to 1.1E+493219 decimal places



OPERATORS: -

Operators in C Language

C language supports a rich set of built-in operators. An operator is a symbol that tells the compiler to perform certain mathematical or logical manipulations. Operators are used in program to manipulate data and variables.
C operators can be classified into following types,
  • Arithmetic operators
  • Relation operators
  • Logical operators
  • Bitwise operators
  • Assignment operators
  • Conditional operators
  • Special operators

Arithmetic operators

C supports all the basic arithmetic operators. The following table shows all the basic arithmetic operators.
OperatorDescription
+adds two operands
-subtract second operands from first
*multiply two operand
/divide numerator by denominator
%remainder of division
++Increment operator - increases integer value by one
--Decrement operator - decreases integer value by one

Relation operators

The following table shows all relation operators supported by C.
OperatorDescription
==Check if two operand are equal
!=Check if two operand are not equal.
>Check if operand on the left is greater than operand on the right
<Check operand on the left is smaller than right operand
>=check left operand is greater than or equal to right operand
<=Check if operand on left is smaller than or equal to right operand

Logical operators

C language supports following 3 logical operators. Suppose a=1 and b=0,
OperatorMeaning of OperatorExample
&&Logial AND. True only if all operands are true If c = 5 and d = 2 then, expression ((c == 5) && (d > 5)) equals to 0.
||Logical OR. True only if either one operand is trueIf c = 5 and d = 2 then, expression ((c == 5) || (d > 5)) equals to 1.
!Logical NOT. True only if the operand is 0If c = 5 then, expression ! (c == 5)equals to 0.

Bitwise operators

Bitwise operators perform manipulations of data at bit level. These operators also perform shifting of bits from right to left. Bitwise operators are not applied to float or double.
OperatorDescription
&Bitwise AND
|Bitwise OR
^Bitwise exclusive OR
<<left shift
>>right shift


Assignment Operators

Assignment operators supported by C language are as follows.
OperatorDescriptionExample
=assigns values from right side operands to left side operanda=b
+=adds right operand to the left operand and assign the result to lefta+=b is same as a=a+b
-=subtracts right operand from the left operand and assign the result to left operanda-=b is same as a=a-b
*=mutiply left operand with the right operand and assign the result to left operanda*=b is same as a=a*b
/=divides left operand with the right operand and assign the result to left operanda/=b is same as a=a/b
%=calculate modulus using two operands and assign the result to left operanda%=b is same as a=a%b

 C Ternary Operator (?:)

A conditional operator is a ternary operator, that is, it works on 3 operands.

Conditional Operator Syntax

conditionalExpression ? expression1 : expression2
The conditional operator works as follows:

  • The first expression conditionalExpression is evaluated first. This expression evaluates to 1 if it's true and evaluates to 0 if it's false.
  • If conditionalExpression is true, expression1 is evaluated.
  • If conditionalExpression is false, expression2 is evaluated.


C Programming Expression :

  1. In programming, an expression is any legal combination of symbols that represents a value. 
  2. C Programming Provides its own rules of Expression, whether it is legal expression or illegal expression. For example, in the C language x+5 is a legal expression.
  3. Every expression consists of at least one operand and can have one or more operators.
  4. Operands are values and Operators are symbols that represent particular actions.



C Programming code gets compiled firstly before execution. In the different phases of compiler, c programming expression is checked for its validity.
ExpressionsValidity
a + bExpression is valid since it contain + operator which is binary operator
+ + a + bInvalid Expression


C Programming supports wide range of operators. While Solving the Expression we must follow some rules
While solving the expression [ a + b *c ] , we should first perform Multiplication Operation and then Addition, similarly in order to solve such complicated expression you should have hands on Operator Precedence and Associativity of Operators.

Associativity

It represents which operator should be evaluated first if an expression is containing more than one operator with same priority.
PrecedencePriority Of Operator

Operator precedence & associativity table

Operator precedence & associativity are listed in the following table and this table is summarized in decreasing Order of priority i.e topmost operator has highest priority and bottommost operator has Lowest Priority.
Operator
Description
Associativity
( )
[ ]


.
->
++ –
Parentheses (function call) (see Note 1)
Brackets (array subscript)
Member selection via object name
Member selection via pointer
Postfix increment/decrement (see Note 2)
left-to-right
++ –
+ –
! ~
(type)
*
&
sizeof
Prefix increment/decrement
Unary plus/minus
Logical negation/bitwise complement
Cast (convert value to temporary value of type)
Dereference
Address (of operand)
Determine size in bytes on this implementation
right-to-left
*  /  %Multiplication/division/modulusleft-to-right
+  –Addition/subtractionleft-to-right
<<  >>Bitwise shift left, Bitwise shift rightleft-to-right
<  <=
>  >=
Relational less than/less than or equal to
Relational greater than/greater than or equal to
left-to-right
==  !=Relational is equal to/is not equal toleft-to-right
&Bitwise ANDleft-to-right
^Bitwise exclusive ORleft-to-right
|Bitwise inclusive ORleft-to-right
&&Logical ANDleft-to-right
| |Logical ORleft-to-right
? :Ternary conditionalright-to-left
=
+=  -=
*=  /=
%=  &=
^=  |=
<<=  >>=
Assignment
Addition/subtraction assignment
Multiplication/division assignment
Modulus/bitwise AND assignment
Bitwise exclusive/inclusive OR assignment
Bitwise shift left/right assignment
right-to-left
,
Comma (separate expressions)left-to-right



C Input Output (I/O)

C programming has several in-built library functions to perform input and output tasks.
Two commonly used functions for I/O (Input/Output) are printf() and scanf().
The scanf() function reads formatted input from standard input (keyboard) whereas the printf() function sends formatted output to the standard output (screen).

getchar() & putchar() functions

The getchar() function reads a character from the terminal and returns it as an integer. This function reads only single character at a time. You can use this method in the loop in case you want to read more than one characters. The putchar() function prints the character passed to it on the screen and returns the same character. This function puts only single character at a time. In case you want to display more than one characters, use putchar() method in the loop.

gets() & puts() functions

The gets() function reads a line from stdin into the buffer pointed to by s until either a terminating newline or EOF (end of file) occurs. The puts() function writes the string s and a trailing newline to stdout..

Difference between scanf() and gets()

The main difference between these two functions is that scanf() stops reading characters when it encounters a space, but gets() reads space as character too.
If you enter name as Study Tonight using scanf() it will only read and store Study and will leave the part after space. But gets() function will read it completely.



If the condition is "true" statement block will be executed, if condition is "false" then statement block will not be executed.
In this section we are discuss about if-then (if), if-then-else (if else), and switch statement. In C language there are three types of decision making statement.
  • if
  • if-else
  • switch

if-then Statement

if-then is most basic statement of Decision making statement. It tells to program to execute a certain part of code only if particular condition is true.

Syntax

if(condition)
{
.......
.......
}


else

It is a keyword, by using this keyword we can create a alternative block for "if" part. Using else is always optional i.e, it is recommended to use when we are having alternate block of condition.
In any program among if and else only one block will be executed. When if condition is false then else part will be executed, if part is executed then automatically else part will be ignored.

Syntax

if(condition)
{
........
statements
........
}
else
{
........
statements
........
}


Switch Statement

A switch statement work with byte, short, char and int primitive data type, it also works with enumerated types and string.
switch case

Syntax

switch(expression/variable)
{
case  value:
//statements
// any number of case statements
break;  //optional
default: //optional
//statements
}

Rules for apply switch

  1. With switch statement use only byte, short, int, char data type.
  2. You can use any number of case statements within a switch.
  3. Value for a case must be same as the variable in switch .

Limitations of switch

Logical operators cannot be used with switch statement. For instance

Example

case k>=20: //is not allowed
Switch case variables can have only int and char data type. So float data type is not allowed.

Syntax

switch(ch) 
{ 
case1:   
statement 1; 
break; 
case2: 
statement 2; 
break; 
} 
In this ch can be integer or char and cannot be float or any other data type.

Example of Switch case

#include<stdio.h>
#include<conio.h>

void main()
{
int ch;
clrscr();
printf("Enter any number (1 to 7)");
scanf("%d",&ch);
switch(ch)
{
case  1:
printf("Today is Monday");
break;
case  2:
printf("Today is Tuesday");
break;
case  3:
printf("Today is Wednesday");
break;
case  4:
printf("Today is Thursday");
break;
case  5:
printf("Today is Friday");
break;
case  6:
printf("Today is Saturday");
break;
case  7:
printf("Today is Sunday");
break;
default:
printf("Only enter value 1 to 7");
}
getch();
}

Output

Enter any number (1 to 7): 5
Today is Friday

Looping

Loops provide a way to repeat commands and control how many times they are repeated. C provides a number of looping way.

while loop

The most basic loop in C is the while loop.A while statement is like a repeating if statement. Like an If statement, if the test condition is true: the statments get executed. The difference is that after the statements have been executed, the test condition is checked again. If it is still true the statements get executed again.This cycle repeats until the test condition evaluates to false.
Basic syntax of while loop is as follows:
Show Example
while ( expression )
{
   Single statement 
   or
   Block of statements;
}

for loop

for loop is similar to while, it's just written differently. for statements are often used to proccess lists such a range of numbers:
Basic syntax of for loop is as follows:
Show Example
for( expression1; expression2; expression3)
{
   Single statement
   or
   Block of statements;
}

In the above syntax:
  • expression1 - Initialisese variables.
  • expression2 - Condtional expression, as long as this condition is true, loop will keep executing.
  • expression3 - expression3 is the modifier which may be simple increment of a variable.

do...while loop

do ... while is just like a while loop except that the test condition is checked at the end of the loop rather than the start. This has the effect that the content of the loop are always executed at least once.
Basic syntax of do...while loop is as follows:
Show Example
do
{
   Single statement
   or
   Block of statements;
}while(expression);

break and continue statements

C provides two commands to control how we loop:
  • break -- exit form loop or switch.
  • continue -- skip 1 iteration of loop.
You already have seen example of using break statement. Here is an example showing usage of continue statement.
#include 

main()
{
    int i;
    int j = 10;

    for( i = 0; i <= j; i ++ )
    {
       if( i == 5 )
       {
          continue;
       }
       printf("Hello %d\n", i );
    }
}
This will produce following output:
Hello 0
Hello 1
Hello 2
Hello 3
Hello 4
Hello 6
Hello 7
Hello 8
Hello 9
Hello 10

ARRAY
An array is a collection of data that holds fixed number of values of same type.

How to declare an array in C?

data_type array_name[array_size];
For example,
float mark[5];
Here, we declared an array, mark, of floating-point type and size 5. Meaning, it can hold 5 floating-point values.

Elements of an Array and How to access them?

You can access elements of an array by indices.
Suppose you declared an array mark as above. The first element is mark[0], second element is mark[1] and so on.
C Array declaration

Few key notes:

  • Arrays have 0 as the first index not 1. In this example, mark[0]
  • If the size of an array is n, to access the last element, (n-1) index is used. In this example, mark[4]
  • Suppose the starting address of mark[0] is 2120d. Then, the next address, a[1], will be 2124d, address of a[2] will be 2128d and so on. It's because the size of a float is 4 bytes.
It's possible to initialize an array during declaration. For example,
int mark[5] = {19, 10, 8, 17, 9};
Another method to initialize array during declaration:
int mark[] = {19, 10, 8, 17, 9};

Initialize an array in C programming
Here,
mark[0] is equal to 19
mark[1] is equal to 10
mark[2] is equal to 8
mark[3] is equal to 17
mark[4] is equal to 9

How to insert and print array elements?

int mark[5] = {19, 10, 8, 17, 9}

// insert different value to third element
mark[3] = 9;

// take input from the user and insert in third element
​scanf("%d", &mark[2]);

// take input from the user and insert in (i+1)th element
scanf("%d", &mark[i]);

// print first element of an array
printf("%d", mark[0]);

// print ith element of an array
printf("%d", mark[i-1]);
Array is a collection of variables belongings to the same data type

1. ONE DIMENSIONAL ARRAY IN C:

Syntax : data-type arr_name[array_size];
The string in C programming language is actually a one-dimensional array of characters which is terminated by a null character '\0'. Thus a null-terminated string contains the characters that comprise the string followed by a null.
In C programming, array of characters is called a string.
S.N. Function & Purpose
1 strcpy(s1, s2); Copies string s2 into string s1. 
2 strcat(s1, s2); Concatenates string s2 onto the end of string s1.
 3 strlen(s1); Returns the length of string s1. 
4 strcmp(s1, s2); Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2.
 5 strchr(s1, ch); Returns a pointer to the first occurrence of character ch in string s1. 
6 strstr(s1, s2); Returns a pointer to the first occurrence of string s2 in string s1.

Type Conversion in C


A type cast is basically a conversion from one type to another. There are two types of type conversion:
  1. Implicit Type Conversion Also known as ‘automatic type conversion’.
    • Done by the compiler on its own, without any external trigger from the user.
    • Generally takes place when in an expression more than one data type is present. In such condition type conversion (type promotion) takes place to avoid lose of data.
    • All the data types of the variables are upgraded to the data type of the variable with largest data type.
          
             bool -> char -> short int -> int -> 
             unsigned int -> long -> unsigned -> 
             long long -> float -> double -> long double
      
    • It is possible for implicit conversions to lose information, signs can be lost (when signed is implicitly converted to unsigned), and overflow can occur (when long long is implicitly converted to float).
    Example of Type Implicit Conversion:
    // An example of implicit conversion
    #include<stdio.h>
    int main()
    {
        int x = 10;    // integer x
        char y = 'a'// character c
     
        // y implicitly converted to int. ASCII
        // value of 'a' is 97
        x = x + y;
        
        // x is implicitly converted to float
        float z = x + 1.0;
     
        printf("x = %d, z = %f", x, z);
        return 0;
    }
    Output:
    x = 107, z = 108.000000
  1. Explicit Type Conversion– This process is also called type casting and it is user defined. Here the user can type cast the result to make it of a particular data type.
    The syntax in C:
    (type) expression
    Type indicated the data type to which the final result is converted.
    // C program to demonstrate explicit type casting
    #include<stdio.h>
     
    int main()
    {
        double x = 1.2;
     
        // Explicit conversion from double to int
        int sum = (int)x + 1;
     
        printf("sum = %d", sum);
     
        return 0;
    }
    Output:
    sum = 2
    Advantages of Type Conversion
    • This is done to take advantage of certain features of type hierarchies or type representations.
    • It helps us to compute expressions containing variables of different data types.
A function that calls itself is known as a recursive function. And, this technique is known as recursion.

Example: Sum of Natural Numbers Using Recursion

#include <stdio.h>
int sum(int n);

int main()
{
    int number, result;

    printf("Enter a positive integer: ");
    scanf("%d", &number);

    result = sum(number);

    printf("sum=%d", result);
}

int sum(int num)
{
    if (num!=0)
        return num + sum(num-1); // sum() function calls itself
    else
        return num;
}
Output

Enter a positive integer:
3
6

Advantages and Disadvantages of Recursion

Recursion makes program elegant and cleaner. All algorithms can be defined recursively which makes it easier to visualize and prove. 

If the speed of the program is vital then, you should avoid using recursion. Recursions use more memory and are generally slow. Instead, you can use loop.

C – Type Qualifiers

TYPES OF C TYPE QUALIFIERS:

There are two types of qualifiers available in C language. They are,
  1. const
  2. volatile

1. CONST KEYWORD:

  • Constants are also like normal variables. But, only difference is, their values can’t be modified by the program once they are defined.
  • They refer to fixed values. They are also called as literals.
  • They may be belonging to any of the data type.

  • Syntax:const data_type variable_name; (or) const data_type *variable_name;

2. VOLATILE KEYWORD:

  • When a variable is defined as volatile, the program may not change the value of the variable explicitly.
  • But, these variable values might keep on changing without any explicit assignment by the program. These types of qualifiers are called volatile.
  • For example, if global variable’s address is passed to clock routine of the operating system to store the system time, the value in this address keep on changing without any assignment by the program. These variables are named as volatile variable.
  • Syntax:
    volatile data_type variable_name; (or) volatile data_type *variable_name;
























Pointers are used in C program to access the memory and manipulate the address.

Address in C

Before you get into the concept of pointers, let's first get familiar with address in C.
If you have a variable var in your program, &var will give you its address in the memory, where & is commonly called the reference operator.
You must have seen this notation while using scanf() function. It was used in the function to store the user inputted value in the address of var.
scanf("%d", &var);
/* Example to demonstrate use of reference operator in C programming. */
#include <stdio.h>
int main()
{
  int var = 5;
  printf("Value: %d\n", var);
  printf("Address: %u", &var);  //Notice, the ampersand(&) before var.
  return 0;
}
Output
Value: 5 
Address: 2686778
Note: You may obtain different value of address while using this code.
In above source code, value 5 is stored in the memory location 2686778. var is just the name given to that location.

Pointer variables

In C, there is a special variable that stores just the address of another variable. It is called Pointer variable or, simply, a pointer.
Declaration of Pointer
data_type* pointer_variable_name;
int* p;
Above statement defines, p as pointer variable of type int.


https://www.programiz.com/c-programming/c-data-types  (Data Types Neat Explanation).
http://www.tutorialspoint.com/ansi_c/c_control_statements.htm
http://fresh2refresh.com/c-programming/c-function/
http://cs-fundamentals.com/c-programming/storage-classes-in-c-and-storage-class-specifiers.php


Image result
OperatorPriorityAssociativity
{}, (), []1Left to right
++, --, !2Right to left
*, /, %3Left to right
+, -4Left to right
<, <=, >, >=, ==, !=5Left to right
&&6Left to right
||7Left to right
?:8Right to left
=, +=, -=, *=, /=, %=9Right to left

Comments

Popular posts from this blog