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

Thursday, November 28, 2024

Introduction to Working with Text Files in PHP, Reading and Writing

Working with text files in PHP is a fundamental yet crucial skill for any PHP developer. Files allow for the storage, reading, and management of data outside of an application's memory, which is essential for many applications, such as logging, storing configurations, or managing user data. This blog post provides a detailed introduction to the basics of working with text files in PHP, including reading, writing, and some advanced operations. Although databases are the predominant storage medium for data today, text files still hold their place in many applications. To proficiently work with text files in PHP, a solid grasp of fundamental concepts is essential. These include understanding file paths, which can be either relative or absolute, and file modes, which dictate whether you're reading, writing, or performing both actions on a file.

  • r - Read only. - Opens a file for reading only. Any attempt to write to the file will fail.
  • w - Write only. - Opens a file for writing only. If the file exists, it will be truncated (empty). If it doesn't exist, a new file will be created.
  • a - Append. - Opens a file for writing. Any data written will be placed at the end of the file. This is useful for adding new data to an existing file without overwriting the existing content.
  • r+ - Read and write. - Opens a file for both reading and writing. The file pointer will be positioned at the beginning of the file.

Programmers are pleasantly surprised by PHP's functions for reading and writing to text files

Programmers are pleasantly surprised by PHP's functions for reading and writing to text files

PHP offers several methods for reading content from text files. Some of the most commonly used methods include using the file_get_contents function which allows reading the entire file content in one go, using the fopen and fread functions which provide more control, especially when working with large files, and using the file function which loads the file as an array where each element is a line. Writing data to text files is a fundamental aspect of PHP programming. The file_put_contents function provides a straightforward way to write to a file, overwriting any existing content. For more granular control over the writing process, the fopen and fwrite functions offer greater flexibility. When appending data to an existing file, the FILE_APPEND mode should be used with file_put_contents.

It's essential to consider security when working with files, implementing measures such as file locking and input validation to prevent potential vulnerabilities. By mastering these concepts, you can effectively leverage file operations to manage data in your PHP applications. Working with text files in PHP is simple and efficient thanks to the built-in functions that the language provides. Whether you need to read data from a file, write logs, or even work with CSV format, PHP offers robust tools for these tasks. We hope our practical examples and tips will help you better understand and utilize the capabilities that PHP provides for working with text files. Let's move on to the practical part.

Efficient Management for Adding, Deleting, and Saving Items in an HTML List

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

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?

Friday, November 06, 2015

Rad sa tekstualnim i binarnim datotekama u C++ programskom jeziku


Najvažnija stvar svake aplikacije je da negde čuva informacije i da koristi iste po potrebi. Inače rezultati vaše aplikacije bi trajali samo dok radi program; čim bi ste ga pokrenuli ponovo, ne bi ste mogli videti informacije koje ste pre unosili. Čak i najbanalnija igrica mora negde da pamti rezultate igrača, međutim RAM memorija nije postojana i ona traje dok se računar ne ugasi. Zato se podaci čuvaju u nekom fajlu ili u bazi podataka. Kad C++ programeri pričaju o tokovima ili drugačije rečeno; streams – strimovima; to znači da pričaju o nečemu gde mogu da se učitaju ili upisuju podaci. Strimovi u suštini obezbeđuju jednostavan rad i sa unosom i sa čitanjem podataka u tekstualni ili binarni fajl. Prvo je potrebno da omogućite podršku operacijama fajl tokova direktivom #include <fstream> koja vam omogućava korišćenje objekata ifstrem i ofstream koji regulišu otvaranje i zatvaranje datoteka.

 

( Input and Output Stream )
 

Zahvaljujući <fstream> koji inače uključuje iostream objekte koji održavaju flagove i obaveštavaju o stanju ulaza i izlaza, vama je omogućeno da koristite logičke funkcije poput eof(), bad(), fail() ili good() i sa takvim funkcijama rad sa bilo kojim tekstualnim fajlom je jednostavan.

Kako da upisujem i učitavam podatke u tekstualnu datoteku?