Skip to main content

C


Data_type: 
Data type is storage representation of different data_type is different in memory.
Data type specify how, we enter data into our program and what of data of data we enter.   

Char: Storing single character.
int : Represent number
float: number with a fraction part
double:Double precision floating point value
Void: Special purpose type without any value 

char/signed char 1_size -128 to 127
unsigned char    1_size 0 to 255

int/signed int   4_size -32768 to 32767

Unsigned int     4_size 0 to 65535

Short int               -128 to 127

un short int            0 to 255

float            4_byte_size 3.4E -38 to 3.4E 38 

double           8_byte_size 1.7E -308 to 1.7E 308

long double      10byte_size 3.4E-4932 to 3.4E4932




Storage class:-
Storage classes are describe about the features of variable/function and include the scope,visible and lifetime of a variable.
  • Auto
  • Register
  • static
  • extern
(1)Auto:- auto is declared within a block/function.
          only accessed within a block.
main()
{
  int detail;
  auto int detail;
}
auto num=10;
{
   auto num=60;
   printf("%d",num);
}
 printf("%d",num);  ///Output are 60 10
}
(2)Register:-
register and auto almost same.
  • register is store variable in microprocessor in free register available.
  • register is variable that declared i,e not save in RAM that save on register, because of fast data access.
fun()
{
 register int a;

or register int a;

#include<stdio.h>

main()
{
    register int a;
    printf("%d",a);
} //////o/p are 7869_garbage_value

(3)static:-
  • Both used in local as well as global.
  • static storage class is tell to compiler that variable declared only one time and destroy end of program. 
  • Static value are constant.
func()
{
     static int a;///local
}
static int a=50;  ////global
main()
{
}
  func()
{
}  
//Program about static

#include<stdio.h>
main()
{
    check();
    check();
    check();
    return 0;
}
void check()
{
    static int c=0;
    printf("%d",c);
    c=c+5;
}//////o/p=0 5 10

(4)extern:-

  • extern are similar like globle.
  • extern storage class can be declared outside all function or inside function using extern keyword.
  • extern is used multiple files.
1)extern int=10; //globle
 main()
 {
 }
 func()
{
}
2)extern int a;
   main()
   {
    }
3)Multiple files are using
file_1 ===int a=10;
               main()
file_2:-
extern int a; ///multiple file
 main()
{
   func()
}
//Program about extern
#include<stdio.h>
void check();
extern int a=50;
main()
{
      a=a+4;
      check();
      return 0;
}
void check()
{
    ++a;
    printf("a=%d",a);
}/////o/p=10


-:Compilation Process:-
4-stages: Preprocessor-compiler-Assembler-Linker

(1)Preprocessor::is a directive that 1_removed comments 2_the include header file of source code 3_replace macro name with code(.c-code).


(2)Compiler::is coverts human readable code into assembler code.

push obj
pop obj.osp
mov exc offset

(3)Assemble:: is covert assemble code into object code.
10100110
10100010
01101010
11001100

(4)Linker:: is our object code and generate .exc file.


Developing a c-lang:-
Prog_creation---Prog_compilation--Prog_execution
vim.tiny prog.c
cc prog.c
./a.out

Keywords: Reserved words(32)
keywords certain words are reserved for doing specific work.
char_int_float_double_long
signed_unsigned_short
auto_register_static_extern
if_else_while_for
do_switch_break_case_return_continue
default_void_goto_const
sizeof_typeof
volatile_union_enum_struct_static

ASCII American standard code for Information Interchange
A-Z___65-90
a-z___97-122
0-9___48-57
;_semicoln__59

%c single character
%d decimal integer%f floating decimal integer
%u unsigned decimal integer
%o unsigned octal integer 
%x %X unsigned hexadecimal integer


Operator::
 Operator specifies an operation to be performed that yield a value.

{1}Arithmetic:used for numeric calculation.

2types__Unary__Binary
  unary_required only one operand.(+x,+y)
  binary_required two operand.(+ - * / %)

{2}Assignment:value can be stored a variable, operand on Left_hand_side should be a variable.
x=8; //8 is assigned to x
s=x+y-2;//value of exp is assigned to s

{3}Increment & Decrement: both are unary operator(they operand single operand)

++x___equivalent to x=x+1
--x___equivalent to x=x-1
 2types_Prefix_postfix  

{4} Relational: used compare value of two expression.

< less than
<= less tham or equal to
== equal to
!= not equal to
>  greater than
>= greater than or equal to

{5}Logical or Boolean: combine two or more expression is termed.

&&__AND like *
++__OR  like +
!__NOT like just opposite

{6}Conditional: is a Ternary operator(? and :) which req. 3 expr. test exp ? exp 1 : exp 2

a>b? a:b 

{7}Comma: (,) is used to permit diff exp appear in situation where  only one exp would be used. 
main()
{
  int a,b,c,sum;
  sum=(a=8,b=7,c=7,sum=a+b+c);
  printf("Sum are:%sum,sum);
}

{8} sizeof: is unary operator that gives the size of its operand in term of byte.

main()

  int var;
  prinft("Size of int: %d,sizeof(int));
  printf("Size of float:%f,sizeof(float));
}

{9}Bitwise: operate integer only and they used for operator        individual bits.


&  Bitwise AND

|  Bitwisw OR
~  one's complement
<< left shift
>> right shift
^  bitwisw xor
  
Precedence & Associativity of Operator:
1) () [] ._dot arrow         L_to_R
2) + - ++ -- ! ~ * & sizeof  R_to_L
3) * / %                     L_to_R
4)+ -                        L_to_R
5)<< >>
6)< <= > >=        
7)== != 
8)&
9)^
10)|
11)&&
12)||
13)?:_conditional R_to_L
14)= *= =* /= %= &= ^= |= <<= >>= R_to_L
15),_comma_smallest               L_to_R


::Control_Statement::                 
1)Selection if..else switch           
2)Iteration while do..while for       
3)Jump      break continue goto Switch

