Showing posts with label library. Show all posts
Showing posts with label library. Show all posts

Wednesday, March 19, 2025

Working with Files in the C Programming Language, Practical Examples and Tips for Efficient Programming

Working with files in the C programming language represents a key functionality that enables programmers to write, read, and manipulate data that is permanently stored on disk. This capability is essential for developing applications that require data storage between program executions, such as databases, configuration files, or logs. In the C programming language, file operations are achieved using the standard <stdio.h> library, which provides functions for opening, reading, writing, and closing files. In the C programming language, files are treated as data streams. There are two basic types of files:

  • Text files – data is stored as a sequence of characters, often human-readable, such as .txt files.
  • Binary files – data is stored in binary format, which is more efficient for machine processing, such as images, executable files, etc.

The FILE data type, defined in <stdio.h>, is used for working with files. This type is actually a pointer to a structure that contains information about the file, such as its location, current pointer position, and operating mode.

Working with files is straightforward in C programming

Working with files is straightforward in C programming

The C standard library provides several functions for working with files, including:

fopen() - Opens a file. 

fclose() - Closes a file.

fprintf() - Writes formatted text to a file.

fscanf() - Reads formatted text from a file.

fputc() - Writes a single character to a file.

fgetc() - Reads a single character from a file.

fwrite() - Writes a block of bytes to a file.

fread() - Reads a block of bytes from a file.

Working with files in the C programming language provides a powerful tool for data management, but requires attention to resource handling and error checking. Through the standard library, programmers can efficiently implement functionalities for both text and binary files, with flexibility for various types of applications. With basic operations such as opening, reading, writing, and closing, the C programming language also enables advanced techniques such as positioning and binary data processing, making it suitable for a wide range of tasks. Understanding basic operations such as opening, reading, writing, and closing files is crucial for developing applications that handle large amounts of data. First, we will focus on and examine C code for working with text files.

Reading and Writing Text Files in C Programming

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

Wednesday, September 11, 2024

NumPy For Machine Learning, An Introduction to the Python Library for Manipulating Arrays and Matrices

NumPy is a Python library providing support for efficient operations on multi-dimensional arrays and matrices. It’s a cornerstone tool for scientific computing in Python and is widely used in fields like data analysis, machine learning, signal processing, visualization, and many others. Its popularity has surged alongside the rapid advancements in AI. Originally, the library was an extension to Python, first worked on by software engineer Jim Hugunin, who left Microsoft to join Google. However, the NumPy we know today is largely the work of Travis Oliphant, often considered the primary creator of NumPy, founder of Anaconda, and the SciPy package in Python.

This open-source library became significant primarily because it addressed the slowness of Python interpretation. NumPy solves this by providing multi-dimensional arrays, functions, and operators that work efficiently with these arrays. When using NumPy, you write code with fewer inner loops. Thus, any algorithm expressible as operations on arrays and matrices can run nearly as fast as equivalent C code.

NumPy's usage and functionality are often compared to the MATLAB environment, as both interpret code and allow users to quickly write computations, with most operations performed on arrays and matrices rather than scalar values. Compared to MATLAB, which originated in 1970, NumPy is integrated into Python, a modern, complete, and natively compiled programming language. However, both languages rely on BLAS and LAPACK for efficient linear algebraic computations.

The moment a programmer discovers the true power of the NumPy library

The moment a programmer discovers the true power of the NumPy library

A central element in NumPy is the ndarray - an n-dimensional array, representing a multi-dimensional homogeneous array of elements of the same data type. NumPy provides efficient operations on these arrays, including mathematical, logical, statistical, and linear algebraic operations. Additionally, NumPy has a large number of built-in functions for working with arrays and the ability to easily read and write data in various formats. While this might sound complex in theory, using the NumPy library is straightforward in practice, despite being crucial for numerical computing in Python. Python was not initially designed for numerical computing but has attracted the attention of the scientific and engineering community.

As a result, a special interest group called Matrix-SIG was founded in 1995 with the goal of defining a set of computational packages for numerical computing. Thanks to NumPy, Python has become a powerful language for numerical computing, data analysis, machine learning, and other areas of scientific research. Regarding NumPy's limitations, it is designed for homogeneous data, requires arrays to be pre-defined in size, array operations require additional memory, lacks out-of-the-box parallelization, and has limited support for non-numerical operations. Despite these limitations, NumPy still provides exceptional value and efficiency in numerical computing. Many of these limitations can be overcome by using other libraries or customizing the code to specific needs.

NumPy in Action: A Practical Guide for Beginners

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? 
 

Saturday, October 31, 2015

Standardne funkcije u C++ programskom jeziku

Najčešće standardne funkcije koje se najviše koriste u C++ programskom jeziku su C-String funkcije, funkcije za pojedinačne znakove, matematičke funkcije, funkcije za slučajno biranje brojeva i funkcije za datum i vreme. Rad sa funkcijama iz standardne biblioteke je zaista velika pomoć prilikom programiranja i pored toga što se na primer u C# programskom jeziku, ništa ne može porediti sa .NET Framework klasama, tako se ništa ne može porediti sa konstantnom upotrebom biblioteke standardni funkcija u C programskom jeziku, ali za  C++ programski jezik, standardna biblioteka nije baš bezbedna dok postoje i bolja rešenja. Ono što izdvaja standardne funkcije je njihova jednosatvna upotreba i navika C programera. Upotreba standardne biblioteke zahteva da uvezete određeni header fajl u vaš program. Najbolje će te shvatati standardne funkcije njihovom upotrebom kodiranjem.


( The C++ Standard Library )

C-String funkcije

C-String funkcije se primenjuju na klasične char* stringove, C programskog jezika <cstring>. Nemojte to da mešate sa funkcijama klase string <string> o kojoj je bilo više reči u postu „Rad sa stringovima u C++ programskom jeziku“. Ja lično ne preporučujem da koristite <cstring> ili objekte Cstring u vašim C++ programima. Uvek koristite <string>. Ali pošte će te često nailaziti na ovakav kod u nekim primerima i starijim projektima, čisto da prepoznate o kakvim funkcijama se radi. Upotreba <cstring> funkcija nije bezbedno koristiti i zato će Microsoft Visual Studio prijavljivati vam grešku kad pokušate da koristite <cstring> osim ukoliko ne naznačite da ne prijavljuje; kliknite desnim tasterom miša na Project u Solution Explorer.

Project -> Properties -> C/C++ -> Preprocessor -> Preprocessor Definitions

Unesite:

               _CRT_SECURE_NO_WARNINGS

Pogledajte primer i analizirajte kod programa kako se upotrebljavaju <cstring> funkcije: