简体   繁体   中英

Is long a data type or qualifier in C?

I am reading Programming in C by Stephan G. Kochan . He states that C has only five data types; int , float , double , char and _Bool .

What about long ? Isn't it a builtin type? http://www.programiz.com/c-programming/c-data-types says long is a qualifier to modify the size. If it is a qualifier then it should be only used as a long int , and not as a standalone long .

And what about _Bool ? Many Internet tutorials say there is no boolean type in C.

Related:

He states that C has only five data types; int, float, double, char and _Bool.

That's quite an over-simplification. Maybe intentional, if the book is aimed towards beginners.

If you go through C11 6.2.5 it lists the following distinct data types:

Character types (6.2.5/15)

char
signed char
unsigned char

Standard signed integer types (6.2.5/4)

signed char
short int
int
long int
long long int

Standard unsigned integer types (6.2.5/5)

_Bool
unsigned char
unsigned short int
unsigned int
unsigned long int
unsigned long long int

Real floating types (6.2.5/10)

float
double
long double

Complex types (6.2.5/11)

float _Complex
double _Complex
long double _Complex

Enumerated type (6.2.5/16)

enum {}

void type (6.2.5/19) (void type is an incomplete type)

void

Derived types (6.2.5/20)

  • Array type
  • Structure type
  • Union type
  • Function type
  • Pointer type
  • Atomic type

Formally the term is type specifier 6.7.2:

type-specifier:
void
char
short
int
long
float
double
signed
unsigned
_Bool
_Complex
atomic-type-specifier
struct-or-union-specifier
enum-specifier
typedef-name

At least one type specifier shall be given in the declaration specifiers in each declaration, and in the specifier-qualifier list in each struct declaration and type name. Each list of type specifiers shall be one of the following multisets (delimited by commas, when there is more than one multiset per item); the type specifiers may occur in any order, possibly intermixed with the other declaration specifiers.
— void
— char
— signed char
— unsigned char
— short, signed short, short int, or signed short int
— unsigned short, or unsigned short int
— int, signed, or signed int
— unsigned, or unsigned int
— long, signed long, long int, or signed long int
— unsigned long, or unsigned long int
— long long, signed long long, long long int, or signed long long int
— unsigned long long, or unsigned long long int
— float
— double
— long double
— _Bool
— float _Complex
— double _Complex
— long double _Complex
— atomic type specifier
— struct or union specifier
— enum specifier
— typedef name

As we can see, long is a type specifier. It is not a type qualifier.

From the C11 draft, section 6.2.5 ("Types)" paragraph 4:

There are five standard signed integer types, designated as signed char , short int , int , long int , and long long int .

How these types are specified in program text is another issue, there are many ways since the syntax is rather lax. For instance, according to 6.7.2 ("Type Specifiers") the following are all valid ways to specify the same type :

long , signed long , long int , or signed long int

This says that long by itself is a valid type specifier for the type long int . This was the same in C99 (and, I would guess , earlier standards too). So no, it's not a qualifier.

In addition, the above can be intermixed with things like static , volatile , pointer asterisks, and so on.

I would suggest reading some other book, since it's confusing to read books that use different terminology from the standard. The standard is often refered to when answering questions about C, so it's a good idea to be familiar with it.

In the C programming language, data types are declarations for memory locations or variables that determine the characteristics of the data that may be stored and the methods (operations) of processing that are permitted involving them.

The C language provides basic arithmetic types, such as integer and real number types, and syntax to build array and compound types. Several headers in the C standard library contain definitions of support types, that have additional properties, such as providing storage with an exact size, independent of the implementation. https://wikipedia.org/wiki/C_data_types

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM