Showing posts with label function. Show all posts

Some basic of printf

This is the system define function  used to print something on monitor screen .

Definition of this function is written in header file stdio.h  and this is the reason we write #include<stdio.h> at top of our program code 

prototype of printf : 

 int printf(char const *,...) ;

printf always return integer value , and the value is equal to how much character or anything else printed on monitor screen.

printf("%d",2);   will return 1
printf("this is core");  will return 12

printf("%f",2.3)     will return 8( because this time printf     print  2.300000  on monitor screen )


Now parameter of printf is (char const *,...) 
first parameter is char const * (constant character string ) 

Ex- "what a look" , "Never go back" "12gshs"  

second parameter is three dot ...    this is called ellipsis
meaning of the ellipsis is you can put variable number of argument in printf 


variable number of arguments means sometime you are using printf to print one value , sometime more than one 

Have a look on this program it will be more clear 




#include<stdio.h>
void ellipsis(int var,...);

void main()

{

int a=5,b=10,c=20;


ellipsis(a);
ellipsis(a,b);
ellipsis(a,b,c);

}

void ellipsis(int var,...)

{

printf("%d",var);


}
output :- 5 5 5 
So printf can have variable number of argument , 


printf  format specifier have the following form 

%[flag][width][.precision] [F|N|H|L] type_char

Each  format specifier  begins with % (percent character )


details of all specifier with example 

[flag] - (optional)        work  
   
   -                  :-     Left-justifies the result, pads on the right with blanks. If not given, it right                   
                              justifies the result, pads on the left with zeros or blanks.

+                       data is preceded by + or – sign if number is +ve and 
                         -ve respectively.
 0(blank)          If value is nonnegative, the output begins with a blank instead of a plus;   

                                 negative values still begin with a minus.

#                         Specifies that arg is to be converted using an alternate form.

Note: Plus (+) takes precedence over blank () if both are given.


    
[width]             (optional)       It is +ve integer .It defines minimum field width. If length of data is    smaller than width. In case right adjustment in put first (width-length of data) number of blank space then prints data.      

width specifier 
n                      At least n characters are printed. If the output value has less than n characters,   
                             the output is padded with blanks (right-padded if - flag given, left-padded
                             otherwise).


0n                     At least n characters are printed. If the output value has less than n characters, 

                                is filled on the left with zeros.


*                The argument list supplies the width specifier, which must precede the actual         

                        argument being formatted.



  #include<stdio.h>
void main()

{

char str[12]="world_of_c";

printf("%15s\n",str);

printf("%-15s\n",str);

printf("%10s\n",str);

printf("%-10s\n",str);

printf("%(-10)s\n",str);

printf("%-(-10)s\n",str);

printf("%10s\n",str);

}


[.precision]   : (optional)this is used to print maximum number of character in case of string and in case of integer minimum  number of digit 


1. in case of string precision is a integer,which indicate
how many character of string will be printed .if the string length is less than the precision length the whole string will be printed 
  



#include<stdio.h>
int main()
{
   char str[10]=''worldwide"; 
 printf("%.2s",str);   // .precision      .2 
}


output :- wo 


What is function in c

We can define function as a block of code which has a name and a special task , we can call it from any where of c program using its name .
If you are not getting the point by its definition then consider on a simple function "printf()" 
what is printf()   ??
its a function and according to definition it has a name(printf) and specific work (to print on monitor screen ).
it's a system define function but we can also define our own function .
So we can categories function in two type

1.System define function . 
Ex- printf(), scanf(),gtes, puts, strcpy() ....etc

2. User define function 
 Ex-    whatever name and task we give 

let's go for write own function and understand how it works ,how many types of aspects of it 

main()
 {
   printf("Implement of function");
   user();
   }
user()
{
   printf("This is the simplest one");
 }
look on this code segment , simplest example of function calling .
Since main() is a function and execution begins from main  actually,  so in every program main must be present .

here execution begins from main and print the message 
Implement of function
and now we are invoking function user(Define by user)
So control goes to user and print the message 
"This is the simplest one "
after this control goes back to main and program terminate .


If we define own  function in any program there should be three part of the function 
1. Function prototype  
2. Function call 
3. Function definition   

program that will  make you familiar to these terms  

Program to sort the element of array .(Bubble sort)

#include<stdio.h>
#include<alloc.h>
  int *array,n;
void sort_array();  // Function prototype 
int main()
 {
 int i;
printf("How many element you want in array \n");
scanf("%d",&n);

array=(int*)malloc(sizeof(int)*n);

printf("Enter the element into array\n"); 

  for(i=0;i<n;i++)
scanf("%d",(array+i)); 

// array before sorting 
  printf("See what you entered\n  ");

 for(i=0;i<n;i++)
printf("%d\n",*(array+i)); 



sort_array();           // function calling 

// array after sorting (after calling the function sort_array()) 
printf("Array after sorting \n ");
  for(i=0;i<n;i++)
printf("%d\n",*(array+i));  // a[i] will also work 

return 0 ;
}
void sort_array()    // Function definition 

{
 int i, j,temp;
for(i=0;i<n;i++)
 {
for(j=i;j<n;j++)
{
if(array[i]>array[j])
{
temp=array[i];
array[i]=array[j];
 array[j]=temp;
}
 }
 }  
}  // closing of function 


If you go through the program you will see all three terms function prototype, function call and function definition .

function prototype :- it's a layout of function we are going to use in our program ,using this compiler knows that what is return type of function, how many argument it has . compiler makes a table of information about this function and keep for future use .

function call :  - we use the function name  to transfer the control to the function definition ,as in program , after executing it control goes to definition of function and complete its work !! 

function definition : - it's body of function i.e. what is the work of function we write in body of function(definition ) after executing the name of function control comes to function definition

Function pointer in c

What is   function pointer  in c ?

Function pointer is a type of pointer by which we can point to a function as we point a ordinary variable using pointer .
Every function  has an address in memory and we can point to that function address but as we know pointer which will point to function should be same type (function type)

 here i am trying to point address of PRINTF function .
#include<stdio.h>

main()
{
   int (*func_point)(char const *,...) ;
   func_point=&printf;                           // assigning the address of printf to "func_point" function 
                                                          // now we "func_point" as printf function  
 func_point("THIS IS FUNCTION POINTER EXAMPLE");
}

   In first line we declaring a function pointer as printf type .........because we have to point the function printf.
now in next line just assigning the address of printf to func_point (i.e. func_point has address of printf now )
and so now we can use the func_point as printf . :) 
A strange thing you might face  in first line if you doesn't know more about the printf 
int (*func_point)(char const*,...)   ...it's just prototype of printf , 
Since we have to point the function printf so we have to make the funcction pointer as same as the function.