Showing posts with label basic. Show all posts
Showing posts with label basic. Show all posts

Friday, February 21, 2025

Understanding Primitive Data Types in the C Programming Language

C is a statically typed programming language, which means that every variable must have a clearly defined type. Data types in C determine the amount of memory that a variable occupies, as well as the operations that can be performed on it. Simply put, in the C programming language, data types refer to things that are separate entities in other programming languages. That's why we'll learn them all at once. Just keep in mind that many things in the C programming language are limited and don't even have what the C++ programming language has, let alone what we can tell you about other more modern programming languages. There are four main categories of data types in C:

  • Basic or primitive data types
  • Certain type modifiers
  • Complex data types
  • User-defined types
Primitive types
in C are simple, close to the hardware, and flexible, but less standardized compared to modern languages. C++ extends them with better tools e.g. bool, <cstdint>, while languages like Java and Python introduce a higher level of abstraction and standardization. If you're programming in C, it's important to understand the platform you're working on because it affects the behavior of these types.

Data types in the C programming language work differently compared to more modern programming languages

Data types in the C programming language work differently compared to more modern programming languages

Let's take a look at the first category of data types. Here's an overview of the basic or primitive data types in C:

Integers - Integer Types:

int - Used for whole numbers (e.g. 5, -10, 42). The size depends on the system architecture (usually 4 bytes on 32-bit or 64-bit systems).

short - A shorter integer (usually 2 bytes).

long - A longer integer (4 or 8 bytes, depending on the system).

long long - An even longer integer (at least 8 bytes, introduced in C99).

Modifiers:

signed - Allows positive and negative values (the default for int).

unsigned - Allows only non-negative values, which doubles the positive range (e.g. unsigned int).

Floating-Point Numbers - Floating-Point Types:

float - Single precision (usually 4 bytes), for decimal numbers (e.g. 3.14).

double - Double precision (usually 8 bytes), for greater accuracy of decimal numbers.

long double - Extended precision (size depends on the system, often 10, 12 or 16 bytes).

Characters - Character Type:

char - A single character (e.g. 'A', 'b') or a small integer (1 byte).

signed char - From -128 to 127.

unsigned char - From 0 to 255.

Logical Type - Boolean:

In standard C before C99, there was no special type, but since C99, _Bool is introduced, 0 for false, 1 for true. With the <stdbool.h> library, bool can be used as an alias for _Bool, along with true and false.

Empty Type - Void:

void - Indicates the absence of a type. It is used in functions that do not return a value or in pointers to an undefined data type void*.

Why Are Primitive Types in C Close to Hardware and How Does This Differ from Other Languages?

Monday, March 26, 2018

Enumeracije ili nabrajanje tipa Enum u C# programskom jeziku


Enumerations - enumeracije su specijalni vrednosni tipovi podataka koji omogućava da zadate grupu imenovanih numeričkih konstanti. U Srbiji se enumeracija često naziva nabrajanje, ali onda je bolje da kažete nabrajanje tipa enum jer postoje i druge vrste nabrajanja u C# programskom jeziku. Programeri C++ programskog jezika ponekad nazivaju enumeraciju skraćeno enums. Zašto enums, zašto ne enum? Zato što je enum skraćenica od ENUM - Electronic number – elektronski broj; što je opet predlog za mapiranje svih telefonskih brojeva u IP adrese.


( Enumeracija čini vaš kod čitljivijim )

Zato je neophodno obratiti pažnju kada pričate sa kolegama o enumeracijama. Inače kada hoćete da deklarišete enumeraciju u C# programskom jeziku; koristite ključnu reč enum izvan funkcija kao što to radite sa strukturama.

enum ime_enumeracije
{
    vrednost1,
    vrednost2,
    …
}

Jednom kad ste deklarisali enumeraciju, možete da je koristite na tačno isti način kao i bilo koji drugi numerički tip podataka. Vrednostima enumeracije možete dodeljivati jedino celobrojnu vrednost. Ukoliko želite vrednostima enumeracije da dodeljujete vrednosti tipa stringova, onda bolje razmislite o tome da koristite strukturu u kojoj će nabrajanje biti jedan od članova strukture. Nabrajanje pružaju funkcionalnost garantovanih fiksnih vrednosti i kad se koriste kao indikatori.

Kako se dodeljuju vrednosti strukturi enumeracija?