Sunday, November 17, 2013

If Statement - C Programming





Monday, November 11, 2013

The program which show the use of printf() & scanf() function




Output

Enter the Name
Laura
Name is :
Laura






Program to illustrate the concept of puts() with gets() functions




Output

Enter the Name
Laura
Name is :
Laura






Sunday, October 13, 2013

C File Input/Output

Home  �  C Programming  �  C File Input/Ouput


     File is not the new concept. File is a more flexible approach than the previous data entry approach. Files are the mega data structure in information processing. Storage of information and its retrieval are the vital components of system design and information system. By using files data can be stored on the disks and can be read whenever you want without destroying the date. A file is place on the disk where a group of related data is stored permanently. By using files we can sense the data. Files establish a permanent links between inputs and ouputs as data can be stored and retrieved. A computer programmber or data entry operator always prefer to enter data in the files instead of storing the data temporary in the main memory using the direct keyboard facility.

Contents


Definition

     A file is a collection of related data structure pertaining to a single entity. A file having payroll data processes the information of pay elements and not about the personal and educational information of an employee. So we can say file has mainly two basic elements: information and a single entity.

Types of Files

     There are two mainly types of Files used in C Programming. These are below:




Files Operations

     The processing of files has number of operation according to the user's requirement and the problem definition. But some of the commonly used file operations are as follows:

  1. Naming a file
  2. Opening a file
  3. Reading data from a file
  4. Writing data to a file or creation of data file
  5. Closing a file
  6. Updating a file


Naming a File

     File name (data file name) should not be more than eight characters and three characters for extension. File name can be defined and enclosed by using the fopen() high-level I/O function. The general Syntax is as follows:

fopen("filename","mode");


For example, "Student.dat" is a data file name, which is defined and enclosed in the fopen() function as:
fopen("Student.dat","mode");



Opening a File

     A file is opened by using fopen() in build high-leve input/ouput function. The general Syntax is as follows:

FILE *fp;
fp = fopen("filename","mode");





Closing a File

     A file is Close by using fclose() in build high-leve input/ouput function. The general Syntax is as follows:

fclose(file-pointer);

For example
FILE *fl;
fl = fopen("Student","w");
fclose(fl);



Input/Ouput statements used in file handling

     File have different Input/Ouput statements used for different purpose. These are used to put data from variables to data file. The various Input/Ouput functions used with standard I/O are discussed as below:

  1. Character Input/Ouput (Standard I/O)
  2. String Input/Ouput (Standard I/O)
  3. Formatted Input/Ouput (Standard I/O)
  4. Record (Block) Input/Ouput (Standard I/O)


A Program Write data into File and Read data from File :








Write a Program How many Words in a File :








Copy One File to another File :







<< Previous Topic
Next Topic >>



Saturday, October 12, 2013

C Unions

Home  �  C Programming  �  C Unions


     Union is not new, it was originated from the Structure. In other words we can say union is similar to the structure, but processing and storage is little different. Actually union is borrowed from the structure having the same syntax and but semantics is different. Manily union is used to conserve memory. Weather union is lesser used in the C program, but for the programs having large number of statements, it is very useful. From the storage point of view, in structure each member has its own storage location, whereas in the union all the members used the same locations, So we can say union saves the memory and make the processing fast as compared to the structure.

Contents





Definition :

     A union is a variable that declares a set of multiple variable (called members or elements having different data-types) sharing the same memory area. The compiler allocates sufficient memory to hold the largest variable of the union.
All the members of union occupy the same memory locations called addresses. The old value of the already declared member will be destroyed and new value to the new member will be assigned in the memory by using the union concept.



Union Variables :The general syntax used to declare a union and union variables is written as below :
union union-tag
{
  date-type1 member-1;
  date-type2 member-2;
  date-type3 member-3;
  ......................................................;
  date-type n member-n;
}v1,v2,.......vn;

one more way of declaration be as:

union union-tag
{
  datatype-1 member-element-1;
  ......................................
  datatype-n member-element-n;
};
union tag-name v1,v2,v3,............vn;


Simple Union Program :



Output is as :

Name=Bintu
City=Muktsar




