Friday 3 October 2014

Suppression Character in Scanf()

Suppression Character in Scanf()

If we want to skip any input field then we specify * between the % sign and the conversion specification. The input field is read but its value is not assigned to any address. This character * is called the suppression character. For example-

scanf("%d %*d %d",&a, &b, &c);

Input 

a=25 , b=30 ,c=35

Here 25 is stored in a, 30 skipped and 35 is stored in the b, since no data is available for c so it has garbage value.

scanf("%d %*c %d %*c %d",&d, &m, &y);

Input :

3/1/2003

Here 3 will be stored in d, then / will be skipped , 11 will be stored in m, again / will be skipped and finally 2003 will be stored in y.

#include<stdio.h>
main()
{
int a, b, c;
printf("Enter three number:");
scanf("%d %*d %d",&a,&b,&c);
printf("%d %d %d", a,b,c);
}

Output

Enter three number: 25 ,30 ,35

25  35  25381

the variable c has garbage value.

Formatted Input and Output

Formatted Input and Output


Formatted input and output means that data is entered and displayed in a particular format. Through format specifications, better presentation of result can be obtained.

Format For Integer Input

%wd :- Here is the conversion specification character for integer value and 'w' is an integer number specifying the maximum field width of input data. if the length of input is more than this maximum field width then the value are not stored correctly. e.g.

scanf("%2d%3d",&a,&b);

"when input data length is less than the given width, then the input values are unaltered and stored in given variable."

Input
6,39

Result 

6 is stored in a and 39 is stored in b.
**************************************************************
scanf("%2d%3d",&a,&b);

Input 
26, 39

Result

26 is stored in a and 394 is stored in b.
*************************************************************

scanf("%2d%3d",&a,&b);

Input

269 , 3845

Result 

26 is stored in a and 9 is stored in b and the rest of input is ignored. the reason of ignoring that %2d and %3d , variable a range of storing the value length is 2 digit and variable b is 3 digits because %wd  is responsible for this.
*************************************************************

Format For Integer Output 

Here w is the integer number specifying the minimum field width of the output data. if the length of the variable is less than the specified field width, then the variable is right justified with leading blanks.

For example-

printf("a=%3d, b=%4d", a,b);

when the length of variable is less than the width specifier.

Value of variable

78 9

Output

here * is space
a= *78, b=***9

the width specifier of first data is 3 while there are only 2 digits in it, so there is one leading blank. The width specifier of second data is 4 while there is only 1 digits, so there are 3 leading blanks.  
******************************************************

  • when the length of the variable is equal to the width specifier

value of variable 

263   1941

Output

a=263, b=1941

  • When length of variable is more than the width specifier, than also the output is printed correctly.
value of variables-

2691   19412

Output

a=2691, b=19412

example-

