Showing posts with label types. Show all posts
Showing posts with label types. Show all posts

Thursday, March 13, 2025

How to Improve Code Readability, User-Defined Types in the C Programming Language

In the C programming language, in addition to basic data types such as int, float, char, etc., you can define your own data types. These user-defined types allow programmers to create more complex data structures that meet the specific needs of their applications. User-defined types in the C programming language: struct, union, enum and typedef, provide programmers with tools to create customized and meaningful data types. Structures are ideal for grouping data, unions for saving memory, enumerations for defining constants, and typedef for simplifying syntax.

Their proper use makes the code more modular, easier to understand and adapt, which is especially important in larger projects. These mechanisms are the foundation for working with complex data in the C programming language and are often used in system programming, database work, and algorithm implementation, while at the same time these user-defined types make the code more readable, organized, and easier to maintain. In addition to user-defined data types, at the end of the lesson, we will also focus on constants in the C programming language.

You need to have a solid understanding of the C programming language to effectively use user-defined types

You need to have a solid understanding of the C programming language to effectively use user-defined types

First of all, we will start with structures because structures are the most commonly used user-defined data types in the C programming language. Structures are user-defined data types that allow grouping different types of data under a single name. They simply enable the combination of different data types into one entity. Each variable within a structure is called a member of the structure. Structures are defined using the struct keyword. A structure is useful when we want to model an entity that has multiple attributes, such as a person, a car, or a point in space.

As we know, the C programming language was developed by Dennis Ritchie at Bell Labs during the early 1970s, and it is based on the earlier programming language B, which was created by Ken Thompson and Ritchie. The B programming language, at that time, around 1970, was simple and did not have structures as a formal concept. Instead, programmers manually managed memory and grouped data using pointers and manual memory offsets. However, Ritchie recognized the need for a higher level of abstraction to make programming more efficient. This means that structures did not exist in the earliest predecessors of the C programming language, but they were introduced very early in its development, practically with the creation of the C programming language as we know it today.

Designing Complex Data Structures in C: Best Practices

Tuesday, February 25, 2025

How to Use Derived Data Types in C Programming Language for Efficient Programming

In the C programming language, in addition to basic data types such as int, float, char, and double, there are derived data types that are based on basic types, but offer additional capabilities and flexibility when working with data. Derived data types are types that extend the functionality of primitive types by combining or referencing existing data. They allow the programmer to efficiently manage memory, group data, or define operations. Unlike primitive types, which directly store values, derived types often involve indirection, such as pointers, or the organization of multiple elements, such as arrays.

The main derived data types in the C language are:

Pointers: A data type that stores a memory address, e.g., int*, char* 
 
Arrays: A collection of elements of the same type, e.g., int arr [10]
 
Functions: The return value type of a function, e.g., int function ())

As you can see, in the C programming language, the main derived data types are pointers, arrays, and functions, and user-defined types such as struct, union, and enum are often implicitly included in this category, although they are formally different. We will write about user-defined types in the next blog post. Now we will concentrate only on derived data types that are not user-defined. If you are wondering how functions are a derived data type, then you simply need to know that functions in the C programming language have a type defined by their return value. They are not variables in the classical sense, but they are considered derived types because they allow defining operations on data. We hope this has cleared up any confusion. If anything is unclear, ask in the comments or contact us personally via the contact form on our blog.

A software engineer is considering using derived data types in the C programming language.

A software engineer is considering using derived data types in the C programming language

Pointers are one of the most powerful and characteristic aspects of the C language. They store the address in memory where the data is located, instead of the value itself. This allows for indirect access to data and dynamic memory allocation. Pointers in the C programming language are one of the key elements that make C so flexible and close to hardware. They are both fascinating and challenging, as they allow direct manipulation of memory, but their proper use can significantly improve program performance, while careless handling can lead to problems such as memory leaks and default errors. With pointers, you must code carefully and think carefully about when and how to use them.

Pointer Declaration:

int *ptr;

This means that ptr is a pointer to an integer value, int type specifies the type of data the pointer points to e.g., int, float, char, while * indicates that this is a pointer.

Key Operators:

& Address-of Operator - Retrieves the memory address of a variable.

* Dereference Operator - Accesses or modifies the value at the address stored in the pointer.

          Initialization Example:

int number = 10;

int *ptr = &number; // ptr points to the address of the number variable

Pointer Dereferencing:

    printf("Value at address: %d\n", *ptr); // Prints 10 

Dereferencing means accessing the value at the address that the pointer points to. Arrays in the C programming language are actually pointers to the first element. You can use them to traverse the array. Pointers can be used to pass arguments to functions to allow modifications of the original values. Function pointers allow functions to be called dynamically, based on their address. A pointer can also point to another pointer, allowing multiple levels of indirection. In any case, you will master pointers most easily through practice and coding.

Understanding and Applying Pointers in a Practical Example in C Programming Language

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?

Friday, November 22, 2024

Top 8 Types of Functions in PHP You Must Know

PHP is a powerful server-side scripting language offering a wide range of functions to facilitate various tasks. Functions in PHP are blocks of code that execute only when called. They can accept data, parameters as input, process it, and return an output using 'return' or simply execute code within the function without returning a value. Such functions are called void functions. If you use a function that PHP has already written for you, such as 'is_numeric' and many others, these are called built-in functions. If a function is located within a class, it is called a method. Regardless of whether you are working with built-in functions, writing your own, or using advanced concepts like closures, understanding functions is crucial for efficient PHP programming. Functions in PHP are a powerful tool that enables modularity, code reuse, and flexibility. You should use them whenever it makes sense in your code, while also professionally creating your own.

Many years ago, if you asked us how many types of functions there are, we would have thought about the basic division and answered four:

a function that takes no parameters and returns a value

a function that takes parameters and does not return a value

a function that takes no parameters but returns a value

a function that takes no parameters and do not return a value

Today, PHP has proven us wrong. There are many more types of functions in the PHP programming language, and today we will list the top 8 that you will often use in your projects.

The student is thrilled with exploring the capabilities of functions in PHP

The student is thrilled with exploring the capabilities of functions in PHP

To make things much clearer for you regarding the numerous functions in PHP, let's dive straight into the practical part when it comes to functions. This is the easiest way to go through and explain even 8 types of functions in the PHP programming language. Unlike previous lessons in this PHP tutorial, today we'll expand our PHP coding by using a CSS file in addition to Bootstrap. You might wonder why use CSS when you're using Bootstrap. The reason is simple. There are things in HTML and PHP coding that you can't always style using Bootstrap alone. So, for larger projects, you'll often find that CSS styling is used in addition to Bootstrap. Nowadays, there are systems that don't allow the use of Bootstrap for security reasons, they only use CSS, while we've never seen a project that prohibited the use of CSS. That's why you should always learn and know how to use HTML and CSS together with PHP. Our HTML & CSS tutorial will make it easier for you to learn or simply refresh your memory, click here.

Then we'll learn how to pass data from one PHP page with an HTML form to another PHP page for further processing. Also, since there are multiple functions, we'll place them in a separate PHP file and teach you how to connect PHP files, so that you have the impression that separate functions in another PHP file are available as if they were in the same one. This PHP lesson might sound a bit complicated, but let us reassure you by coding everything together, writing simple code that you can definitely use in your projects.

Choose the Right Function for the Job: 8 Function Types for Better Code

Sunday, September 29, 2024

Understanding PHP Data Types, A Practical Guide

Even though we have already installed and prepared everything we need to learn and program a programming language, as we prepared to learn the PHP programming language in the previous PHP tutorial post, see here; and since we have printed the famous "Hello World" on the local server web page; the first steps in programming begin with variables. Variables in programming are places in computer memory where data is stored that the program uses for calculation, processing, and manipulation. In the PHP programming language, as in other programming languages; variables are names used to reference places in memory. The easiest way to understand variables is to think of them as memory boxes. But not all boxes are the same. In some, you can put a value that represents an integer, while in others you can put floating-point numbers. There are also boxes in which you can put a set of any characters. To know what value, you can put in a variable, you need to know what data type you have decided to assign to a variable. All programming languages have variables and certain data types.

Most basic variable types are essentially the same or similar. However, some programming languages have more data types while others have fewer. Also, even the same name of a variable type in many programming languages can have different limitations. For example, in the PHP programming language, the float data type has the precision of the double data type, which means that the value of floating-point numbers is much larger than it would be if the data type was float in the C# programming language. Or instead of the char data type in C#, the string type is used in the PHP programming language, while char is a function used to convert an ASCII number to a character. So that you are not confused at the very beginning of learning data types, it is best to concentrate on the data types that the PHP programming language has.

The boss explains the importance of data types in PHP to a new employee

The boss explains the importance of data types in PHP to a new employee

Variables are the fundamental building blocks of every program. They serve as containers for data that can be changed and manipulated throughout the program's execution. In PHP, variables are declared simply by assigning a value to a name. The data type of a variable is determined automatically based on the assigned value. Variable names can include letters, numbers, and underscores, but they must start with a letter or an underscore. Additionally, PHP requires a dollar sign ($) before each variable name to identify it. For example, in this line, we've created a variable named $number and assigned the integer value 10 to it.

$number = 10;

It's essential to choose clear and descriptive variable names to enhance code readability. For instance, if a variable holds the value of the 13th salary, a suitable name would be $thirteenthSalary. Remember that PHP is case-sensitive, meaning $number and $Number are considered different variables. A common convention in PHP is to use camelCase for variable names, as seen in $thirteenthSalary.
Unlike many other programming languages, PHP doesn't require explicit type declarations. When you assign a value to a variable, its data type is inferred automatically, and the necessary memory is allocated.

Basic Data Types in PHP: Simply Explained

Wednesday, April 03, 2024

Mastery of Operators in C# 12 Programming Language, Everything You Need for Efficient Coding

Operators in the C# programming language are crucial elements for shaping, manipulating, and processing data. Understanding their proper usage not only facilitates software development but also enables efficient and readable code. In this lesson, we will explore several key operators in C# 12 and how to masterfully apply them to achieve optimal performance and code clarity. Mastery in using operators in C# 12 involves understanding their characteristics, execution priorities, and proper 
application in various contexts.

Efficient coding requires clear, readable, and optimized code that utilizes operators appropriately to achieve desired results. This lesson covers key aspects of operator usage in C# 12 and provides guidelines for achieving efficient and readable code, even if some examples go beyond beginner level. Rather than confusing or demoralizing, it should serve as guidance for your future endeavors. C# is a powerful programming language that provides various operators for data manipulation. In C# 12, some new features have been added to facilitate efficient coding.

A girl is learning programming

A girl is learning programming in C# 12 programming language

For example, “Primary Constructors”, introduced in C# 12 allow creating primary constructors in any class or structure. The parameters of the primary constructor are available throughout the class body. Adding a primary constructor prevents the implicit generation of a parameterless constructor. “Collection Expressions”, with the introduction of a new syntax, allow creating common collections. This includes array initialization and collection initialization. These operators and functionalities enable programmers to write efficient and readable C# code.

However, we will learn about these concepts when we cover classes, constructors, and collections. Meanwhile, we’ll primarily introduce more important operators through practical examples to ensure better understanding. In this lesson, you’ll be typing a lot and getting accustomed to writing code extensively. In C# programming language, operators have their own precedence when they are executed. For example, multiplication will always be performed before addition. Therefore, it is necessary and safer to always use parentheses before you find yourself in a situation where operator precedence leads to an incorrect result.

int number = 4 + 5 * 6; // Result is 34

 

// You might have expected the result to be 54

// Always use parentheses to clarify expressions

 

int number = (4 + 5) * 6; // Result is 54

The computer will always perform calculations according to operator precedence unless you change it with parentheses.

Mastering Arithmetic Operators in C# 12: Efficient Calculations Made Easy

Monday, April 01, 2024

Master the Fundamentals of C# 12 Programming, How to use Variables, Data Types, and Constants in Your Projects?

In the previous lesson, we created our first C# 12 program. We learned how to print text to the Terminal panel and explored the three types of comments used in the C# programming language. Additionally, we discovered how to set attributes in the *.csproj file, which affects the entire project. We also delved into creating regions, which allow us to organize code into blocks and navigate through it easily, regardless of the number of lines of code.

Furthermore, despite the convenience of C# 12’s top-level statements, we can still create programs with the traditional Main method that you might recognize from older versions of the C# programming language. However, everything we’ve learned so far would only suffice for printing text on consoles. Programming encompasses much more than that! Let’s first focus on what programming is and what it involves.

A boy is learning C# 12 programming at home

A boy is learning C# 12 programming at home to make a game