Difference between Structure and Union:
STRUCTURE
UNION
Every member has its own memoryAll members use the same memory
Keyword struct is usedKeyword union is used
All members may be initializedOnly its first member may be initialized
Different interpretations of the same memory location are not possibleDifferent interpretations of the same memory location are possible
Consumes more space compared to unionConservation of memory is possible


<< Previous Topic
Next Topic >>



Friday, October 11, 2013

C Structures

Home  �  C Programming  �  C Structures


     As you know different type of variables can be used for different purpose having different types of data-types. C supports such types of a constructed data type, which is called Structure. In Structure you can pack number of variables having different data type with single structure, which is called structure name or tag name or structure tag.



Contents





Definition :

     A structure is heterogeneous collection of related fields. Here fields are called structure member or structure element. Every field has different type-specifier (data-type).

The definition of structure of opposite of array, because array is set of homogeneous elements. But the structure has deep relationship with array.



Structure Variables :The general syntax used to declare a structure and structure variables is written as below :
struct structure-tag
{
  date-type1 strcture element-1 or member-1;
  date-type2 strcture element-2 or member-2;
  date-type3 strcture element-3 or member-3;
  ......................................................;
  date-type n strcture element-n or member-n;
};
main()
{
  struct structure-tag v1,v2,..........vn;
  local declaration;
  executable statements;
}


Simple Structure Program :



Output is as :

Name=Bintu
City=Muktsar

Note:- struct is the keyword which we can't use as a variable.




Structure within Structure (Nested Structure):



Output is as :
Enter the Name and City
Bintu
Muktsar
Name=Bintu
City=Muktsar



Structure and array :

     Arrays play very important role with structure. Structure has two types of view with the array. The two types of relationship of structure with array...





<< Previous Topic
Next Topic >>



Thursday, October 10, 2013

C Pointers

Home  �  C Programming  �  C Pointers


     Only the C Language supports pointers and they play an important role in it. Pointers are very simple to use, provided the basic concept is understood properly. Mainly pointers are used in C Program to acquire as much as stroage space to store more values at a time and at the same time allow the program to deallocate the storage, which is no more required in the program. Learning of pointer may be difficult, but not too difficult. It looks difficult because it is new topic and pointers are not used in the basic learning languages like Basic, Cobol, Fortran etc.

Contents



Definition :
     Pointers are memorey addresses variable i.e the variables, which have addresses of the variable having some value, stored in the memory. Further we can say pointers are directly linked to the memory address. Actually pointer is a variable having address of another variable and not the value. For example, suppose q is an integer variable and this variable have value 250 stroed in the memory.
It is represented as :
Variable nameMemory valueMemory addressMemory variable
q2505000p (pointer)







Use of Pointer or Advantages of Pointer :

   Pointers are used to direct link the memory address. Now question arises, Why pointers are required.
To solve this question some reasons for the use of the pointer are discussed as follows:

  1. A pointer enables the varible, which is used outside the function or used in the another Sub-program.

  2. Pointers increase the execution speed of the C-Program and are more efficient.

  3. Pointers reduces the length and complexity of the program.

  4. Pointers accesses the memory elements very easily.

  5. Pointers are more efficient in handling the date table i.e. two-dimensional way.

  6. Use of the pointer to the character array or to the string save the storage space in the memory.

  7. Pointers have direct link with structure and union.

  8. By using pointer, we can declare lesser number of variables in memory.

  9. Pointers help to return more than one value from the function sub-program

  10. By using pointers, function arguments can be passed to the functions.

  11. Pointers saves the memory space. This can be done by using the dynamic memory allocation technique.

  12. Pointers are also very useful to handle files.

  13. Pointers are very useful in data structure manipulation.






Declaring a Pointer Variable (Initialization of Pointer) :

   Declaration of pointer variable is similar to the declaration of a common variable. A Pointer variable should be declared before they are used.

   When we want to use pointer variable, then these should be declared in data-type statement used in the beginning of main program. The general syntax used for the declaration of pointer (memory variable) is as:

data-type *pointer-variable;

Where data-type may be integer (int), real (float), character (char) or double. Also here * (asteric sign) means it is pointer operator and pointer variable is any variable linked with '*' sign . The symbol * is called indirection operator. Some valid pointer declaration statement are as follows:

int *p;
float *y;
char *c;


   For example, suppose if p is a pointer variable, which gives the address of the variable, then we can use the above statement as:
int *p, q=250;
p = &q; /* This is initialization of Pointer*/
printf("\n %d is stored at address %u",q,p);
Here the result be as :
250 is stored at address 5000
  Note that the format code in the control string of printf() statement be %u because addresses are always unsigned integer.




Pointer Variables

Simple Variable
Pointer variable
Pointer to Pointer
int a;
a = 10;
a
10
4080
int *p;
p = &a;
p
4080
8080
int **t;
t = &p;
t
8080
2040








Pointer Program Using Function (Pass By Reference)



<< Previous Topic
Next Topic >>



Wednesday, October 9, 2013

C Functions

Home  �  C Programming  �  C Functions


     Subprogram is the independent and complete program. It is also called function program or function sub-program. In other words sub-programs are complete because, these are the programs, which have the global as well as local declaration statement, executable statement and function calling statement like main program. Subprograms in the c-language are called user-defined functions.

Contents


Advantages of a function sub-program (Why we need function)


     As a Function is a self contained module of some c statements having specific objective. But the declaration and use of function depends upon its need. So due to number of features in it, it becomes more important than the main program to solve some complex problems. Following are the some advantages of functions :

  1. Function makes the lengthly and complex program easy and in short forms. It means large program can be sub-divided into self-contained and convenient small modules having unique name.

  2. The length of source program can be reduced by using function by using it at different places in the program accroding to the user's requirement.

  3. By using function, memory space can be properly utilized. Also less memory is required to run program if funciton is used.

  4. A function can be used by many programs.

  5. Function increases the execution speed of the program and makes the programming simple.

  6. By using function, portability of the program is very easy.

  7. Debugging (removing error) becomes very eaiser and fast using the function sub-programming.

  8. It removes the redundancy i.e. avoids the repetition and saves the time and space.

  9. Functions are more flexible than library functions.

  10. Testing (verification and validation) is very easy by using functions.





Output is as :
I am a Good Boy

Return Statement

     Return statement is the last statement of a function. But in some cases, it can be used anywhere within the subprogram. When return statement execute, the control moves to the calling statement. Whether a fucntion send back any value to the calling function or not, but there must be the use of return statment with or without return value. The main purpose of the return function is to communicate the result of the operation of the called function to the place where the call is made. The general syntax and the procedure used for the return statement is :

return;
or

return (expression);
or

return (variable);






Categories of Functions

     Sometimes function program takes values from the main program and sometimes it does not. Similarly function sub-program sometimes-return values and sometimes it does not, return any value to the main program. we categorize the function subprogram in four sections:

  1. Function with no argument and no return value.
  2. Function with argument and no return value.
  3. Function with no argument and return value.
  4. Function with argument and return value.


Function with no argument and no return value.

     In this category of the function sub-program, main program will not send any argument to the function and also function subprogram will not send (return) any value to the main program. For example, the program procedure to display the processing of the function with no argument and no return value is as:





Output is as :
Enter the Two No. for Sum :
10
10
Sum= 20




Function with argument and no return value.

     In this category of the function sub-program, main program or the calling program will send argument value(s), but called program or the function subprogram will not return any value. For example, the program procedure to display the processing of the function with argument and no return value is as:





Output is as :
Enter the Two No. for Sum :
10
10
Sum= 20




Function with no argument and return value.

     In this category of the function sub-program, main program or the calling program will not send argument value(s), but called program or the function subprogram will return any value. For example, the program procedure to display the processing of the Function with no argument and return value is as:





Output is as :
Enter the Two No. for Sum :
10
10
Sum= 20




Function with argument and return value.

     In this category of the function sub-program, main program or the calling program will send argument value(s), but called program or the function subprogram will return any value. For example, the program procedure to display the processing of the Function with argument and return value is as:





Output is as :
Enter the Two No. for Sum :
10
10
Sum= 20




Recursion

     When a called function in turn calls another function, then a process of 'chaining' occurs. Recursion is a special case of this process or chain, So when a function calls itself, then it is called recursion. This chain continues till a specific condtion met. If it has not any stop condition, then it will create an indefinite loop. Recursion is also called self-reference loop.
     Recursion is used to solve for the problems, which can't be solved by the iterative procedure for while and do loops. It is useful to solve repetitive problems, where input of one sub-program can be used as starting value having the previous output.







Void Statement


     When a void statement is used in function program, then it returns nothing. In other words, when we want to return no value to the calling program, then void function or void statement is used. The general syntax is as:

void function name(); /*during function declaration*/
or
void function-name() /*during function in use*/



<< Previous Topic
Next Topic >>



Tuesday, October 8, 2013

C Strings

Home  �  C Programming  �  C Strings


     When you press any key from the keyboard, then it is said to be a character. But when you press more than one key, it becomes a string. So combination of characters (group of characters) is called string. "I am a good boy." is a string. We can print or display the string by using the printf() function as:

printf("\n I am a good boy");

Contents


We can print a string by using control string %s or %[..] or %[^] i.e the declaration is as follows:

char name[10];
printf("%s",name);        /*to print a single string*/

or

char name[5][10];
for(i=0;i<5;i++)        /*to print a multiple string*/
{
   printf("%s",name[i]);
}




Operations on String

     String has number of operations in the C-Language, but some commonly used are as follow:

  1. Initialization of String variable
  2. Reading and Writing of String
  3. Combining strings together
        or
    Concatenation of two or more than two strings
  4. Copy one string to another
  5. Comparing two strings
  6. Extracting a portion of string
        or
    To display a sub-string from the string
  7. To reverse a string
  8. To find whether a string is palindrome or not

String Handling Functions

     C-language is rich in library functions, but to handle or operate some operations with string, we use some powerful string handling functions. All these function are linked with the "string.h" header file strored in the include sub-directory in the Turbo-C compiler. Below the five commonly used string handling function as:

  1. strcat()
  2. strcmp()
  3. strcpy()
  4. strlen()
  5. strrev()

i) stcat()

     The purpose of this string handling function strcat() is to concatenate or combine two different strings together.

The general syntax used for this is as:

strcat(string1,string2);





Output is as :
Enter the two Names
Bintu
Chaudhary
BintuChaudhary




ii) stcmp()

     The purpose of this function compare two strings. It will check which string is alphabetically above the others. For comparison ASCII (American Standard Code for Information Interchange) values are used

The general syntax used for this is as:

strcmp(string1,string2);





Output is as :
Enter the two Names
Bintu
Chaudhary
Both String are not equal




iii) strcpy()

     The purpose of this function is to copy one string into another string. Note that target or destination field should be larger than the source filed. In other words size of the string1 should be larger to receive the contents of the string2.

The general syntax used for this is as:

strcpy(string1,string2);





Output is as :
Enter the two Names
Bintu
Chaudhary
Bintu Chaudhary




iv) strlen()

     The purpose of this function is to count the number of character in a string i.e. to find the length of the string.

The general syntax used for this is as:

n=strlen(string);





Output is as :
Enter the Name
Laura
Length of String=5




v) strrev()

     The purpose of this function is to reverse a string.

The general syntax used for this is as:

strrev(string);





Output is as :
Enter the Name
Laura
aruaL



<< Previous Topic
Next Topic >>



Monday, October 7, 2013

C Arrays

Home  �  C Programming  �  C Arrays


     Array is similar type of data collection name i.e. it is a collection of same data type elements. An array is a group of related data items, which share common name. It is the set of homogeneous data.

Program without array having different variables:





Output is as :
20   30   40   50



Program with array having same variable name




Output is as :
How many number you want to enter: 5
Enter the 5 different elements in the array:
Enter the 5 different elements in the array:
20   30   40   50

Different array elements are:
20   30   40   50

      So array will solve the problem with the use of lesser number of variable and variable having the same name.

Types of Array

Linear Array

     This type of array is also called one Dimensional Array. This is also called list array. Another name of linear array is called single-dimensional array. These array are of ladder type. In the linear array only one subscript is used. It is written either in row or in column form.

The syntax to define or declare linear array is:

data-type arrary-name[size];


where data types are integer(int), real(float), double and character(char).
For example, some of the valid one dimensional array declaration statements are written as below:

int a[50];
float f[50];
char n[20];





Output is as :




Non-Linear Array

     Array of having different dimensions or n subscript is in the form of Non-linear array. Non-linear array are further of n different types as:

(a) Two Dimensional Array
(b) Three Dimensional Array
............................
(n) N-Dimensional Array


Two-Dimensional Array

     These arrays are also called double dimensional array. Another name of two-dimensional array is Tabular or Rectangular Array. These array are in row and column form, so these are also called Row-Column array or square array.

The syntax to define or declare two dimensional array is as:

data-type arrary-name[row size][column size];


For example, some of the valid one dimensional array declaration statements are written as below:

int a[10][10];
float f[50][50];
char n[20][20];





Output is as :




<< Previous Topic
Next Topic >>



Sunday, October 6, 2013

C Jumping Statements

Home  �  C Programming  �  C Jumping Statements


     There are three different controls used to jump from one C program statement to another and make the execution of the programming procedure fast. These three Jumping controls are:

a) goto statment
b) break statment
c) continue statment




goto statement

     The powerful Jumping statement in C-Language is goto statement. It is sometimes also called part of branching statement. The go to moves the control on a specified address called label or label name. The goto is mainly of two types. One is conditional and the other is unconditional.










break statement

     Break is always used with then decision-making statement like if and switch statments. The statement will quit from the loop when the condition is true.

The general syntax for break statement is as:

break;




Output is as :
I=1
I=2
I=3
I=4
I=5







Continue statement

     Continue statement also comes with if statement. This statment is also used within any loop statement like do loop, while loop and for statement.

The general syntax for continue statement is as:

continue;


This statement is skip some part of iteration (loop) and comes to the next looping step i.e. it will increment / decrement the loop value, when continue occurs.




Output is as :
I=1
I=2
I=3
I=4
I=5
I=7
I=8
I=9
I=10



<< Previous Topic
Next Topic >>



Saturday, October 5, 2013

C Looping Statements

Home  �  C Programming  �  C Looping Statements


When a single statement or a group of statements will be executed again and again in a program (in an iterative way), then such type processing is called loop. Loop is divided into two parts:

(a) Body of the loop
(b) Control of loop

The looping statements used in C-language are :

a) while statement or while loop
b) do statement or do loop
c) for statement or for loop
d) Nested for loop statement




while Statement or while loop

     While statement or while loop is an entry control loop. In this, first of all condition is checked and if it is true, then group of statements or body of loop is executed. It will execute again and again till condition becomes false.

The general syntax is :
while (test condition)
{
   block of statements;
}
statements-x;




Output is as :
I=1
I=2
I=3
I=4
I=5
I=6
I=7
I=8
I=9
I=10







do Statement or do loop

     Do statement is exit control loop. It is also called do-while statement. In this statement, first body of the loop is executed and then the condition is checked. If condition is true, then the body of the loop is executed. When condition becomes false, then it will exit from the loop.

Note : do while loop always give one output although given condition is true or false.

The syntax of do-loop is as follow:
do
{
    block of statements;
}
while (condition);
statement-x;




Output is as :
I=1
I=2
I=3
I=4
I=5
I=6
I=7
I=8
I=9
I=10







for Statement or for loop

     It is a looping statement, which repeat again and again till it satisfies the defined condtion. It is one step loop, which initialize, check the condition and increment / decrement the step in the loop in a single statement.

The general syntax is as :
for(initial value; test condition; increment/decrement)
{
    body of the loop;
}
statement-x;




Output is as :
I=1
I=2
I=3
I=4
I=5
I=6
I=7
I=8
I=9
I=10







Nested for loop statement

     When a for statment is executed within another for statement, then it is called nested for statement. We can apply number of nested for statement in C-Language.

The general syntax is as : for(initial value1; test condition1; increment/decrement1)
{
  for(initial value2; test condition2; increment/decrement2)
  {
      inner-body of the loop;
  }
  outer-body of the loop;
  statement-x;
}




Output is as :
*
**
***
****
*****



<< Previous Topic
Next Topic >>



Friday, October 4, 2013

C Branching Statements

Home  �  C Programming  �  C Branching Statements


    In the term software or computer programming, it has a set of instruction (in simple or complex form) called program. These instructions are also called statements, which occurs sequentially or in either conditional way or in the iterative way. To handle such types of statements some flow controls required. These flow controls are called Control Statements

    In other words, the control statements are used to control the cursor in a program according to the condition or according to the requirement in a loop. There are mainly three types of control statements or flow controls. These are illustrated as below:

Contents

Branching Statement

  1. if statement
    1. Simple if statement
    2. if-else statement
    3. nested if statement
    4. else-if or ladder if or multi-condition if statement
  2. switch statement
  3. conditional operator statement

Branching



if statement

    The if statement is a powerful decision making statement which can handle a single condition or group of statements. These have either true or false action....
    When only one condition occurs in a statement, then simple if statement is used having one block.









if-else statement

    This statement also has a single condition with two different blocks. One is true block and other is false block...



Output is as :
Enter the Number
4
This is Even Number









nested if statement

    When an if statement occurs within another if statement, then such type of is called nested if statement.



Output is as :
Enter the three Ages of Ram,Sham and Ajay
14
17
19
Ram is Youngest







Ladder if or else if statement

    When in a complex problem number of condition arise in a sequence, then we can use Ladder-if or else if statement to solve the problem in a simple manner.

The marks obtained by a student in 5 different subjects are input through the keyboard. The student gets a division as per the following rules:

=> Percentage above or equal to 60 - First Division
=> Percentage between 50 and 59 - Second Division
=> Percentage between 40 and 49 - Third Division
=> Percentage less than 40 - Fail





Method-1



Method-2











switch statement

    When number of conditions (multiple conditions) occurs in a problem and it is very difficult to solve such type of complex problem with the help of ladder if statement, then there is need of such type of statement which should have different alternatives or different cases to solve the problem in simple and easy way. For this purpose switch statement is used. .



Output is as :
Enter the Choice from Four Days...
S = Sunday
M = Monday
T = Tuesday
H = Thursday

S
Sunday

Conditional Control Statement

     This statement is based on conditional operator. This statement solves the problem's condition in a single line and is a fast executable operation. For this purpose we can take combination of ? and :




<< Previous Topic
Next Topic >>



Thursday, October 3, 2013

C Input/Output Statements

Home  �  C Programming  �  C Input/Output Statements


        In C Language input and output function are available as C compiler function or C library provided with each C compiler implementation. These all functions are collectively known as Standard I/O Library function. Here I/O stands for Input and Output used for different inputting and outputting statements. These I/O functions are categorized into three prcessing functions. Console input/output function (deals with keyborad and monitor), disk input/output function (deals with floppy or hard disk) and port input/output function (deals with serial or parallet port). As all the input/output statements deals with the console, so these are also Console Input/Output functions. Console Input/Output function access the three major files before the execution of a C Program. These are as:

stdin: This file is used to receive the input (usually is keyborad file, but can also take input from the disk file).

stdout: This file is used to send or direct the output (usually is a monitor file, but can also send the output to a disk file or any other device).

stderr: This file is used to display or store error messages.

Contents

Input Ouput Statement

    Input and Output statement are used to read and write the data in C programming. These are embedded in stdio.h (standard Input/Output header file). There are mainly two of Input/Output functions are used for this purpose. These are discussed as:

Unformatted I/O functions

   There are mainly six unformatted I/O functions discussed as follows:

a) getchar()
b) putchar()
c) gets()
d) puts()
e) getch()
f) getche()

a) getchar()

    This function is an Input function. It is used for reading a single character from the keyborad. It is buffered function. Buffered functions get the input from the keyboard and store it in the memory buffer temporally until you press the Enter key.

The general syntax is as:
v = getchar();

where v is the variable of character type. For example:
char n;
n = getchar();

A simple C-program to read a single character from the keyboard is as:

/*To read a single character from the keyboard using the getchar() function*/
#include
main()
{
char n;
n = getchar();
}


b) putchar()

    This function is an output function. It is used to display a single character on the screen. The general syntax is as:

putchar(v);

where v is the variable of character type. For example:
char n;
putchar(n);

A simple program is written as below, which will read a single character using getchar() function and display inputted data using putchar() function:

/*Program illustrate the use of getchar() and putchar() functions*/
#include
main()
{
char n;
n = getchar();
putchar(n);
}


c) gets()

    This function is an input function. It is used to read a string from the keyboar. It is also buffered function. It will read a string, when you type the string from the keyboard and press the Enter key from the keyboard. It will mark nulll character ('\0') in the memory at the end of the string, when you press the enter key. The general syntax is as:

gets(v);

where v is the variable of character type. For example:
char n[20];
gets(n);

A simple C program to illustrate the use of gets() function:

/*Program to explain the use of gets() function*/
#include
main()
{
char n[20];
gets(n);
}


