Showing posts with label type. Show all posts
Showing posts with label type. 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

Wednesday, March 05, 2025

Practical Examples of Using Operators in the C Programming Language

In the C programming language, operators are symbols that enable the execution of specific operations on data, such as mathematical calculations, logical comparisons, or bit manipulations. Operators are symbols that instruct the compiler what to do with operands in an expression. Understanding operators is crucial for writing efficient and effective code. The C programming language is known for its simplicity and direct hardware control, which is reflected in its set of operators. C programming language operators reflect its low-level and efficiency philosophy. Unlike higher-level languages such as Python or JavaScript, the C programming language does not have operators for advanced functionalities like exponentiation ** in the Python programming language because it focuses on simplicity and control. Bitwise and pointer operators make the C programming language powerful for system programs, while the lack of logical types or automatic memory management requires greater programmer attention compared to modern languages. Essentially, C programming language operators are the foundation for many other languages, but their directness and minimalism remain unique.

Operators in the C programming language require deeper knowledge

Operators in the C programming language require deeper knowledge

The C programming language supports a wide range of operators used for various data operations. Each operator has its own specific behavior and rules of application, which allows programmers to manipulate data in various ways and execute complex operations. Correct use of operators can significantly improve code performance and readability. Operators in C programming language can be categorized into several types:
  • Arithmetic Operators
  • Assignment Operator
  • Compound Assignment Operators
  • Relational (Comparison) Operators
  • Logical Operators
  • Bitwise Operators
  • Unary Operators
  • Ternary Operators
  • Pointer Operators
  • Size and Type Operators
  • Etc.

Basic Arithmetic Operators in the C Programming Language: Everything You Need to Know

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

Tuesday, March 22, 2016

Manifest, GAC i Refleksija


Pre nego što pređemo na Manifest, podsetio bi vas da dobro prostudirate dva prethodna posta Arhitektura Microsoft .NET Framework-a i Šta su sklopovi? Koje sve komponente sadrži .Net Framework i koji je proces kompajliranja i izvršavanja aplikacija programiranih u .Net programskim jezicima je toliko bitno znati, da ćemo prvo u najkraćim crtama ponoviti materiju koja se mora znati. Znači, Microsoft .Net Framework je platforma koja omogućava razvoj i izvršavanje .Net aplikacija. To je platforma koje obezbeđuje komponentnu infrastrukturu, integraciju programskih jezika, internet interoperabilnost, jednostavan razvoj, jednostavnu instalaciju, pouzdanost i bezbednost. CLR – Common Language Runtime je najvažniji deo Microsoft .Net Framework-a nadležan za aktiviranje objekata, izvršavanje bezbednosnih provera nad njima, njihovo smeštanje u memoriju, izvršavanje i uklanjanje iz memorije. CLR podržava sve programske jezike koje se mogu prevesti u MSIL – Microsoft Intermediate Language. 
 

( Sklop u jednom i sklop u više fajlova )

Sledeće komponente Microsoft .Net Framework-a su BCL – Base Class Library, biblioteka baznih klasa koja se sastoji se od klasa, interfejsa i tipova podataka koje omogućavaju korišćenje funkcija sistema i FCL – Framework Class Library koja sadrži specijalizovane biblioteke tipova podataka koje sadrže Data i XML klase. Microsoft .Net Framework sadrži i klase koje omogućavaju tehnologije za razvoj aplikacija poput konzolni aplikacija, formi, WPF aplikacija, univerzalni aplikacija, mobilni aplikacija, web servisa, web aplikacija itd. Proces prevođenja izvornog koda u izvršni u najkraćim crtama se može podeliti u dva koraka. Prvo se izvorni kod prevodi u MSIL – Microsoft Intermediate Language, zatim u mašinski kod. MSIL je jezik nižeg nivoa koji se lako pretvara u mašinski kod. On omogućuje platformsku nezavisnost, poboljšanje performansi i jezičku interoperabilnost. Jednostavno klasa koja je napisana u jednom programskom jeziku može da komunicira sa klasom pisanom u drugom programskom jeziku. CTS – Common Type System – zajednički sistem tipova definiše skup pravila koje .Net kompajleri moraju da poštuju. CTS tipovi imaju istu semantiku bez obzira u kom su programskom jeziku definisani. I naravno ne treba izostaviti ni GC – Garbidge Collection – skupljač smeća, komponentu za upravljanje memorijom. GC jedino radi sa referentnim tipovima podataka, što znači da objekte na hipu do kojih ne vodi ni jedna referenca se automatski oslobađa. GC sprečava curenje memorije, tačno vreme oslobađanja memorije nije određeno i oslobađanje resursa ne treba da se radi u destruktoru klase.

Gde se sad u sve navedeno uklapa sklop? 
 

Friday, January 08, 2016

Konverzija tipova podataka u C# programskom jeziku


Bez obzira da li radite na malim programima ili velikim projektima često će te dolaziti u situaciju da konvertujete vrednost iz jednog tipa u drugi. Takođe, vi ne možete deklarisati istu promenjivu dvaput. Ako je promenjiva deklarisana kao integer vi ne možete njoj dodeliti vrednost tipa string. Odmah bi ste dobili grešku „Cannot implicitly convert type 'string' to 'int'.” U konzolnim aplikacijama vi unosite podatke putem tastature u obliku znakova i oni se prilikom ulaza tretiraju kao vrednost tipa string. Ukoliko podatak u vašem programu treba da bude broj tipa integer ili float neophodno je da konvertujete vrednost tipa string u integer ili string u float, kasnije možda i integer u float kako bi ste mogli da vršite aritmetičke operacije sa vašim brojevima; tada vršite konverziju vrednosti iz jednog tipa podataka u drugi.


( Slikovit prikaz konverzije tipova podataka )

Konverzija tipova podataka se uglavnom deli na implicitnu, eksplicitnu konverziju ili programerski definisanu konverziju. Implicitna konverzija je ona konverzija koja se vrši automatski, eksplicitna konverzija je ona konverzija koja se vrši kastingom i programerski definisana konverzija je konverzija koju je kodirao programer. Međutim, uopšteno konverzija tipova podataka se takođe vrši na više načina:

Korišćenjem kastinga
Korišćenjem as operatora
Korišćenjem Parsing-a
Korišćenjem klase System.Convert
Korišćenjem klase System.Bit
Boxing i Unboxing konverzija


Šta je widening i narrowing konverzija?