c language tutorial for beginners | how to study c programming language | c programming blog

Introduction To C language


c language tutorial for beginners
Reading, processing and writing of data are the three essential functions of a computer Program.Most programs take some data as input and  display the processed data often known as information or results.There are two methods of providing data to the program data to the  program variables.One method is to assign values to variables through the assignment statements such as x=5, a=0 and so on. Another method to use the input function scanf which can read data from a terminal. For outputting results the function printf which sends results out to a terminal is used.'C' does not have any built-in input and output statements as part of its syntax .All input and output operations are carried out through function calls such as printf and scanf.There exits several functions that have more or less become standard for input and output operation in C.These functions are collectively known as i/o library. Each program that uses a standard input/output function must contain the statement, at the beginning.#include'stdio.h' The filename stdio.h is an  input-output eader file.
The instruction#include'stdio.h'tells the compiler to search for a file named stdio.h and place its contents at this point in the program. when it is compiled the contents of the header file become part of the  source code.how to study c programming language     


Reading A Character


The simplest of all input and output operations is reading a character from the standard input unit and writing it to the output unit. Reading a character can be done by the function 'getchar'. 


The getchar take the form:   variable_name=getchar();variable_name a valid 

c name that has been declared as char type. When encountered,the computer waits until a key is pressed and then assign this character as a value to getchar
function.

Example:    char name;

            name=getchar();
Program:
        /* Reading a character from terminal*/

       #include'stdio.h'

       main()
       {
        char answer;
        printf(" would u like to know my name?\n ");
        printf(" type y for yes and n for no: ");
        answer=get char();
        if(answer=='y' || answer=='Y')
        printf("\n\n my name is busy bee \n ");  
        else
        printf(" \n\n u are good for nothing \n ");
       }

Output:would u like to know my name?

          type y for yes and n for no:y
          my name is busy bee
             
Expiation:-Along with the getchar() function,there are two functions which receive a character typed from the keyboard and tests whether it is a letter or digit and print out a message accordingly. 
   
They are:
             isalpha(character)
             isdigit(character)
The function isalpha a value non-zero(true)if the argument character contains an alphabet otherwise it assumes zero(false)similar is the case with the function isdigit.

Program:    

      /*Testing character type*/
          #include'stdio.h'
          #include'ctype.h'
          main()
          {
           char character;
           printf("press any key\n");
           character=getchar();
           if(isalpha(character)>0)
           printf("the character is a letter");
           else
           if(isdigit(character)>0)
           printf("the character is a digit");
           else
           printf("the character is not alphanumeric");
          }

output:           press any key

                  f  
                  the character is a letter

                  press any key

                   3
                  the character is a digit

                  press any key

                  * 
                  the character is not alphanumeric

        The function in character test include:

     function                        test

    1.isalpha(c)              is c an alphabetic character?

    2.isalphanum(c)           is c an alphanumeric character?
    3.sdigit(c)               is c a digit?
    4.islower(c)               is c a lower case letter?
    5.isprint(c)               is c a printable character?
    6.ispunct(c)               is c a punctuation mark?
    7.isspace(c)               is c a white pace character?

    8.isupper(c)               is c a uppercase  letter?


Writing A Character

     The function putchar is used for writing character one at a time to the terminal.It takes the form as shown below:
      putchar(variable_name);
     where variable_name is a type char variable containing a character.

for example:
         an='y';
         putchar(an);
     will display the character y on the screen.

        The  statement      putchar('\n')  ;
         would cause the cursor on the screen to move to the
beginning of the next line.

program:   /* Writing a character to the terminal */  
          #include
          #include
          main()
          { 
            char alphabet;
            printf("enter an alphabet");
            putchar('\n');
            alphabet=getchar();
            if(islower(alphabet))
            putchar(toupper(alphabet));
            else
            putchar(tolower(alphabet));
          }      

Output:          enter an alphabet        
                 a
                 A
                 enter an alphabet
                 R
                 r  
Formatted input: Formatted input refer to an input data that
has been arranged in a particular format.

example:14.35           134             pinky
       
        The first part of data should be read into a variable float,the second into int,and the third part into char. This is possible in C using the scanf  function.   
     scanf("control string", arg1,arg2............argn); the control string  specifies the field format in which the data is to be entered  and the argument arg1,arg2,... argn specify the address of locations where the data is stored.
Control string contain field specification which direct the interpretation of input data.  

It may include: field specifications,consisting of the conversion 
character%, a data type character and an optional number,

specifying the field width.

No comments:

Post a Comment