?? the c programming language torn apart.html
字號:
<P>g, G	Floating Point Number in either fixed decimal or exponential form, </P>
<P>e, E	Floating Point Number in Exponential Form</P>
<P> </P>
<P>And the precision is the number of places which the output numeral of the Type will have. Like I said before, all this would definitely sound a bit too overwhelming for a complete newbie, but stick with me and keep reading the following examples and I assure you, all your doubts would be cleared.</P>
<P>Examples:</P>
<P>printf ("sum = %10d \n", result);</P>
<P>if the value of the variable result is 145, then the output will be:</P>
<P>sum = 145</P>
<P>In this case, due to the format options we gave, the C program assumes that the value stored by the variable result is a decimal Number [Specified by d] and displays the output i.e. the vale of the variable result in a field of 10 characters wide. [ Specified by %10]. As the width in this example is positive, [10] the output is left aligned. Now let us say if we change the above line of code to the following:</P>
<P>printf ("sum = %-10d \n", result);</P>
<P>then everything else remains the same, only the output instead of being left aligned, is right aligned.</P>
<P>printf ("Percentage = %10.3f", percent);</P>
<P>Assuming that the value of the variable percent is 2.345, the output will be:</P>
<P>Percentage = 2.345</P>
<P>Let's take a bit more complex example which includes escape Sequences.</P>
<P>printf ("\t%2d:%2d %c \n", hours, minutes, time);</P>
<P>Assuming that the value of hours is 11, value of minutes is 45 and the value of time is PM, the output would be:</P>
<P>	11:45 PM</P>
<P>Consider the following example:</P>
<P>printf ("%s \t %6.2f \t", item, price);</P>
<P>Assuming that the value of item is CD and the value of price is 100.25, then the output would be:</P>
<P>CD	100.25</P>
<P>Well, till now I am sure almost all of you must be clear in your mind, as to how the printf function can be successfully used. However, if you still have any questions, feel free to contact me.</P>
<P>Gathering Input: The scanf( ) routine</P>
<P>Just like we have the printf( ) function to display output on the screen, we have the scanf( ) function to gather input from the user and to add interactivity to our programs. Before I move on to the syntax and other information on the scanf( ) routine, you need to understand the difference between a variable and the memory location of a variable.</P>
<P>You see, whenever we declare a variable, the C program keeps a part a specific part of the memory for it. Now say we declare a variable and name it ankit and give it the null value i.e. nothing. Now when we give the following print command:</P>
<P>printf ("Ankit's value is: %s", ankit);</P>
<P>then the output would be:</P>
<P>Ankit's value is: NULL</P>
<P>Remember that the variable ankit has been assigned a memory location to store whatever value we want to assign it. Right? Well to refer to this memory location set apart of the variable ankit, we need to make use of the address operator i.e. the & operator. So giving the following command will print the memory address assigned to the variable ankit:</P>
<P>printf ("%s", &ankit);</P>
<P>Now that you know when the address operator is used, let us move on to the basic syntax of the scanf command:</P>
<P>scanf ("Specification", addresses);</P>
<P>where specification is a set of rules which decides the kind of input the program expects the User to type and addresses is a set of memory locations to which the Input is to be stored. </P>
<P>The specification part is nothing but the formatting options which the printf( ) routine has. It decides whether the program is looking for decimals, floating points or characters as input. It also decides the maximum number of characters which can be accepted as input. Basically the syntax of Specification is as follows: </P>
<P>%[width] type</P>
<P>where width specifies the maximum number of characters which can be accepted as input. If the User tries to input more number of characters than specified by width, the program does not accept them and the input cursor does not move ahead. </P>
<P>Type can be anything from the following list of values-:</P>
<P> </P>
<P>d,	Signed Decimal Number</P>
<P> i 	Signed Integer whose base(Type of Number System) is decided by the format of the number input:</P>
<P>	If the prefix is 0 it expects an octal integer</P>
<P>	If prefix is 0x then it expects a Hexadecimal Integer</P>
<P>	Else if there is no prefix, then it expects a decimal integer</P>
<P>c	Character</P>
<P>s	String</P>
<P>o	unsigned Octal</P>
<P>x, X	unsigned Hexadecimal</P>
<P>u	unsigned Decimal Integer</P>
<P>e, f , g 	Signed Floating Point Value</P>
<P>l	This is used to prefix e, f, g and signifies a long integer or unsigned long integer</P>
<P>L	This is used to prefix e, f, g and signifies a long double	</P>
<P> </P>
<P>Let us take some examples to make this routine clearer.</P>
<P>Examples: </P>
<P>scanf ("%d", &pie);</P>
<P>will take a decimal from User Input and store it in the address of the variable ' pie '.</P>
<P>scanf ("%10s", &name);</P>
<P>will take the first 10 characters from the User Input and store them in the address of the variable ' name '.</P>
<P>scanf ("%d%c%lx", &dec, &stringvar, &longvar);</P>
<P>will take a decimal integer, a single character and an unsigned Long Hexadecimal number and assigns them to the addresses of the variables dec, stringvar and longvar.</P>
<P>NOTE: Please, note the Address Operator before the variable name in each of the above examples. Without the use of this operator the program will not work and the variable will not be assigned any value. Somehow, we do not need to use the Address Operator while printing the value stored by a variable. Just remember that while using scanf, you need to use the address operator and with printf, no address operator needs to be used. There is no need to go deep into the reasons behind this.</P>
<P>Wasn't that easy? Well C is not as difficult as it is projected to be and I am sure all experienced C programmers agree with me on that fact. Anyway, till now we have learnt how to print the value stored by a variable and also how to get input from the user and assign it to a variable. But, these routines are of no use if we do not know how to declare variables. In C, we cannot declare and assign values to variables at the same time. You need to first declare a variable and only then can you assign it a value. So let us know learn how to do just that.</P>
<P>Variables</P>
<P>In C, all variables must be declared before they can be used or assigned a value. Variables are usually declared in the beginning of a function i.e. before any executable commands or routines start. A variable declaration is of the following format:</P>
<P>Variable_Type Variable_Name</P>
<P>where Variable_Type is the type is the type of variable, which symbolizes the type of data stored by the declared variable(int, char, float etc) , and Variable_Name is the name of the variable being declared.</P>
<P>A typical variable declaration would be:</P>
<P>int number;</P>
<P>The above line declared a variable by the name number and declared it to store an integer. We can declare more than a single same type of variable in a single line:</P>
<P>int number, number1, number2;</P>
<P>This line declares three Integer variables: number, number1 and number2.</P>
<P>C supports the following basic data types:</P>
<P>int	Integer</P>
<P>float	Floating Point (Numbers having a fractional Part)</P>
<P>char	Single Character (Single Byte like a, b, c etc)</P>
<P>short	Short Integer</P>
<P>long	Long Integer</P>
<P>double	Double Integer</P>
<P>NOTE: The range of int and float varies from system to system. And these are not the only kind of data types. We still have arrays, pointers, structures etc which we will discuss later.</P>
<P>Now, that we have learnt how to declare variables, we need to know how to assign values to them. Well, to assign values to variables, we use the assignment operator, i.e. The ' = ' operator.</P>
<P>For example,</P>
<P>int number;</P>
<P>number = 20;</P>
<P>declares a variable by the name number and type Integer and then the second line assigns it the value 20.</P>
<P>My First Useful Working Program</P>
<P>Now that we know a bit about I\O and also a bit about variables, we are in a position of creating out first useful working C program!!!. This program will ask the user to type the temperature in Fahrenheit and will convert it into Celsius and print the result on the screen. However before we move on, we need to cover a tiny detail: Comments. Anything between a ' /* ' and a ' */ ' is called a comment and is ignored by the compiler. They are inserted so that people find it easier to read the code and understand what each line is meant to do. The following example, has a lot of comments.</P>
<P>#include <stdio.h> /* Include the Standard I/O Header File */</P>
<P>main() { /*Start the Main Function, which is executed automatically */</P>
<P> int fah, cel;	/* Declare the (int) variables which will hold the Fahrenheit and Celsius Equivalents */</P>
<P> printf ("Enter Temperature in Fahrenheit:"); /* Hello User. I am hungry, feed me a value */</P>
<P> scanf ("%d", &fah); /* Get the User Input and store it in the address of fah */</P>
<P> cel = (Fah -32) * 5 / 9;			 /* Get the Grey Cells Working, convert input to Celsius */</P>
<P> printf ("\n \n Temperature in Celsius is
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -