Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use character data and floating point data in C language

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to use character data and floating point data in C language". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use character data and floating point data in C language.

Character type

First of all, you need to understand the ASII code, there are a lot of things in it, but you just have to remember three things to deduce the rest.

0 corresponds to 48, A corresponds to 65, and a corresponds to 97.

So 1 corresponds to 49, B corresponds to 6, and b corresponds to 98.

And then, and so on.

The char below is essentially a 1-byte integer.

1. To practice, output a character A#include int main () {/ / defines a character variable, char represents a character variable char ch ='A'; / /% c is a placeholder and outputs a character variable printf ("% c\ n", ch); / / return 0 / * the ruturn here means the end of the program, it doesn't matter whether you write it or not, but programmers still suggest that you write * /} run the result.

two。 Convert lowercase letters to uppercase letters * # include / / convert lowercase letters to uppercase letters int main () {char ch; scanf ("% c", & ch); / * just remember these three words in ASII code, that is, 0 corresponds to 48 char A corresponds to 65 ch a corresponds to 97, and so on * / / char type can operate with integers ch = ch-32 / * because a corresponds to 97 printf 97-65 equals 32, there is a difference of 32 units * / min ("% c\ n", ch);}

Running result

Floating point type

Floating-point type is mainly divided into single-precision floating-point float and double-precision floating-point double.

Double floating point type is more accurate than float floating point type.

Let's first take a look at the float code # include int main () {/ / single-precision floating-point float pi = 3.141558; / /% f is a placeholder for outputting a floating-point data / / printf ("% f", pi) / * floating-point type retains six decimal places by default. 2f data will be rounded * / printf ("% .2f\ n", pi);} run result

If the above is printed directly with printf, it will output something like this.

Because floating-point models retain six decimal places by default, but usually two decimal places are retained in exams, two decimal places are retained by * *% .2f * *.

The following is a double-precision floating-point double

Exercises

OK, after seeing so much, it's time to face our big BOSS. Let's do an exercise, consolidate it by the way, and see for ourselves, ha.

Calculate the area and perimeter of a circle

# include int main () {/ / single-precision floating-point float pi = 3.141558; / /% f is a placeholder for outputting floating-point data / / printf ("% f", pi); / * floating-point data defaults to six decimal places and two decimal places. 2f data is rounded * / printf ("% .2f\ n", pi) } I typed a 5 on the keyboard and take a look at the results.

At this point, I believe you have a deeper understanding of "how to use character data and floating point data in C language". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report