Tuesday, 27 March 2012

Basic Functions & programs of C | 2nd Session

Hello friends so today as i said today i'm gonna teach you some common function & also write some basic programs of c . so lets start.



The structure of C program
















C is made up entirely of building blocks which have a particular ‘shape’ or form .A program is made up of functions, functions are made up of statements and declarations surrounded by curly braces { } .One and only one of these functions in the program must have the name main(). This function is always the starting point of a C program . The parentheses ‘()’ which follow the name of the function must be included even though they apparently serve no purpose at this stage.


m

The Headers 

#include header.h
At the top of a program file. For instance:
#include "myheader.h"
Includes a personal header file which is in the current directory Or
#include <stdio.h>
includes a file which lies in a standard directory like ‘/usr/include’.
The #include directive is actually a command to the C preprocessor .



Now i will tell you about printf function .

The printf() function 


  • What is full form of  printf  function?  
    - Print format function

  • What is print format (printf) ?
      - It refers to a control parameter used by a class of functions typically associated with some types of     programming languages. The format string specifies a method for rendering an arbitrary number of varied data type parameter(s) into a string. In simple words we can say It provides an superbly versatile way of printing text.


  • How to print a normal string  ?
     -  printf ("..some string..."); 

  • How to print a Integer ?
     - Here we cant use the same command we used in printing the text, we have to use control sequence  -    %d  . The variable someinteger is printed instead of ‘%d’  . Below you see the command .

        printf ("Integer = %d",someinteger);


Here is a small program with its output showing the example of printf function .


/***********************************************************/
/* any program */
/***********************************************************/
#include <stdio.h>
/***********************************************************/
main () /* program */
{
printf ("Astronomy is %dderful \n",1);
printf ("And interesting %d \n",2);
printf ("The ear%d volves around the sun \n",3);
printf ("And makes a year %d you \n",4);
printf ("The moon affects the sur %d heard \n",5);
printf ("By law of phy%d great \n",6);
printf ("It %d when the the stars so bright \n",7);
printf ("Do nightly scintill%d \n",8);
printf ("If watchful providence be%d \n",9);
printf ("With good intentions fraught \n");
printf ("Should not keep up her watch divine \n");
printf ("We soon should come to %d \n",0);
}


Output - 


Astronomy is 1derful \n"
And interesting 2
The ear3 volves around the sun
And makes a year 4 you
The moon affects the sur 5 heard
By law of phy6d great
It 7 when the the stars so bright
Do nightly scintill8
If watchful providence be9
With good intentions fraught
Should not keep up her watch divine
We soon should come to

Then after you have successfully compiled it  then save it as printf.c  . It has its own extension .c, like .exe, pdf .

Some Questions for practice -
1. Why is #include <stdio.h> used for ?

2. Write a command to print out the message "hackingsec".
3. Write a command to print out the number 97?
4. Write two commands to print out "The 2 hackers" two different ways.





So hope its clear & you can solve this question .
if any doubt then please mention it Via comment



.


No comments:

Post a Comment