11

I have read a book called "Pointers On C". In that book, there is a type called scalar types.

I know that arithmetic types and pointer types are collectively called scalar types, but I want to know what is the difference between scalar type and aggregate type and what occasion use them?

shengfu zou
  • 560
  • 1
  • 7
  • 16

4 Answers4

15

C11-§6.2.5 Types (p21):

Arithmetic types and pointer types are collectively called scalar types. Array and structure types are collectively called aggregate types.46)

Scalar data types can hold only single data item while aggregate types can hold more than one data items.

int a;             //Scalar Type
char c;            //Scalar Type
float *p;          //Scalar Type
char str[10];      //Aggregate Type
struct s{
    int a;
    float b[5];
} ss;              //Aggregate Type

46) Note that aggregate type does not include union type because an object with union type can only contain one member at a time.

Community
  • 1
  • 1
haccks
  • 104,019
  • 25
  • 176
  • 264
  • Why it has two types, scalar and aggregate? And what is the different between them? – shengfu zou Mar 01 '16 at 12:19
  • 1
    *Why it has two types*: Because C standard committee or may be language writer decided that. *And what is the different between them?*: It is already mention in the answer. Let me explain it further. – haccks Mar 01 '16 at 12:30
  • Is a `union` a scalar type though? Or is it a different beast from either scalars or aggregates? It's neither an arithmetic type nor a pointer type, and a union can contain an aggregate. – Joe Z Dec 17 '22 at 21:54
  • @JoeZ In fact union doesn't fall under either scalar or aggregate type but it is in itself a new type! Though this is not mentioned in the standard anywhere explicitly, you can say that apart from scalar and aggregate there is a new type called `union`. – haccks Dec 18 '22 at 10:06
4

As the word aggregation itself implies aggregation types are composed from other types.

In C aggregation types are array and structure types.

Take into account that an aggregation type can include another aggregation type.

Here is an example of aggregation types

#include <stdio.h>

int main( void ) 
{
    struct TLine
    {
        struct TPoint
        {
            int x;
            int y;
        } first, second;
    } lines[10] = { [0] = { { 0, 0 }, { 10, 10 } } };

    printf( "lines[%d].first = { %d, %d }, lines[%d].second = { %d, %d }\n", 
            0, lines[0].first.x, lines[0].first.y, 
            0, lines[0].second.x, lines[0].second.y );
}    

The program output is

lines[0].first = { 0, 0 }, lines[0].second = { 10, 10 }

The structure TLine is an aggregation type that contains another aggregation type definition struct TPoint. And lines is an object of one more aggrefation type - array of TLines.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
3

Taken from here:

C Scalar

“C Scalar Data Types” lists C scalar data types, providing their size and format. The alignment of a scalar data type is equal to its size. “Scalar Alignment” shows scalar alignments that apply to individual scalars and to scalars that are elements of an array or members of a structure or union. Wide characters are supported (character constants prefixed with an L). The size of each wide character is 4 bytes.

C Aggregate Data Types

An aggregate data type consists of one or more scalar data type. You can declare the following aggregate data types:

  • Array

  • Union

  • Struct

This was just a brief definition, but the link I've given has enormous content.

To make it more clear, here's a flowchart:

enter image description here

Here's an even more detailed flowchart:

enter image description here

Community
  • 1
  • 1
Box Box Box Box
  • 5,094
  • 10
  • 49
  • 67
0

The internet can be your friend! But, a scalar type is a type for which there exists an arithmetic for its values (integers, floats, pointers), and a aggregate type is just a type for which values are tuples of values, i.e. an aggregation of values, a structure, and for which there is no natural arithmetic (ex.: a person described by its name and age, you can't add or multiply two persons...).

Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69