C Articles

Page 92 of 96

Static functions in C

Samual Sam
Samual Sam
Updated on 15-Mar-2026 21K+ Views

A static function in C is a function that has a scope limited to its object file. This means the static function is only visible within the file where it is defined and cannot be accessed from other files. A function can be declared as static by placing the static keyword before the function name. Syntax static return_type function_name(parameters) { // function body } Example 1: Static Function in Same File Here's an example demonstrating a static function called within the same file − #include static ...

Read More

exit(), abort() and assert() in C/C++

Tapas Kumar Ghosh
Tapas Kumar Ghosh
Updated on 15-Mar-2026 2K+ Views

In C programming, the exit(), abort(), and assert() functions are used for program termination and debugging. Each function serves a different purpose and is defined in different header files. The exit() Function The exit() function is used to terminate a program immediately in a normal way. It is defined in the header file and allows the program to return a status code to the operating system. Syntax void exit(int status_value); Parameters: status_value − The termination status code (0 indicates success, non-zero indicates error) Example In this example, the ...

Read More

Swap two variables in one line in C/C+

Tapas Kumar Ghosh
Tapas Kumar Ghosh
Updated on 15-Mar-2026 2K+ Views

In C programming, swapping two variables means exchanging the values stored in them. There are multiple ways to implement swapping using a single line statement. This article demonstrates various approaches to swap variable values efficiently. Syntax // General concept variable1 = new_value_of_variable2; variable2 = new_value_of_variable1; Example Scenario Let's consider the following input and expected output − Input: int a = 5; int b = 10; Output: a = 10 b = 5 Method 1: Using Arithmetic Operations This method uses arithmetic operations to swap variables without temporary storage. ...

Read More

What are Wild Pointers in C/C++?

Tapas Kumar Ghosh
Tapas Kumar Ghosh
Updated on 15-Mar-2026 4K+ Views

In C, a wild pointer is a pointer that has not been initialized to a valid memory address. When a pointer is declared but not assigned a value, it contains garbage data and points to an unknown memory location, making it unpredictable and dangerous to use. Syntax data_type *pointer_name; // Uninitialized pointer (wild pointer) Example: Wild Pointer Behavior Here's an example demonstrating a wild pointer that attempts to access uninitialized memory − #include int main() { int *ptr; // Wild pointer - ...

Read More

How will you print numbers from 1 to 100 without using loop in C?

Tapas Kumar Ghosh
Tapas Kumar Ghosh
Updated on 15-Mar-2026 4K+ Views

You can print numbers from 1 to 100 without using traditional loops by employing alternative control structures like recursive functions and goto statements. These methods achieve the same repetitive behavior through different mechanisms. Method 1: Using Recursion Recursion allows a function to call itself repeatedly until a base condition is met. We can use this property to print numbers sequentially without explicit loops − #include void printNumbers(int n) { if (n > 100) return; printf("%d ", n); ...

Read More

How to clear console in C?

Samual Sam
Samual Sam
Updated on 15-Mar-2026 22K+ Views

In C, clearing the console or output screen can be achieved using several methods. While clrscr() from conio.h was commonly used in older compilers, modern standard C uses system() calls for cross-platform compatibility. Note: The examples below use system() calls which work in most environments. For online compilers, console clearing may not be visible as output is captured after program execution. Syntax The following are standard methods to clear the console in C − #include system("cls"); /* Windows */ Or, #include system("clear"); /* ...

Read More

atexit() function in C/C++

karthikeya Boyini
karthikeya Boyini
Updated on 15-Mar-2026 461 Views

The atexit() function in C is used to register functions that will be called automatically when the program terminates normally. These registered functions are executed in reverse order (LIFO - Last In, First Out) after the main function completes but before the program ends. This function is declared in the header file. Syntax int atexit(void (*function_name)(void)) Parameters: function_name − A pointer to the function that will be called at program termination. The function must take no parameters and return void. Return Value: Returns 0 on success, or a non-zero value if ...

Read More

Difference between strlen() and sizeof() for string in C

karthikeya Boyini
karthikeya Boyini
Updated on 15-Mar-2026 1K+ Views

In C programming, strlen() and sizeof() are commonly used to work with strings, but they serve different purposes and return different values. Understanding their differences is crucial for effective string manipulation. strlen() Function The strlen() function is declared in string.h header file and calculates the actual length of a string by counting characters until it encounters the null terminator '\0'. Syntax size_t strlen(const char *string); string − The string whose length is to be calculated. Example #include #include int main() { char s1[10] ...

Read More

Print contents of a file in C

Samual Sam
Samual Sam
Updated on 15-Mar-2026 7K+ Views

In C, printing the contents of a file involves opening the file, reading its data character by character or line by line, and displaying it on the console. The fopen(), fgetc(), and fclose() functions are commonly used for this purpose. Syntax FILE *fopen(const char *filename, const char *mode); int fgetc(FILE *stream); int fclose(FILE *stream); Method 1: Reading Character by Character Let's say we have "new.txt" file with the following content − 0, hell!o 1, hello! 2, gfdtrhtrhrt 3, demo Note: Create a file named "new.txt" in your working directory ...

Read More

EOF, getc() and feof() in C

karthikeya Boyini
karthikeya Boyini
Updated on 15-Mar-2026 27K+ Views

In C programming, EOF (End of File), getc(), and feof() are essential for file handling operations. EOF is a constant that indicates the end of a file stream, while getc() reads characters from files and feof() checks if the end-of-file indicator has been set. EOF (End of File) EOF stands for End of File and is a macro defined in . The getc() function returns EOF when it reaches the end of file or encounters an error. Syntax #define EOF (-1) Example: Using EOF with getc() Note: This example assumes a ...

Read More
Showing 911–920 of 953 articles
« Prev 1 90 91 92 93 94 96 Next »
Advertisements