Introduction To C
The C Programming Language is also called the Mother of languages. The C language was developed by Dennis Ritchie between 1969 and 1973 and is a second and third generation of languages. The C language provides both low and high level features it provides both the power of low-level languages and the flexibility and simplicity of high-level languages.
C provides standard functions scanf() and printf(), for performing formatted input and output .These functions accept, as parameters, a format specification string and a list of variables.
The format specification string is a character string that specifies the data type of each variable to be input or output and the size or width of the input and output.
Now to discuss formatted output in functions.
Formatted Output
The function printf() is used for formatted output to standard output based on a format specification. The format specification string, along with the data to be output, are the parameters to the printf() function.
Syntax:
printf (format, data1, data2,……..);
In this syntax format is the format specification string. This string contains, for each variable to be output, a specification beginning with the symbol % followed by a character called the conversion character.
Example:
printf (“%c”, data1);
The character specified after % is called a conversion character because it allows one data type to be converted to another type and printed.
See the following table conversion character and their meanings.
Conversion Charr | Mning |
d | The data is converted to decimal (integer) |
c | The data is taken as a character. |
s | The data is a string and character from the string , are printed until a NULL, character is reached. |
f | The data is output as float or double with a default Precision 6. |
Symbols | Meaning |
\n | For new line (linefeed return) |
\t | For tab space (equivalent of 8 spaces) |
Example
printf (“%c\n”,data1);
The format specification string may also have text.
Example
printf (“Character is:”%c\n”, data1);
The text "Character is:" is printed out along with the value of data1.
Example with program
- #include<stdio.h>
- #include<conio.h>
- Main()
- {
- Char alphabh="A";
- int number1= 55;
- float number2=22.34;
- printf(“char= %c\n”,alphabh);
- printf(“int= %d\n”,number1);
- printf(“float= %f\n”,number2);
- getch();
- clrscr();
- retrun 0;
- }
- Output Here…
- char =A
- int= 55
- flaot=22.340000
What is the output of the statement?
printf(“Integer is: %d; Alphabet is:%c\n”,number1, alpha);
Where number1 contains 44 and alpha contains "Krishna Singh".
Give the answer below.
Between the character % and the conversion character, there may be:
- A minus sign: Denoting left adjustment of the data.
- A digit: Specifying the minimum width in which the data is to be output, if the data has a larger number of characters then the specified width occupied by the output is larger. If the data consists of fewer characters then the specified width, it is padded to the right or to the left (if minus sign is not specified) with blanks. If the digit is prefixed with a zero, the padding is done with zeros instead of blanks.
- A period: Separating the width from the next digit.
- A digit following the period: specifying the precision (number of decimal places for numeric data) or the maximum number of characters to be output.
- Letter 1: To indicate that the data item is a long integer and not an int.
Format specification string Data Output |%2d| 9 |9| |%2d| 123 |123| |%03d| 9 |009| |%-2d| 7 |7| |%5.3d| 2 |002| |%3.1d| 15 |15| |%3.5d| 15 |0015| |%5s| “Output sting” |Output string| |%15s| “Output sting” |Output string| |%-15s| “Output sting” |Output string| |%15.5s| “Output sting” |Output string| |%.5s| “Output sting” |Output| |%15.5s| “Output sting” |Output| |%f| 87.65 |87.650000| |%.4.1s| 87.65 |87.71|
- #include<stdio.h>
- #include<conio.h>
- main()
- {
- Int num=65;
- printf(“Value of num is : %d\n:, num);
- printf(“Character equivalent of %d is %c\n”, num , num);
- getch();
- clrscr();
- rerurn o;
- }
char =A
int= 55
flaot=22.340000
Formatted Input
The function scanf() is used for formatted input from standard input and provides many of the conversion facilities of the function printf().
Syntax
scanf (format, num1, num2,……);
The function scnaf() reads and converts characters from the standards input depending on the format specification string and stores the input in memory locations represented by the other arguments (num1, num2,….).
For Example:
scanf(“ %c %d”,&Name, &Roll No);
Note: the data names are listed as &Name and &Roll No instead of Name and Roll No respectively. This is how data names are specified in a scnaf() function. In case of string type data names, the data name is not preceded by the character &.
Example with program
Write a function to accept and display the element number and the weight of a proton. The element number is an integer and weight is fractional.
Solve here:
- #include<stdio.h>
- #include<conio.h>
- main()
- {
- Int e_num;
- Float e_wt;
- printf (“Enter the Element No. and Weight of a Proton\n”);
- scanf (“%d %f”,&e_num, &e_wt);
- printf (“The Element No.is:”,e_num);
- printf (“The Weight of a Proton is: %f\n”, e_wt);
- getch();
- return 0;
- }
0 comments:
Post a Comment