Let us learn C in Code <6> _printf()

来源:互联网 发布:直播电影的软件 编辑:程序博客网 时间:2024/06/14 14:42

Now , last chapter told us that if u wanted our pc to output something, we can use the printf() function, but that's just the one thing we want, much more important thing we cared is how the printf() function to output the result of the calculation based on math problem like addition, subtraction, multiplication and division. Surely ,in this chapter, let us learn it. Before we used printf() function, as previous chapter said, the printf() function can output the characters included some special characters like '\n', '\a','\0' etc. If you have write down them in your source files and compile and execute , you may see a jump out windows (if your operation system is windows you will see a windows titled XXX.exe)which output the characters you wrote between the double quotes  printf() function. For example, if write like this 

main()

{

printf("Hi , everyone!");

printf("Hi , everybody!\nI'm a new line!);

}


It will output like belows      

Hi , everyone!

Hi , everybody!

I'm a new line!

From above characters, we know that the '\n' characters is a control character which will cause a new line, if you use '\a' and turn up the  volume of our pc , we will listen a sound like "beep" or "boom" , just like this sound i can't describe it well. If you want to hear it, just add some character in your printf() function, but don't forget the double quotes.

Ok, from above code , we know that if we want printf() function to output the result , we just need to add some append commands to the printf() function. Surely, these special command named format code list the below table,

--------------------------------------

Format           means

-------------------------------------

%d                output decimal

%x                 output Hexadecimal

%s                output string

%c                output character

%f                 output float

%p               output pointer

...                  .....

-------------------------------------


And the base format ,in printf() function like this

printf("The integer is %d",inte);

The 'inte"' is a variable declared before the printf() function, if you define 'inte' is a integer variable and equals 10 .When your program is executed, the output characters include the "10". Now let us code it,

main()

{

int inte = 10;

printf("The integer is %d",inte);

}

It output     The integer is 10  , surely you can output the difference result in the same printf() function, just add the format code , then as the same sequence add the variables, as the same time don't forget the comma . Let us output multiple numbers,

main()

{

int inte = 10;

float fnum = 9.5;

char cnum = '!';

printf("The integer is %d, the float one is %f, \n the character one is %c",inte,fnum,cnum);

}

Output

The integer is 10, the float one is 9.50000,

the character one is !


Ok, before over this chapter , please solve the problem  equation like this "y = x^2 + 3*x + 8" , if you assign a value x ,it calculate the y , then output the result of the 'y' .Any data type is ok . The "x^2" means two power of x or you can change it to "x*x".

Have a nice day ! If you have any question just leave me a message. 





0 0