Programming is the process of creating computer programs, where programmers use a specific language and tools to communicate with the computer. Programming is the art of writing code that enables computers to perform specific tasks and Programmers use various languages (such as C#, Python, Java, etc.) to write instructions for the computer. In any case, programming requires creativity and problem-solving skills. It goes beyond mere code writing. It involves creating solutions, thinking about problems, and communicating with computers to achieve desired functionality.

Programmers often need to devise innovative ways to tackle challenges. But how do computers actually function? Programming involves giving instructions to the computer in the form of one or more grouped lines of code, known as statements. These statements are grouped into methods, and methods into classes, to avoid code repetition in a project and enable calling previously written and tested code from other parts of the project. 

The code you write must have a purpose and be written with a significant reason. It should also be correct and optimized. Your computer will always execute exactly what you've instructed, regardless of whether it's logical. Logic and proper code writing are up to you. Learning programming usually begins with console applications. Although console applications aren’t used for creating market-ready 
programs due to their purely textual user interface, they are excellent for testing methods and classes.

Understanding Declarations, Variables, Data Types, and the Role of Constants

Sunday, May 28, 2023

Preuzmi kontrolu nad svojim C++ kodom, nauči promenjive, tipove podataka i konstante

Promenjiva – variable je lokacija u memoriji računara na kojoj čuvate određenu vrednost i iz koje možete da menjate ili preuzmete vrednost. Jednostavno memoriju računara zamislite kao niz memorijski lokacija. Memorijske lokacije su numerisane i njih nazivamo memorijske adrese. Jedna promenjiva rezerviše jednu ili više memorijski lokacija u kojoj će se čuvati neka vrednost. Sve vaše promenjive se kreiraju u RAM - Random Access Memorymemoriji. Kad vi promenjivoj zadate ime promenjive, vi ne morate znati stvarnu memorijsku adresu promenjive. Vi promenjivoj pristupate preko njenog imena i njena memorijska adresa ostaje ista tokom trajanja promenjive. Ali ukoliko bi ste pokrenuli vaš program i ugasili ga, zatim ga ponovo pokrenuli; vaša promenjiva bi imala drugu memorijsku adresu. Promenjive koristimo da uzmemo podatke od korisnika ili sami možemo definisati njihove vrednosti u kodu. Da bi ste napravili i koristili neku promenjivu vi je prvo morate deklarisati. Iako računar tretira sve promenjive i čuva ih kao brojeve, programeri promenjive dele prema potrebama u 3 osnovna formata.



 ( C++ Datatypes )

Promenjive delimo pre svega na formate, celi broj – Integerbroj sa pokretnim zarezom – Floating Point i tekstualni string – Text StringPored osnovnih formata promenjivih; postoje i mnoštvo tipova promenjivih koji su u principu izvedeni od već navedenih formata. Svaki format promenjivih ima više tipova promenjivi. Vi čak možete definisati i praviti svoje vlastite tipove promenjivih, iako za tim nećete imati potrebe. Najvažnije je da shvatite da svaka vrednost ima prvo svoj format, zatim i svoj tip promenjive. Na primer, decimalne brojeve ćete stavljati u promenjivu formata Floating Point, u tip podataka Float ili Double dok ćete tekst stavljati u format promenjivih String. Međutim vaš računar će prevesti svaku vašu promenjivu i čuvati kao binarni broj bez obzira koji ćete format i tip podataka koristiti. Osnovni tipovi u C++ programskom jeziku su:

char                                                     1 bajt            256 znakova

unsigned short int                              2 bajta          od 0 do 65 535

short int                                              2 bajta          od – 32 768 do 32 767

unsigned int (16 bitova)                    2 bajta          od 0 do 65 535

unsigned int (32 bitova)                    4 bajta          od 0 do 4 294 967 295

int (16 bitova)                                     2 bajta         od – 32 768 do 32 767

int (32 bita)                                         4 bajta         od – 2 147 483 648 do 2 147 647

unsigned long int                               4 bajta          od 0 do 4 294 967 295

long int                                               4 bajta          od – 2 147 483 648 do 2 147 483 647

float                                                    4 bajta          od 1,2e-38 do 3,4e38

double                                                8 bajtova      od  2,2e-308 do 1,8e308

Imajte u vidu da ove vrednosti na vašem računaru mogu da variraju u zavisnosti od vašeg računara i kompajlera. Da bi ste napravili i koristili promenjivu prvo je potrebno da je deklarišete.

Kako da deklarišem neku promenjivu?