#include<stdio.h>
main()
{
int a=4000, b=200, c=15;
printf("a=%d\n b=%d\n c=%d\n,a,b,c);
printf("a=%d\n b=%d\n c=%d\n,a,b,c);
}

Output of the first printf would be

a=4000
b=200
c=15

while the second output of prinf

a=4000
b=200
c=15

*******************************************************************************
*******************************************************************************


Format For Floating point Numeric Input-

%wf :- Here 'w' is the integer number specifying the total width of the input data (including the digits before and after decimal and the decimal itself ).

scanf("%3f %4f",&x , &y);

when input data length is less than the given width, value are unaltered and stored in the variables.

Input  :    5  , 5.9

Output / result: 5.0 is stored in x and 5.90 is stored in y.
********************************************************

scanf("%3f %4f",&x , &y);

When  input data length is equal to the given width, then value are unaltered and stored in the given variables.

Input  : 5.3 , 5.92

Result  :  5.3 stored in x and 5.92 is stored in y.
*******************************************************

scanf("%3f %4f",&x , &y);

When input data length is more than the given width then the given values are altered and stored in the given variable.

Input   :  5.93 ,  65.87

Result  :  5.9 is stored in x and 3.00 is stored in y. 

**************************************************************
**************************************************************


Format For Floating point Numeric Output

%w.nf :- here w is the integer number specifying the total width of the input data and n is the number of digits to be printed after decimal point. By default 6 digits are printed after the decimal. for example 

printf("x=%4.1f, y=%7.2f", x,y);

if the total length of the variable is less than the specified width 'w' then the value is right justified with leading blanks. if the number of digits after decimal is more than 'n' then the digits are rounded off.

Value of variable

a.   8, 5.9
b.   25.3,1635.92

Output :- * used like a blank space

a.  x=*8.0, y=***5.90

b.  x=25.3, y=1635.92




Method of Output

Writing Output Data-

Output data can be written from computer memory to the standard output device(monitor) using printf() library function. Output is the method to display the massage or input variable on the screen so that user cab see our program result, using printf() function.

There are two way to using printf() function in C language-
first way when we want to display only information and massage then we use the formate-

Syntax- printf(" write massage here:");
E.g.

printf('' Hi! I am sudhir Singh:");

When we want only display the massage in the program, then we write the massage in printf function in double quotes as above in the example......

the second way we want display the value of variable that is hold at the time of input means through the scanf() function. when we want to display the value of variable then use control string as used in scanf() function but major different is,in the printf function we did not use & sign with the variable because & sign used for create memory but here use only display the value of variable so only use the variable in the printf function. 

Syntax- printf("control string",variable 1, variable 2,.........);

e.g.

printf("%d%d", a,b);
here two variable a and b that is display the variables value, we did not use the & sign with the variable a and b. when we display the information then conversion specification is not used.

Example :

#include<stdio.h>
main()
{
int age;
printf("Enter any age for any one");
scanf("%d",&age);
printf("%d",age);


*******************************************************
#include<stdio.h>
main()
{
int basic=2222;
.................
printf("%d",basic);
.................
}

in this example control string contains a conversion specification %d which implies that an integer value will be displayed. The variable basic has that integer value which will be displayed as output.


*******************************************************************************
#inlude<stdio.h>
main()
{
float height=5.6;
printf("%f",height);
}

Here control string has conversion specification character %f, which means that floating point number will be displayed. The variable height has that floating point value will be displayed as output .

**************************************************************
#include<stdio.h>
main()
{
char ch='$';
printf("%c",ch);
}

In this example the control string has conversion specification character %c means that a single character will be displayed and variable ch has that character value.


**************************************************************
#include<stdio.h>
main()
{
int b=500;
float h=200.50;
char g='A';
printf("basic =%d\n hra=%f\n grade=%c\n", b,h,g);
}

Output

Basic=500
hra  = 200.50
grade=A

in this example we print the massage and value together format is above in the program, in the printf function '\n' used, '\n moves the cursor to the beginning of next line.Here we have placed a '\n' at the end of control string also. it ensure that the output of the next program starts at a new line.

**************************************************************



#include<stdio.h>
main()
{
int b=500;
float h=200.50;
char g='A';
printf("basic =%d\t hra=%f\t grade=%c\n", b,h,g);
}

Output

Basic=500     hra=200.50     g=A

in this example '\t' moves the cursor to the next tab stop.
for example we can use: \b moves the cursor one position back, \r moves the cursor to the beginning of the current line and \a alert user by a beep sound.\v moves the cursor to the next vertical tab position  and \f moves the cursor to the next page.
If we want to print character like single quote(') and double quote (") or the backslash character (\), then we have to precede them by a backslash character in the format string.

For example-

printf("10\\ 04 \\1992");       then print         10 \ 04 \1992

printf(" she said,\" sudhir mad\"."); then    she said,"sudhir mad".







Wednesday 1 October 2014

Reading Data in C

Input Data in C

Input data can be entered into the memory from a standard from a standard input device(keyboard). C provide scanf() library function for entering input data. this scanf() can take all type of values ( numeric, character, string, float etc) as input.

Syntax of sacnf():  

scanf("control string/conversion specification",address1 , address2,........n);

This function should have at least two parameters, First parameter is a control string, which contains conversion specification characters. It should be within double quotes. The conversion specification character may be one or more. number of conversion specification depends on number of variable/address in the scanf ().if we use 2 address then use 2 conversion specification, three address, three conversion specification. it depends on the number of variable we want to input. the other parameter are addresses of variables. in the scanf() function at least one address should be present.The address of variable is found by preceding the variable name by an ampersand(&) sign. This sign is called address operator and it gives the starting address of the variable name in memory. A string variable is not preceded by &sign to get address. discus in String chapter.
 let's write a input code......

#include<stdio.h>
main()
{
int marks;
..............
scanf("%d",&marks);
..............
}
in above example the control string contains only one conversion specification character %d, which implies that one integer value should be entered as input. This entered value will be stored in the variable marks.

**************************************************************

#include<stdio.h>
main()
{
char ch;
scanf("%c",&ch);
..............
}

here the control string contains conversion specification character %c, which means that a single character should be entered as input. This entered value will be stored in the variable ch.

**************************************************************

#include<stdio.h>
main()
{
float fee;
scanf("%f",&fee);
}

here the control string contains conversion specification character %f, which means that a floating point number should be entered as input. This entered value will be stored in the variable fee.

**************************************************************

#include<stdio.h>
main()
{
char str[30];
scanf("%s",str);
}

In this, String has conversion specification character %s implying that a string should be taken as input. Note that the variable str is preceded by & sign. The entered string will be stored in the variable str.

**************************************************************


More than one value can also entered by scanf() function-

#inclue<stdio.h>
main()
{
int basic, da;
...........
scanf("%d%d",&basic,&da);
..........
}
in this example the control string has two conversion specification character imply that two value should be entered. These values are stored in the variable basic and da. the data can be entered with space as the delimiter.Number of   conversion specification is responsible for number of variable.

**************************************************************


How to input different type of value 


#inclue<stdio.h>
main()
{
int basic;
float hra;
char grade;
................
scanf("%d %f %c",&basic,&hra,&grade);
...............
}

Here the control string has three conversion specification characters %d %f and %c, means that one integer value, one floating point value and one single character can be entered as input. These values are stored in the variables basic, hra and grade. The input data can be entered as-
1500 , 200.50, A.
When more than one value are input by scanf(),These values can be separated by whitespace character like space. tab or newline. A specific character can be also be placed between two conversion specification character.

#include<stdio.h>
main()
{
int basic ;
float hra;
scanf("%d : %f",&basic, &hra);
}
here the delimiter is colon(:). the input data can be entered as 
1500:200.5

the value 1500 is stored in variable basic and 200.50 is stored in hra.

#inclue<stdio.h>
main()
{
int basic;
float hra;
scanf("%d,%f",&basic,&hra);
}

Here the delimiter is comma (,). The input data can be enterd as-
1500, 200.40

#include<stdio.h>
main()
{
int day,month,year;
int basic;
.............
scanf("%d-%d-%d",&day,&month,&year);
scanf("$%d",&basic);
............
}
here if the data is entered as-
24-5-1992
$2000

Then 24 is stored in variable day, 5 is variable month and 1992 is stored in the variable years and 2000 stored in the variable basic.

#inclue<stdio.h>
main()
{
int x, y, z;
scanf("%d %d %d",&x ,&y,&z);
}
  if the data is entered as
12  34  56
12 is stored in x, 34 is stored in y and 56 is stored in z;

Conversion Specification and intro input/output

Input data , process data , and output process data. there are three main function of any program. The input operation involve movement of data moves from an input device (like keyboard) to computer memory, while in output operation the data moves from computer memory to the output device (monitor).  
     C language does not provide any facility for input/output operations. The input and output operation perform through a library function that are used by compiler. we will discus library function in the potion of Function. in short, "library function is the predefine function, which is written by programmer, only we call that function and use in the program". before using library function in the program. we must include header file in the program, in which specific function is stored. for example: for input and output operations we include header file <stdio.h> "stdio" stands for standard input output operation and .h stands for header file. all input output operation perform through this function. Scanf(); and printf() is the member of stdio.h library function. how to take input and output data in C language, we must know what is Conversion Specification.

Conversion Specification

The function scanf( ) and printf( ) make use of conversion specification to specify the type and size of data. Conversion specification is the way that specify and control input and output data. data type size and type depend on this. Each conversion specification must begin with a percent sign(%). some conversion specification give below-
%c              -     read/output a single character.
%d                    read/output integer value.
%f                     read/output floating point number.
%e                    read/output floating point number
%lf                    read/output long rang of float.
%h                    for short integer.
%o                    for octal integer.
%u                    an unsigned integer.

for eg. to read data .

sacnf("%d",&a);

in above example %d specify that the input data is integer that is stored in variable 'a'. & is used for address, with variable it create a memory location for variable, depend on data type. we declare float type variable then & create 4 byte on memory for variable. if we declare integer type variable then it create 2 byte memory.


Tuesday 30 September 2014

Structure of C

Structure of C language

Before stating in c programming,we must to know structure of c. it is very important. 
                 C program is a collection of one or more functions. Every function is a collection of statement and perform some specific task. The general structure of C.


Comments

preprocessor 
global variable
main() function
{
local variable
statements
...............
..............
................
}


function( )

{
local variables
statements
................
...............
}


function 2 ( )

{
local variables
statements
........
........
...........
}



Comment can be placed anywhere in a program and are enclosed between the delimiter /* and */ . this is called single line comment. // this is called multiple line comment. comment are generally used for documentation purposes.


prepossor directive are processed through prepocessor before the C source code passes through compiler. The commonly used preposser directive are #include and #define. # include is used for including header files. #define is used to define symbolic constants and macros.

Every C program has one or more functions.if a program has only one function then it must be main( ). Execution of every C program starts with main( ) function. it has two parts, declaration of local variables and statements. The scope of the local variable is local to that function only. Statement in the main () function are executed one by one. Other function are the user defined functions, which also have local variable and C statements.they can be defined before or after main(). It may be possible that some variable have to be used in many function, so it is necessary to declare them globally. These variable are called global variables.

Sunday 28 September 2014

Variable in C language

What is variable ?


Variable is the name or user define name that can be used to store values. Variable can take different type of values but it stores only one value at a time. These values can be changed during the execution of program. A data type is associated with each variable. The data type of the variable decides what values it can take or store.

Rule for naming variable-
1     
       The name should be consist of only alphabets (both upper and lower          case),digits and underscore sign.
     
        First character should be alphabets and underscore.
     
        The name should not be keyword.
   
        Since c is the case sensitive, the upper case and lower case are considered         different for e.g. Apple, APPLE both different.

Declaration of variable


We know that what is variable, it must to declare a variable before it is used in the program. Declaration of a variable specifies its name and data type. The type and range of values that a variable can store depends upon its data type.
Initialization of Variable-
When a variable is declare it contain undefined value known as garbage value.If we want we can assign some initial value to the variable during the declaration of variable. this is called initialization of the variable. for example: 
float x=8.9, y=98.0

Syntax for declaration of variable-

Data type <variable name ;>
Or
Data type <variable name1, variable name2 … n ;> 

For example-
                  
                   Int x;                       // declare variable named ‘x’

                   Float salary               // declare variable ‘salary’
                  
                   Char grade               // ‘grade’ is the variable

Here x is a variable of type int. this mean that x will take  the value of integer such as 12, 3, 4,etc. 
salary is a variable of type float, it take a value type of float like 3421.543 etc. and grade is a variable of type char. char means character it takes the value if character such as A, a, B, b, C,c etc.

declaration of mare than one variable-

int x,y,z,total; 

Here x, y,z and total  are all variable and type of variable is int / integer because all variable declare with int data type after declare variable we must terminate the variable with semicolon. in the upper example x, y,z,total are 4 variable x y z and total separate by the comma and variable declaration end by (;)semicolon. if we are not terminate the variable with semicolon our program generate a error. 



int a=5;
here a=5 means that 5 is assign to the variable a, if we write 'a' in all the program, 'a' means we write 5. 'a' and 5 both are equal after assigning the value. other initialization....

here x value is 8.9 and y value is 98.0 if we write x then its equal to 8.9 and if we write y then it is equal to 98.0.
char ch='y'
double num =0.4324;
int l,m,u,totle=0;
in the declaration only one variable is initialize.