Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C Articles
Page 83 of 96
How to measure time taken by a function in C?
Here we will see how to calculate the time taken by a process or function in C. For this problem, we will use the clock() function. The clock() function is present in the time.h header file. To get the elapsed time, we can get the time using clock() at the beginning, and at the end of the tasks, then subtract the values to get the differences. After that, we will divide the difference by CLOCKS_PER_SEC (Number of clock ticks per second) to get the processor time. Syntax clock_t clock(void); Example Here's a complete ...
Read MoreHow to change the output of printf() in main()?
Here we will see how to change the output of the printf() function from main(). We will define a macro that will modify all printf() statements to use a different value than what is passed to it. We will use the #define macro to accomplish this task. This macro will be defined inside a function, allowing us to control when the printf() behavior changes. By calling the function from main(), we can control exactly when printf() starts behaving differently. Syntax #define printf(format, value) printf(format, replacement_value); Example In this example, we define a macro ...
Read MoreAssigning multiple characters in an int in C language
The character type data is stored by its ASCII value internally in C. If we want to print a single character as integer, we will get the ASCII value. But when we are trying to print more than one character using a single quote, then it will print some strange output. Syntax int variable = 'character'; int variable = 'multiple_chars'; // Non-standard behavior Example Please check the following program to get the idea − #include int main() { printf("%d", 'A'); printf("%d", ...
Read MoreAddress of a function in C or C++
In C programming, every function is stored in the computer's memory and has a unique memory address, just like variables. We can access and display these function addresses to understand how functions are stored in memory. Syntax functionName // Returns address of the function (void*)functionName // Cast to void pointer for printing Accessing Address of a Function To access the address of a function, we use its name without parentheses. When we write hello() with parentheses, we're calling the function. But when we write just hello, it ...
Read MoreImplicit initialization of variables with 0 or 1 in C
In C programming, uninitialized variables and partially initialized arrays follow specific rules for implicit initialization. Global variables, static variables, and array elements are automatically initialized to zero, while local variables contain garbage values if not explicitly initialized. Syntax // Global variables - implicitly initialized to 0 int global_var; int global_array[SIZE]; // Partial array initialization - remaining elements become 0 int array[SIZE] = {value1, value2, ...}; Example 1: Global Variables Implicit Initialization Global and static variables are automatically initialized to zero − #include int x, y; ...
Read MoreStorage of integer and character values in C
We have used the integer and character variables many times in our program. Here we will see how they are stored in the memory. In C the character values are also stored as integers. When we assign a value larger than the character range to a char variable, overflow occurs and only the lower 8 bits are stored. Syntax char variable_name = value; int variable_name = value; Example: Character Overflow Behavior In the following code, we shall put 270 into a character type data. The binary equivalent of 270 is 100001110, but char ...
Read MoreConvert C/C++ program to Preprocessor code
Here we will see how to generate the preprocessed or preprocessor code from the source code of a C or C++ program. To see the preprocessed code using gcc compiler, we have to use the '-E' option with the gcc. The preprocessor includes all of the # directives in the code, and also expands the MACRO function. Syntax gcc -E program.c Example Let's create a simple C program with macro definitions and see how the preprocessor expands them − #include #define PI 3.1415 #define SQUARE(x) ((x) * (x)) int ...
Read MoreHow does a C program executes?
Here we will see how C programs are executed in a system. This is basically the compilation process of a C program from source code to executable machine code. The following diagram shows how a C source code file gets converted into an executable program − C Source Code (.c file) Preprocessor (.i file) ...
Read MoreUninitialized primitive data types in C/C++
In C programming, uninitialized primitive data types contain unpredictable values called "garbage values". The C standard does not guarantee that these variables will be initialized to zero or any specific value. The actual values depend on what was previously stored in those memory locations. Syntax data_type variable_name; // Uninitialized variable data_type variable_name = initial_value; // Initialized variable Example: Uninitialized Variables Let's examine what happens when we declare variables without initializing them − #include int main() { char a; float b; ...
Read MoreHow to modify a const variable in C?
In C, const variables are designed to be immutable after initialization. However, there are ways to modify their values using pointers, though this practice leads to undefined behavior and should be avoided in production code. Syntax const data_type variable_name = value; int *ptr = (int*)&const_variable; // Cast away const *ptr = new_value; // Modify through pointer Example 1: Attempting Direct Modification (Compile Error) Direct assignment to a const variable results in a compilation error − #include int main() { const int x = 10; ...
Read More