(1)If..else : Bi-directional selection control statement,which to take one of two possible action.

if(expression)

     statement 1;
else
     statement 2;


//Program biggest number
#include<stdio.h>
main()
{
  int a,b;
  printf("Enter 2-number:");
  scanf("%d %d",&a,&b);

  if(a>b)

     printf("Biggest number:%d",a);
  else
     printf("Biggest number:%d",b);
}

2)Nested of if...else:
  
if(expression 1)
{
  if(expression 2)
  statement A1
else
  statement A2
}
else
{
 if(expression)
  statement 1
 else
  statement 2
}
             
Loop's:-

1)while:  if..else also can have either single statement.
          while(Expression)
               statement
or:-
while(expression)
{
  statement
  statement
}


//Program num from 1 to 10
#include<stdio.h>
main()
{
  int i=1;
  while(i<=10)
  {
     printf("%d",i);
  }
}//end

(2)do..while: is also used for looping.


 do
 {
   statement 
   statement
 }
  while(expression)
   
//Prog num 1 to 10 using do..while
main()

  int i=1;
  do
  {
      print("%d",i);
      i=i+1;
  }
}//end

(3)For:

  • for statement has 3 three expression.
  • semicolons are used to separation these statement.
for(expression_1; expression_2; expression_3)
   statement

expression_1____Initialization

expression_2____Test(body_of_loop)
expression_3____Update

//prog to print 1 to 10 using for loop
#include<stdio.h>
main()
{
   int i;
   for(i=1; i<=10; i++)
       printf("%d",i);
}//end 

Jump:-

(1)break:: used inside loop and switch statement

//prog to understand use of break
main()
{
        int n;
        for(n=1;n<=5;n++)
        {
               if(n==3)
                    break;
                printf("Number=%d",n);
        }
}///end

(2)continue: when we want go to next iteration of the loop after skipping statement of loop.
//prog to understand continue statement
#include<stdio.h>
main()
{
    int n;
    for(n=1;n<=5;n++)
    {
     if(n==3)
        continue;
        printf(Number:%d",n);
    }
    printf("out of loop");
}//end

Diff_between_break_and_continue
>>break is encountered the loop terminated the control is transfer to the next side.
>>continues statement encountered the loop is not terminated and the next iteration of loop.


(3)goto:: unconditional control statement that transfer the flow of control to another part.

goto label;

     -------
     -------
label:
     statement 
     ---------
     ---------

//Prog find no whether old or even
#include<stdio.h>
main()
{
  int n;
  printf("Enter any number:");
  scanf("%d",&n);
 if(n%2==0)
   goto even;
 else
   goto old;
even:
  printf("Number are even");
old:
  printf("Number are Old");
end:
}//end

(4) Switch():: multi-way conditional statement which help us to make choice among a number of alternative.

switch(expression)
{
 case const_1:
   statement
 case count_2:
   statement 
 ---------
 ---------
 case count_n:
   statement
            
 default:
    statement
}
//Program to understand switch statement
#include<stdio.h>
main()
{
  int choice;
  printf("Enter your choice:");
  scanf("%d",&choice);
  
  switch(choice)
  {  
    case 1:
      printf("First");
    case 2:
      printf("Second:);
    case 3:
      printf("Third");
   
    default:
       printf("Out of choice");
  }
}///End
  
::Function:: 
  • Function is self-contained subprogram that perform some specific well defined task.
  • Function are divided into 2-subprogram and them solved.
  • avoid repetition of that code.
  • Program become easily under-stable,modified and easy to debug and test.
2-types:: 1_Library_function  
          2_User-defined_function

(1)Library_function:
C-has facility to provide library function for performing some operation.
That function present C-library and they are Pre-defind.
sqrt()

strlen()
strcmp()

///Program to find square root of number.
#include<stdio.h>
#include<math.h>
main()
{
  double num,sq;
  printf("Enter any number:");
  scanf("%f",&num);

  sq=sqrt(num);

  printf("Square root are:%f",sq);
}///end

(2)User_defined Function::
Program can create their own funtion for performing any specific task of program.

3_Step's:
  1. Function definition
  2. Function declaration
  3. Function Call
//Program to find sum of two numbers
#include<stdio.h>
main()
{
  int a,b,s;
  printf("Enter two number's:);
  scanf("%d %d",&a,&b);
  
  s=sum(a,b);  //function call
  printf("Addition are:%d",s);
}
int sum(int x,int y)  //function definition
{
  int s;
  s=x+y;
  return s;
}

Function into 4_types depending on return value & Argument:
  1. Function with No-argument and no return value.
  2. Function with No-argument but return value.
  3. Function with argument but no return value.
  4. Function with argument and return value.
(1)Function with no argument and no return value:-

void func(void);
main(void)
{
  ------
  func()
  ------
}
void func(void)
{
  -------
  -------
}
///Program about no argument and no return value.
#include<stdio.h>
void displaymenu(void);
main()
{
 int choice;
 displaymenu()
 printf("Enter your choice:");
 scanf("%d",&choice);
}
void displaymenu()
{
  printf("1. Create database:");
  printf("2. Intert new record:");
  printf("3. Modify a record:");
  printf("4. Exit");
}///end 

(2)Function with no argument but return value:
Function not received only arguments but they can return a value.

int func(void);
main()
{
   int x;
   -------
   -------
   r=func();
   -------
   -------
   return;
}  int func(void)
{
  ------
  ------
  return expression;
}///End
///Program use function of no argument bit return value.
#include<stdio.h>
int func(void);
main()
{
  printf("%d",func());
  return 0;
}
int func(void) //return sum of square of all add number 1 to 25
{
   int num,s=0;
   for(num=1; num<=25; num++)
   {
      if(sum%2!=0)
      {
        s=s+num*num;
      }
  }
}//End

(3)Function with argument but no return value:-These type of function have argument so calling function can send data to called function but func does not return only value.
void func(int, int)
int main()
{
 ---
 func(a,b);
 ----
 ----
 return 0;
}int func(int c,int d)
{
 -----
 -----
}
(4)
Function with argument and return value:-
These type of function argument, so the ca;;ing function cam send data to called function.

int func(int,int)
main()

 int r;
 -----
 -----
 r=func(a,b);
 ------
 ------
 return 0;
}
int func(int a,int b)
{
  -----
  -----
  return expression;
}

Recursion:
Recursion is process of calling function itself, until some specific condition satisfied.

main()

{
  -----
  rec();
  -----
}
void rec()
{
  -----
  rec();///recursion call
}
///Program about factorial number using recursion
#include<stdio.h>
long int fact(int n);
main()
{
  int num;
  printf("Enter a number:");
  scanf("%d",&num);
  
  printf("Factorial number are: %d", fact(num));
}
long int fact(int n)
{
  if(num==0)
     return 1;
  
  return(n*fact(n-1));
}//end


::Array::
  • Array is ability to use single variable to represent a collection of item's.
  • Array is variable that hold multiple element which has some data type.
  1. Declaration 
  2. Accessing 
  3. Processing
  4. Initialization
{1}Declaration:-Array should also declared before they are used in program.
data_type array_name [size];

int age[50];

float salary[15];
char grade[50];

{2}Accessing:-accessed by specifying the array name followed by subscript in breaket.

int arr[5];//Size of array are five can hold five element

{3)Processing:- we generally use a function loop and loop variable is used at the place of subscript.

[a]Reading value in arr: 

 for(i=0;i<10;i++)
   scanf("%d",&arr[i]);

[2]Display

  for(i=0;i<10;i--)    scanf("%d",arr[i]);

[3]Adding

  sum=0;
  for(i=0;i<10;i++)
    sum+=arr[i];

{4}Initialization:- After declaration, the element of local array have garbage value while element of global and static array are automatically initialize to zero.

data_type arr_name[size]={val_1, val_2,---val_n};


// Prog to input value into array and display them.
#include<stdio.h>
main()
{
  int arr[5],i;
  for(i=0; i<5;i++) 
  {
      printf("Enter a value for array:");
      scanf("%d"&arr[i]);
  } 
printf("Enter array element are:");
for(i=0;i<5;i++)
   printf("%d",arr[i]);
}///end



::Pointer:: 
  • Pointer is a user-defined data_type which create special type of variable which can hold the address of data_type like char,int,float,double etc.
  • Pointer are used in c-prog to access the memory and manipulate the address.

BENEFIT'S:
(1)It's is allow a function to call to reference.
(2)Return more than one value from function.
(3)Pass many more conveniently from one func to another.
(4)Allow dynamic memory allocation in C_language.

RULE'S:

(1)Pointer variable can be assigned with NULL.
(2)Pointer variable can be assigned value of another pointer value.
(3)Pointer variable should not be modified by addition, multiplication,multiply by other value.

(1)Memory::

{1}Address of these byte start from zero and address of last byte is one less than size of memory.
{2}Data_type of variable also mentioned so that compiler know how much space need to be reseved.

int__4-reserve consecutive byte from memory.

int age[5];

(2)Address Operator::
C provide address operator(&) which return the address, when placed before it. 

//Program of print address of variable

#include<stdio.h>
main()
{
  int age=30;
  float salary=1500;
  printf("Address of age:%d",&age);
  Printf("Address of salary:%d",&salary);
}//End

{3}Pointer_variable:-
A pointer is a variable that store memory address.
Like all other variable also has a name has declared and occupies some space in memory.

[a]Declaration: Pointer variable should also declared before used.  
           Data_type *pname;
pname:-name of pointer
*_asterisk preceding, name inform the compiler that variable is declared in pointer. 

[b]Assigned_Address: Global & Local static pointer are automatically initialized to NULL, but when we declared a automatically pointer variable garbage value i,e may be Pointing anywhere in the memory. 
int *iptr, age=30;

double *dptr,x=1000;
iptr=&age;
dptr=&x;



Value of pointer iptr is 6080 which is address of variable age and value of pointer dptr is 5060 which is address of variable of variable x.


*p1=9;   is equivalent to a=9

(*p1)++; is equivalent to a++
x*p2+10; is equivelent to x=x+10
printf("%d %d",*p1,*p2);
printf("%d %d",p1,p2);



::Pointer to Pointer::

The pointer variable takes some space in memory and hence,its has on address.

We can store the address of a pointer variable in some other variable which known as pointer_to_pointer.
data_type **pptr;
int a=5;

int **pa=&a;
int **ppa=&a;


//Program of pointer to pointer
#include<stdio.h>
main()
{
 int a=5;
 int *pa;
 int **ppa;
 pa=&a;
 ppa=&pa;

printf("Address of a=%p",&a);
printf("Value of pa=address of a=%p,pa);
printf("Value of *pa=value of  a=%d"*pa);
printf("address of pa=%p",&pa);

printf("Value of ppa== address of pa:%p",ppa);
printf("Value of *ppa==value of pa=%p,**ppa);
}///END
Pointer to array:-  We had a pointer to 0th element of are,we can also declared a pointer that can point to whole array instead of only one element of array.
int (*ptr)[10];
           
//Prog to understand diff between pointer to array and pointer to array of integer.
#include<stdio.h>
main()
{
 int *p;        //can point to integer
 int (*ptr)[5]; //can point array of 5_element
 int arr[5];

 p=arr;     //point to 0th element of array
 ptr=&arr;  //point to whole array 
 printf("P=%p, ptr=%p, p,ptr);
 p++;
 ptr++;
 printf("P=%p, ptr=%p, p,p++);
}//END

::Void Pointer::
A Pointer to void is generic pointer that can point to any data_type, we assign address of any data_type to void pointer can be assigned, to pointer of any data_type without any explicit casts.

Void *vpt;

>>Void is a keyword and vpt is declared as pointer void type.

int i=2. *ip=&i;
float f=2.3, *fp=&f;
double d;
void *vp;

///Dereferening void pointer#include<stdio.h>
main()
{
 int a=3;
 float b=3.4, *fp=&b;
 void *vp;
 printf("Value of a=%d", *(int *)vp);

 *(int *)vp=12;
 printf("Value of a=%d",*(int *)vp);

 vp=fp;
 printf("Value of b=%f",*(float )vp);
}//END

Dynamic memory Allocation(DMA)::
  • Allocated the memory run_time is called DMA.
  • Pointer play an important role in dynamic memory allocation because we can access the dynamically allocated memory only through pointer.
  1. malloc()
  2. calloc()
  3. realloc()
  4. free()
{1}malloc():- used to allocate memory dynamically. The argument size specified the number of bytes to allocated.
void *malloc(size_t size);

ptr=(data_type *)malloc(specified_size);


ptr=pointer of type datatypee

size=specified size--byte required to allocated.
(data_type *)___typecast pointer returned to malloc()

int *ptr;

ptr=(int*)malloc(12);

allocate 12-contiguous byte of memory space.

Space hole 3-register,the allocated memory contained garbage value.

//Program to understand dynamic memory allocation
#include<stdio.h>
main()
{
 int *p,num;
 printf("Enter the number of integer to entered:");
 scanf("%d",&num);

 p=(int*) malloc(num *sizeof(num));

 if(p==num)
 {  
  printf("Memory not available");
  exit(0);
 } for(i=0;i<num;i++) ///First enter variable that allocate memory
 {
    printf("Enter an integer:");
    scanf("%d",p+i);
 }
 for(i=0; i<num; i++)   ///print all variable
 {
    printf("%d",*(p+i));
 }
}///END

{2}calloc():- is used allocated multiple memory block of memory.
                     
 >>Similar to malloc() tajke different 
1)take two argument
2)one specific the size of block
void *calloc(size_t n,size_t size);
e.g:- ptr=(int *)calloc(5, sizeof(int));

      
5-block of memory,each 4-bytes, starting address is started in pointer.

 {3}realloc():-
>>The function realloc() is used to change the size of memory block.

>>It alters the memory block without losing the old data, this is reallocation of memory.
>>Take 2_argument's
void *realloc(void *ptr, size_t new_size);

e.g:-ptr=(int*) realloc(ptr, new_size);


{4}free():-
(1)Free() is used to release the memory space allocated dynamic.
(2)DAM is not automatically released, it will exit till that memory.

void free(void *p);

e.g:-
1) free(ptr);
2) void func ()
   {
        int *ptr;
        ptr=(int*) malloc(10*sizeof(int));
   }

::String::
  • String are defined as array of character.
  • Different between array and string is the terminated with special character('\0').
{1}Declaration:-    Char str_name [size];

///Prog to print character and address of character
#include<stdio.h>
main()
{
   char str[]="India";
   int i;
   for(i=0;str[i]='\0'; i++);
   {
     printf("Character are:%c",str[i]);
     printf("Address are  :%d",&str[i]);
}//END

String_Library_function:-
Library function are used manipulate string and prototype of function header file. like <string.>
strlen()
strcat()
strcpy()
strcmp()

{1}strlen(): return length of string. >>Number of character excluding the termination null character.
size_t strlen(char const *string);

//Program about strlen()
#include<stdio.h>
#include<string.h>
main()
{
    char str[40];
    printf("Enter a string:");
    gets(str);
 
    printf("Length of string are:%u",strlen(str));
}//END

///Array Version
main()
{
   }
unsigned int mystr(char str[])
{
   int i;
   while(str[i]!='\0)
      i++;
   return i;
}END

{2}Strcmp():- Function are used for lexicographic comparison of two string. Two_String return value 0. otherwise return non_zero value.
int strcmp (const char *s1, const char *s2);

//Program about strcmp()
#include<stdio.h>
main()
{
   char s1[10],s1[10];
   printf("Enter 1st string:");
   gets(s1);
   printf("Enter 2rd string:");
  gets(s2);

  if((strcmp(s1,s2)==0)
        printf("String are same");
  else
        printf("Both are not same");
}

{3}strcpy():- strcmp is used for copying one string to another string.

///Program about strcpy()
#include<stdio.h>
main()
{
  char s1[10], s2[10];
  printf("ENter a string:");
  gets(s2);
  strcpy(s1,s2);

  printf("First string %s, second string:%s", s1,s2);
  strcpy(s1,dehli);
  strcpy(s2, Bangalore);
  printf("First string %s, second string:%s",s1,s2);
  return 0;
}//END

{4}Strcat():-used to append a copy of string, at the end of other string.(like 2 word added)
    e.g: s1="Mohit" s2="Bawankar" ----MohitBawankar

//Program about strcat()
#include<stdio.h>
main()
{
  char s1[10],s2[10];
  printf("Enter two string:");
  gets(s1);
  gets(s2);
  strcat(s1,s2);
  printf("First string:%s ,second string:%s", s1,s2);

  strcat(s1,"_one");
  printf("Now first string are:%s", s1);
}END

{5}StrStr:- used the locate the first occurrence of a substring in another string.

//Program about strstr()
#include<stdio.h>
main()
{
   char *ptr;
   ptr=strstr("Placement seection","cement");
   printf("%s",ptr);
}//END

::Structure::is user_defined data_type which store the different data_type variable together.

{1} Declaring: create a format that describes struct type characteristics of member.

struct tag_name
{
  data_type mem1;
  data_type mem2;
  - - - - -- - - - - -
 - - -- - - - - - - 
 data_type menN;
}

{2}Declaring struct variable:- Actually use of argument will be when declare variable.

struct student 
{
   char name[30];
   int rollno;
   float marks;
};stu1,stu2,stu3;/// Student is variable of struct student
                          ///declaring struct variable

{3}Initialization of struct variable:-Initialization struct variable similar to array.
struct student 

  char name[20];
  int rollno;
  float marks; 
}stu_1={"Mohit",25,98};//Initialization of variable
struct student stu_2={"Bawankar",33,25.57};
//float marks=99; ----It is Invalid

//Program to display the value of struct member
#include<stdio.h>
#include<string.h>
struct student
{
 char name[50];
 int rollno;
 float marks;
};
main()
{
  struct student stu_1={"Mohit",25,69.32};
  struct student stu_2,stu_3;

  strcpy(stu_2.name,"Bawankar");
  stu_2.rollno=26;
  stu_2.makrs=98;

  printf("Enter name, rollno and marks for student:");
  scanf("%s %d %f", stu_1.name, stu_1.rollno, stu_1.marks);

  printf()"%s %d %f", stu_2.name, stu_2.rollno, stu_2.marks);
}///END

Array of struct::

stu[0].name----stu[9].name
stu[0].rollno----stu[0].rollno
stu[0].marks---stu[0].marks

#include<stdio.h>
struct student
{
   char name[50];
   int rollno;
   float marks;
}
main()
{
    int i;
    struct student stuarr[10];
 
    for(i=0; i<10; i++)
   {
     printf("Enter name,rollno,marks:");
     scanf("%s %d %f",stuarr[i].name,stuarr[i].rollno,stuarr[i].marks);
    }
}///END

Pointer_To_structure:-
Pointer is variable which hold starting address of another variable of data_type like int, float,char.

struct student
{
   char name[20];
   int rollno;
   int marks;
};struct student stu, *ptr;    *ptr=pointer_variable

//Program to pointer to structure
#include<stdio.h>
struct student
{
   char name[50];
   int rollno;
   float marks;
};
main()
{
   struct student stu={"Mohit",  25, 65};
   struct student *ptr=&stu;
 
   printf("Name are:%s"    ,ptr->name);
   printf("Roll_no are:%d",ptr->rollno);
}//END

::UNION::

  • Union is derived data_type like Structure.
  • Union is user_defined data_type that enable store different data_type in same memory location.
  • Union variable then member can be accessed dot(.)
  • Pointer to union accessed using arrow(-->) 

union union_name
{
   data_type mem1;
   data_type mem2;
   --------------------
   --------------------
  data_type menN;
};

///Program to accessing union member
#include<stdio.h>
{
  union result
  {
    char grade;
    int marks;
     float per;
  }rec;
main()
{
    res.marks=90;
    printf("Marks are:%d", res.marks);
    res.grade='A';
    printf("Grade are:%c",res.grade);
    res.per=82.2;
    printf("Percentage are:%f",res.per);
}

::Enumeration::
Enumeration is user defind data_type which consists of integer constant.
Each integer constant is given a name.

enum identifier {val_1,val_2,- - - - val_N}; 

//Program about enumeration
#include<stdio.h>
enum rank{first=1, second, third};
main()
{
   enum rank, r;
   printf("Student A:%d" Rank", r+1);
   printf("Student B:%d" Rank",r+2)
}

::File::<stdlib.h>
Files are used to store data on memory device permanently and access whenever is required.

Buffer:: Buffer is area of memory, where the data is temporarily stored before being written to file.

Step's for File: 

  1. Open a file
  2. Read/write in file
  3. Close the file

fopen() and fclose used for opening and closing the files reps.

{1}Opening a file:-  FILE *fp;
File must be open before any I/O operation can be performance on that file.

 FILE *fopen (const char *file_name, const char *mode);

fopen----take 2_string as argument
first     ___Name of file to opends
second___Mode of decides (read,write,append)

FILE *fp1,*fp2;
fp1=fopen("myFile.txt","w");
fp2=fopen("YourFile","r");

{2}Closing a file:- int fclose(FILE *fptr);
  • File was opends using fopen() function must be closed,when no more function.
  • After closing the file, connection between file pointer and file is broken.
  • fptr__Free to be connected to some other file.
fptr:- free to be connected to some other file
e.g fclose(fptr1);
      fclose(fptr2);

Close_multiple_file fcloseall()
declation: int dcloseall(void);

{3}End_of_File(EOF): File reading function need to know file that can stop reading.



::Function used for file I/O::

Character I/O__ fgets(), fputs(), gets(), puts()
String I/O      __fgets() fputs
formatted I/O__fprints(), fscanf()
Record I/O    __fread(),fwrite()

{1}fputs():- Function write a character to specified file at the current file operation.

Declaration:- int fputs(int c, FILE *fptr);

//Program use of fputs()
#include<stdio.h>
#include<stdlib.h>
main()
{
  FILE *fptr;
  int ch;
  if((fptr=fopen("myfile","w")
  {
     printf("Error in opening file");
     exit(1);
  }
while(ch=getchar())!=EOF)
    fputs(ch,fptr);

fclose(fptr);
};/////END
 
{2}fgets(): Function are reads a single character from given file and increments the file position indicator.
int fgets(FILE *fptr);



::C_Preprocesssor::

  • Preprocessor is a directive to compiler to perform certain things before the actual compilation process begin's. 
  • Starting # are known as  preprocessor directive.
  • Preprocessor directive don't end with semicolon(;)

Advantage's:-
1) Enhances readability of program.
2)Program modification become easy.
3)Can be used for testing and debugging.

Preprocessor_Directive::12_Total
#define
#include
#if
#else
#elif
#endif
#ifdef
#ifndef
#error
#undef
#line
#pragma

Three_Operator:-
(1)Defined operator    defind()
(2)Stringizing         #
(3)Token passing       ##

(1)#define:- We have already used direction to define symbolic constant.

#define macro_name macro_expression;

#define TRUE 1
#define PI 3.14
#define False 0
#define max 100

//Program to show macro_expression.
#include<stdio.h>
#define Msg "My name is Mohit"
main()
{
  printf("Msg");
  return 0;
}

(2)Parameterized maco:- #define directive can be used macros with parameter.

#define macro_name (part1, part 2, --------)macro_expression;

e.g #define sum(x,y) ((x+y))
    #define PROD (x+y) ((x)-(y))

///Program to understand macro's with argument
#include<stdio.h>
#define sum(x,y) ((x)+(y))
#define PROD (x,y) ((x)-(y))
main()
{
  int m,n,i,j,a=5,b=3;
  float p,q;
  
  m=sum(a,b);
  n=PROD(a,b);
  i=sum(4,6);
  j=PROD(4,6);
  printf("m=%d, n=%d, i=%d, j=%d");
}//END
O/P: m=8, n=15, i=10, j=24

{3} Problem with macro's:-

#include<stdio.h>
#define PROD (x,y) x*y
main()
{
  printf("%d",PROD(2,4));
  printf("%d",PROD(3+5)(5+1));
}//END
O/P: 8,42


{4} Strinigizing Operator(#):-
Definition of macro, formal parameter occur's inside a string in macro_expression, then replaced by actual argument.

#define SHOW(var)
printf("Var: %d",var);

//Program to understand # operator
#include<stdio.h>
#define (var, format)
printf(#var "= % " # format "\n",var);
main()
{
 int x=9;
 float y=2.5;
 char z='$';

 SHOW(y,0.2f);
 SHOW(x,d);
 SHOW(z,c);
 SHOW(x*y,  0.2F);
}//END
o/p: x=9 y=2.50  z=$   x*y=22.50


{4} Token Passing Operator(##):-
Used to macro definition to concatenate 2-token into single taken.

//Program to understand ## Operator
#include<stdio.h>
#define PASTE(a,b) a##b
#define MARKS(subject) marks_##subject

main()
{
  int k1=14, k2=25;
  int mark_chem=89, mark_math=98;
  printf("%d %d", PASTE(k,2), PASTE(k,3));
}


(1) Armstrong number:- 
which sum of its digit to power of number of digit is equal to number.
153=1^3 + 5^3 + 3^3 =1+125+27= 153

#include<stdio.h>
main()
{
  int num, r, temp,sum=0;
  printf("Enter any number:");
  scanf("%d", num);
  
  temp=num;
  while(num!=0)
  {
    r=num%10;
    num=num/10;
  sum=sum+(r*r*r);
   }
   if(sum==temp)
     printf("Armstrong number");
   else
     printf("Not Armstrong number");
}///END

(2) Palindrome number:-
is that same digit reserved.(like Reserve of number)
e,g:- 245--542

#include<stdio.h>
main()
{
int num,temp,r,sum=0;
printf("Enter any number:");
scanf("%d",&num);
temp=sum;

while(num!=0)
{
   r=num%10;
   num=num/10;
  sum=sum*10+r;
}
if(sum==temp)
  printf("Palindrome number");
else
  printf("Not palindrome number");
}/END

{3}Strong number:- its is sum of factorial number of it's digit equal to number itself.
145=1! + 4! +5!
     =1+24+120
     =145

#include<stdio.h>
main()
{
  int num,i,fact,r,temp,sum=0;

  printf("Enter any number:");
  scanf("%d",&num);
  temp=num;
  
while(num)
{
  i=1,j=1;
  r=num%10;
  while(i<=r)
  {
    fact=fact*i;
    i++;
   }
  sum=sum+fact;
  num=num/10;
}
if(sum==temp)
  printf("Strong number");
else
  printf("Not strong number");
}//END

{4} Perfect number:- number factor's (other then itself) addup to number.
e.g:- 6=1+2+3
#include<stdio.h>
main()
{
  int num,i=1,sum=0;
  printf("Enter any number:");
  scanf("%d",&num);
  while(num>0)
  {
    if(num%i==0)
      sum=sum+i;
      i++;
  }
if(sum==num)
   printf("Perfect number");
 else
  printf("Not perfect number");
}//END

{5}Prime number:- A natural number is greater than one has not other divisor expect one(1). 
e.g:- 2,3,5,7,11

main()
{
  int num,i,count=0;
  printf("Enter any number:");
  scanf("%d",&num);

for(i=2; i<=num/2; i++)
{
 if(num%i==0)   //Condition for non_prime number
 {
count=1;
break;
 }
}
if(count==0)
   printf("Prime number");
else
  printf("Not Prime number");
}//END

{6}Fibonacci number:- A series of number in which each sequence number is sum of it's two previous number
fn=fn-2+fn-1
14_range= 0 1 1 2 3 5 8 13 

main()
{
  int num,first=0,second=1,next,i;
  printf("Enter a range:");
  scanf("%d",&num)
  
printf("Fibonacci series:");
for(i=0; i<num; i++) 
{
  if(i<=1)
  {
    next=1;
  }
  else
  {
    next=first+second; 
    first=second;
    second=next;
  }
  printf("%d",next);
 }//for
}//END

{7}Binary_to_Decimal:-  1010----10_o/p
main()
{
 int bn,r,j=1,dn=0;
 printf("Enter binary number:");
 scanf("%d",&bn);

 while(bn!=0)
 {
   r=num%10;
   dn=dn+r*j;
   j=j*2;
   bn=bn/10;
  }
 printf("Equivalent decimal number are:%d",dn);
}//END

{8} Add/sum of enter number:-
#include<stdio.h>
int mysum(int n);
main()
{
 int num;
 printf("Enter any number:");
 scanf("%d",&num);
}
int mysum(int n)
{
 int r,sum=0;
 while(n!=0)
 {
 r=n%10;
 sum=sum+r;
 n=n/10;
 return sum;
}

{9}Add 2_number without operator
#include<stdio.>
int mysum(int x,int y);
main()
{
 int a,b;
 printf("Enter 2_number");
 scnaf("%d %d",&a,&b);
 printf("Sum of number:%d",mysum(a,b));
}
int mysum(int x,int y)
{
  return printf("%c%c",x ' ',y,' ');
/////No_space
}

{10} Reverse string without using library:


(1) Find length of string  strlen=?
(2) No need tp print in reverse
i,e for(i=len-1;--;--)  

#include<stdio.h>
main()
{
 char str[50];
 int len=0,i;
 printf("Enter a string:");
 scanf("%s",str);

 len=strlen(str);
 for(len-1;i>=0;i--)
 {
    printf("%c",str[i]);
 }
}//END




Void Pointer:- is a pointer that has no-associated data_type with it. 

int a=10;
char b='x';
void *p=&a; ///void pointer hold address of int a
p=&b;           ///hold address of char 'b'

Dangling pointer:- is a pointer to memory location that been already deleted(free) is called.

main()
{
  int *ptr=(int *) malloc(sizeof(int));
  //Afrer below free call, ptr is dangling pointer
free(ptr);
   ///No more a dangling pointer
ptr=NULL:


NULL Pointer:- is a pointer which is pointing to nothing, In case, if don;t have address to be assigned to a pointer, then we can simply use NULL.

main()
{
  int *ptr=NULL; //Null pointer
  printf("Value of ptr:%u",ptr);
}
o/p: value of ptr 0

Wild Pointer:- has not been initialized to anything(not even NULL).
main()
{
  int *p;
  int x=10;
  ///P is not wild pointer now
  p=&x;
  
  printf("%u",ptr);
  printf("%u",*ptr);
}

Diff between struct and Union:-

  • Struct is user_defined data_type which can store different data_type together.
  • Union is special data_Type available in C_lang i,e storing different data_type in same memory number. 


Struct define a new_data_type with more then one member.
 struct [struct_name]
 {
     mem_1 define;
     mem_2 define;
 }

Union define as a structure.
  union [union_name]
  {
    mem_1 define;
    mem_2 define;
  }

Similarities of struct and union:-

Both are user_Defined data_type used to store data of difference types as a single_unit.
Both are supports only assignment (=) and sizeof operator.
._dot operator is used to accessing member.

//Program about struct
#include<stdio.h>
struct student 
{
  char name[50];
  int rollno;
};
main()
{
  struct student stu;
  print("Enter a name:"); 
  scanf("%s",name);
  printf("Enter employee number:");
  scanf("%d",&empno);
  
   ///Details of employeer 
  printf("Name of employee :%s",emp.name);
  printf("Number of employee:%d",emp.empno);
}///END
o/p:- Union is store data in same memory location.
>>max no of size is printed.
Mohit 
89
so output are:-
Mohit 
garbage_value


Struct size or Structure padding:- 
To find size of structure by sizeof_operator.
In struct, sometime sizeof struct is more than sizeof all struct because of structure padding.

//Prog about struct padding
#include<stdio.h>
struct stu
{
  int i;
  char ch;
  double d;
 }
main()
{
  struct stu A;
  printf("Size of all struct: %d",sizeof(A));
}///END
o/p: A is 16
1)int_4 char_1 double_8 total_13

2)struct actual size is 13 but 3-byte wasted
3)To avoid struct padding, we can use progma pack.

//Prog using pragma pack
#include<stdio.h>
#pragma pack(1)
struct s
{
  int i;
  char ch;
  double d;
};
main()
{
  struct s A;
  printf("Size of A is: %ld",sizeof(A));
}///END


Diff between call_by_value and call_by_reference:-

  • 2_method to pass the data into function in c-lang i,e : 1)call by value 2)call by reference
  • The parameter passed to function are called actual parameter.
  • Parameter received by function are formal parameter.



  • Call_by_value is copy of the value is passed into the function.
  • Call_by_reference is address of value is passed into the function.

  • Call_by_value is change mode inside the function is limited to function array.
  • Call_by_reference is change mode inside the function validate outside of the function also.
  • Call_by_value is actual and formal arguments are created at different memory location.
  • Call_by_reference is actual and formal argument are created at same memory location.
//Prog for understand
#include<stdio.h>
void myswap(int x,int y);  //Function prototype
main()
{
  int a=10,b=20;
  myswap(a,b);  ///Pass_by_value
  printf("After swapping:%d %d",a,b);
}
void myswap(int x,int y)
{
  x=x+y;
  x=x-y;
  y=x-y;
}///END






























Comments

Popular posts from this blog

#110 Mavanuru Malleshwara Temple

  Mavanuru Malleshwara Temple is located in Hassan district of Karnataka, This place is 30KM away from Hassan main town and 170KM from Bangalore. เคฎเคตเคจुเคฐु เคฎเคฒ्เคฒेเคถ्เคตเคฐ เคฎंเคฆिเคฐ เค•เคฐ्เคจाเคŸเค• เค•े เคนाเคธเคจ เคœिเคฒे เคฎें เคธ्เคฅिเคค เคนै, เคฏเคน เคœเค—เคน เคนाเคธเคจ เคฎुเค–्เคฏ เคถเคนเคฐ เคธे 30KM เค”เคฐ เคฌैंเค—เคฒोเคฐ เคธे 170KM เคฆूเคฐ เคนै। The temple is perched on top of a hill with beautiful views of the town, mountains and windmills all around. Though a small one, it looks stunning in bright golden colour and houses Lord Shiva as the deity.  เคฎंเคฆिเคฐ เคเค• เคชเคนाเคก़ी เค•ी เคšोเคŸी เคชเคฐ เคธ्เคฅिเคค เคนै, เคœिเคธเค•े เคšाเคฐों เค“เคฐ เคถเคนเคฐ, เคชเคนाเคก़ เค”เคฐ เคชเคตเคจ เคšเค•्เค•िเคฏां เคนैं। เคนाเคฒांเค•ि เคเค• เค›ोเคŸे เคธे, เคฏเคน เคšเคฎเค•ीเคฒे เคธुเคจเคนเคฐे เคฐंเค— เคฎें เค†เคถ्เคšเคฐ्เคฏเคœเคจเค• เคฆिเค–เคคा เคนै เค”เคฐ เคญเค—เคตाเคจ เคถिเคต เค•ो เคฆेเคตเคคा เค•े เคฐूเคช เคฎें เคฆेเค–เคคा เคนै। Distance between Silk Board to temple is 170KM. เคธिเคฒ्เค• เคฌोเคฐ्เคก เคธे เคฎंเคฆिเคฐ เค•े เคฌीเคš เค•ी เคฆूเคฐी 170KM เคนै।  Click here for Google Map Mavanuru Malleshwara Temple, Bijemaranahalli, Karnataka 573225 You should also Lakshmi Narasimha Nuggehalli toward Bangalore and distance is only 35KM. เค†เคชเค•ो เคฒเค•्เคท्เคฎी เคจเคฐเคธिเคฎ्เคนा เคจुเค—्เค—ेเคนเคฒ्เคฒी เคฌैंเค—เคฒोเคฐ เค•ी ...

#103 Chandra Choodeswarar Temple Hosur

'Chandra Choodeswarar' means the Eshwara who wears the Moon (Chandra) as an ornament on his crest or tuft of hair on top of the head, Shiva's consort Parvathi is worshiped here as Maragathambal. ‘Maragatham’ means green and ‘Ambal’ means mother. This is in reference to the green plants and trees (Photosynthesis), which provide sustenance to all living beings. 'เคšंเคฆ्เคฐ เคšूเคกेเคถ्เคตเคฐ' เค•ा เค…เคฐ्เคฅ เคนै เคˆเคถ्เคตเคฐ เคœो เคšंเคฆ्เคฐเคฎा (เคšंเคฆ्เคฐ) เค•ो เค…เคชเคจे เคถिเค–ा เคชเคฐ เค†เคญूเคทเคฃ เค•े เคฐूเคช เคฎें เคฏा เคธिเคฐ เค•े เคŠเคชเคฐ เคฌाเคฒों เค•े เค—ुเคš्เค›े เค•े เคฐूเคช เคฎें เคชเคนเคจเคคा เคนै, เคถिเคต เค•ी เคชเคค्เคจी เคชाเคฐ्เคตเคคी เค•ी เคฏเคนां เคฎเคฐाเค—เคฅंเคฌเคฒ เค•े เคฐूเคช เคฎें เคชूเคœा เค•ी เคœाเคคी เคนै। 'เคฎเคฐเค—เคฅเคฎ' เค•ा เค…เคฐ्เคฅ เคนै เคนเคฐा เค”เคฐ 'เค…ंเคฌเคฒ' เค•ा เค…เคฐ्เคฅ เคนै เคฎां। เคฏเคน เคนเคฐे เคชौเคงों เค”เคฐ เคชेเคก़ों (เคช्เคฐเค•ाเคถ เคธंเคถ्เคฒेเคทเคฃ) เค•े เคธंเคฆเคฐ्เคญ เคฎें เคนै, เคœो เคธเคญी เคœीเคตिเคค เคช्เคฐाเคฃिเคฏों เค•ो เคœीเคตिเค•ा เคช्เคฐเคฆाเคจ เค•เคฐเคคे เคนैं। Distance between Silk Board, Bangalore to Chandra Choodeswarar Temple Hosur is 35 KM. เคธिเคฒ्เค• เคฌोเคฐ्เคก, เคฌैंเค—เคฒोเคฐ เคธे เคšंเคฆ्เคฐ เคšूเคกेเคถ्เคตเคฐ เคฎंเคฆिเคฐ เคนोเคธुเคฐ เค•े เคฌीเคš เค•ी เคฆूเคฐी 35 KM เคนै। Click here for Google Map Sri Chandra Choodeswara Temple, Sanasandiram, Hosur, ...

#96 Hutridurga Betta _Trek_88KM

  H utridurga Fort is indeed a hidden gem near Bangalore. A trip to Hutridurga combines the pleasure of a good weekend trip from Bangalore with the thrill of trekking up the Hutridurga Uttari Betta. เคนुเคค्เคฐीเคฆुเคฐ्เค—ा เค•िเคฒा เคตाเคธ्เคคเคต เคฎें เคฌैंเค—เคฒोเคฐ เค•े เคชाเคธ เคเค• เค›िเคชा เคนुเค† เคฐเคค्เคจ เคนै। เคนुเคค्เคฐीเคฆुเคฐ्เค—ा เค•ी เคฏाเคค्เคฐा เคฌैंเค—เคฒोเคฐ เคธे เคเค• เค…เคš्เค›ी เคธเคช्เคคाเคนांเคค เคฏाเคค्เคฐा เค•े เค†เคจंเคฆ เค•ो เคนुเคค्เคฐीเคฆुเคฐ्เค—ा เค‰เคค्เคคเคฐी เคฌेเคŸ्เคŸा เคคเค• เคŸ्เคฐेเค•िंเค— เค•े เคฐोเคฎांเคš เค•े เคธाเคฅ เคœोเคก़เคคी เคนै। Distance between Silk Board, Bangalore  to Hutridurga Betta Trek Point is 88 KM. เคธिเคฒ्เค• เคฌोเคฐ्เคก, เคฌैंเค—เคฒोเคฐ เคธे เคนुเคค्เคฐीเคฆुเคฐ्เค—ा เคฌेเคŸ्เคŸा เคŸ्เคฐेเค• เคชॉเค‡ंเคŸ เค•े เคฌीเคš เค•ी เคฆूเคฐी 88 KM เคนै। Click here for Google Map Huthri Betta Hike Starting Point, Hutridurga Rd, Huthridurga(Santhepete), Karnataka 572126 You should also visit Magadi Ranganathaswamy Temple, It is 20KM toword to Bangalore. เค†เคชเค•ो เคฎเค—เคฆी เคฐंเค—เคจाเคฅเคธ्เคตाเคฎी เคฎंเคฆिเคฐ เคญी เคœाเคจा เคšाเคนिเค, เคฏเคน เคฌैंเค—เคฒोเคฐ เคธे 20 เค•िเคฎी เคฆूเคฐ เคนै। Click here for Google Map Click here for full Details and Blog Historically the Hutridurga is connected to Kempegowda who is credited as the fou...