d) puts()

    This is an output function. It is used to display a string inputted by gets() function. It is also used to display an text (message) on the screen for program simplicity. This function appends a newline ("\n") character to the output.


The general syntax is as:
puts(v);
or
puts("text line");

where v is the variable of character type.
A simple C program to illustrate the use of puts() function:

/*Program to illustrate the concept of puts() with gets() functions*/
#include
main()
{
char name[20];
puts("Enter the Name");
gets(name);
puts("Name is :");
puts(name);
}

OUTPUT IS:
Enter the Name
Laura
Name is:
Laura


e) getch()

    This is also an input function. This is used to read a single character from the keyboard like getchar() function. But getchar() function is a buffered is function, getchar() function is a non-buffered function. The character data read by this function is directly assign to a variable rather it goes to the memory buffer, the character data directly assign to a variable without the need to press the Enter key.
Another use of this function is to maintain the output on the screen till you have not press the Enter Key. The general syntax is as:

v = getch();

where v is the variable of character type.
A simple C program to illustrate the use of getch() function:

/*Program to explain the use of getch() function*/
#include
main()
{
char n;
puts("Enter the Char");
n = getch();
puts("Char is :");
putchar(n);
getch();
}

OUTPUT IS:
Enter the Char
Char is L
f) getche()

    All are same as getch(0 function execpt it is an echoed function. It means when you type the character data from the keyboard it will visible on the screen. The general syntax is as:

v = getche();

where v is the variable of character type.
A simple C program to illustrate the use of getch() function:

/*Program to explain the use of getch() function*/
#include
main()
{
char n;
puts("Enter the Char");
n = getche();
puts("Char is :");
putchar(n);
getche();
}

OUTPUT IS:
Enter the Char L
Char is L



Formatted I/O functions

    Formatted I/O functions which refers to an Input or Ouput data that has been arranged in a particular format. There are mainly two formatted I/O functions discussed as follows:

a) scanf()
b) printf()

a) scanf()

    The scanf() function is an input function. It used to read the mixed type of data from keyboard. You can read integer, float and character data by using its control codes or format codes. The general syntax is as:

scanf("control strings",arg1,arg2,..............argn);
or
scanf("control strings",&v1,&v2,&v3,................&vn);
Where arg1,arg2,..........argn are the arguments for reading and v1,v2,v3,........vn all are the variables.
The scanf() format code (spedifier) is as shown in the below table:

Format CodeMeaning
%cTo read a single character
%dTo read a signed decimal integer (short)
%ldTo read a signed long decimal integer
%eTo read a float value exponential
%fTo read a float (short0 or a single precision value
%lfTo read a double precision float value
%gTo read double float value
%hTo read short integer
%iTo read an integer (decimal, octal, hexadecimal)
%oTo read an octal integer only
%xTo read a hexadecimal integer only
%uTo read unsigned decimal integer (used in pointer)
%sTo read a string
%[..]To read a string of words from the defined range
%[^]To read string of words which are not from the defined range


/*Program to illustrate the use of formatted code by using the formatted scanf() function */ #include
main()
{
char n,name[20];
int abc;
float xyz;
printf("Enter the single character, name, integer data and real value");
scanf("\n%c%s%d%f", &n,name,&abc,&xyz);
getch();
}

b) printf()

    This ia an output function. It is used to display a text message and to display the mixed type (int, float, char) of data on screen. The general syntax is as:

printf("control strings",&v1,&v2,&v3,................&vn);
or
printf("Message line or text line");
Where v1,v2,v3,........vn all are the variables.
The control strings use some printf() format codes or format specifiers or conversion characters.


These all are discussed in the below table as:

Format CodeMeaning
%cTo read a single character
%sTo read a string
%dTo read a signed decimal integer (short)
%ldTo read a signed long decimal integer
%fTo read a float (short0 or a single precision value
%lfTo read a double precision float value
%eTo read a float value exponential
%gTo read double float value
%oTo read an octal integer only
%xTo read a hexadecimal integer only
%uTo read unsigned decimal integer (used in pointer)


/*Below the program which show the use of printf() function*/ #include
main()
{
int a;
float b;
char c;
printf("Enter the mixed type of data");
scanf("%d",%f,%c",&a,&b,&c);
getch();
}




<< Previous Topic
Next Topic >>