Found 1339 Articles for C

How to compare pointers in C/C++?

Tapas Kumar Ghosh
Updated on 17-Jun-2025 14:57:16

8K+ Views

The pointers can be directly compared using relational operators. In this article, we will learn about the comparisons of the pointers with the help of examples. Pointers Comparison in C and C++ In C/C++, we can directly compare the pointers using relational operators (==, !=, , =). These operators are used to compare two variables, values, or pointers. It returns a Boolean true value when the comparison is correct otherwise, it is false. The core syntaxes of C and C++ pointers are similar such as declaration, dereferencing, and pointer arithmetic. All of these are identical in behavior and still use ... Read More

Function Pointer in C

Anvi Jain
Updated on 30-Jul-2019 22:30:26

5K+ Views

Function Pointers point to code like normal pointers.In Functions Pointers, function’s name can be used to get function’s address.A function can also be passed as an arguments and can be returned from a function.Declarationfunction_return_type(*Pointer_name)(function argument list)Example Live Demo#include int subtraction (int a, int b) {    return a-b; } int main() {    int (*fp) (int, int)=subtraction;    //Calling function using function pointer    int result = fp(5, 4); printf(" Using function pointer we get the result: %d",result); return 0; }OutputUsing function pointer we get the result: 1

The best way to check if a file exists using standard C/C++

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

18K+ Views

The only way to check if a file exist is to try to open the file for reading or writing.Here is an example −In CExample#include int main() {    /* try to open file to read */    FILE *file;    if (file = fopen("a.txt", "r")) {       fclose(file);       printf("file exists");    } else {       printf("file doesn't exist");    } }Outputfile existsIn C++Example#include #include using namespace std; int main() {    /* try to open file to read */    ifstream ifile;    ifile.open("b.txt");    if(ifile) {       cout

How to write my own header file in C?

Anvi Jain
Updated on 30-Jul-2019 22:30:26

1K+ Views

Steps to write my own header file in C −Type a code and save it as “sub.h”.Write a main program “subtraction.c” in which −include new header file.Write “sub.h” instead of All the functions in sub.h header are now ready for use.Directly call the function sub().Both “subtraction.c” and “sub.h” should be in same folder.Sub.hint sub(int m, int n) {    return(m-n); }subtraction.cExample#include #include "sub.h" void main() {    int a= 7, b= 6, res;    res = sub(a, b);    printf("Subtraction of two numbers is: %d", res); }After running ”subtraction.c” the output will be −OutputSubtraction of two numbers is: 1Read More

How to use use an array of pointers (Jagged) in C/C++?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

619 Views

Let us consider the following example, which uses an array of 3 integers −In CExample Live Demo#include const int MAX = 3; int main () {    int var[] = {10, 100, 200};    int i;    for (i = 0; i < MAX; i++) {       printf("Value of var[%d] = %d", i, var[i] );    }    return 0; }OutputValue of var[0] = 10 Value of var[1] = 100 Value of var[2] = 200In C++Example Live Demo#include using namespace std; const int MAX = 3; int main () {    int var[] = {10, 100, 200};    int i;    for (i = 0; i < MAX; i++) {       cout

ctime() Function in C/C++

Smita Kapse
Updated on 30-Jul-2019 22:30:26

455 Views

The C library function char *ctime(const time_t *timer) returns a string representing the localtime based on the argument timer.The returned string has the following format − Www Mmm dd hh:mm:ss yyyy, where Www is the weekday, Mmm the month in letters, dd the day of the month, hh:mm:ss the time, and yyyy the year.The syntax is like below −char *ctime(const time_t *timer)This function takes the pointer to a time_t, which is containing the calendar time. It returns a string containing date, time info in human readable format.Example Live Demo#include #include int main () {    time_t curtime;    time(&curtime); ... Read More

Write a program that produces different results in C and C++

Anvi Jain
Updated on 30-Jul-2019 22:30:26

124 Views

Here we will see some program that will return different results if they are compiled in C or C++ compilers. We can find many such programs, but here we are discussing about some of them.In C and C++, the character literals are treated as different manner. In C, they are treated as int but in C++, they are treated as characters. So if we check the size using sizeof() operator, it will return 4 in C, and 1 in C++.Live Demo For C.Example Live Demo#include int main() {    printf("The character: %c, size(%d)", 'a', sizeof('a')); }Output(C)The character: a, size(4)Live Demo For ... Read More

C program that won’t compile in C++

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

505 Views

The C++ language is designed by adding some additional features like Object Oriented concepts with C. Most of the C programs can be compiled using C++ compiler also. Though there are some programs that cannot be compiled using C++ compiler.Let us see some code, that will compile in C compiler, but not in C++ compilers.In this program there will be one compilation error for C++ code. Because it is trying to call a function that is not declared before. But in C it may compileLive Demo For C.Example Live Demo#include int main() {    myFunction(); // myFunction() is called before its ... Read More

When to use inline function and when not to use it in C/C++?

Tapas Kumar Ghosh
Updated on 11-Jul-2025 17:48:27

2K+ Views

In C/C++, an inline function is a function where the compiler replaces the function call with the actual code of the function during compilation. So, this makes the program run faster than a normal function call. Why to Use Inline Function in C/C++? We should use an inline function in C/C++ when the function is very simple and small size. Also, avoid the regular function call and replace macros with type safety. Let us understand how an inline function is used for small functions. Suppose we write square(5), the compiler converts it directly to 5*5. This makes the program run ... Read More

Floating Point Operations and Associativity in C, C++ and Java

Chandu yadav
Updated on 30-Jul-2019 22:30:26

287 Views

In C, C++, and java, we do some mathematical operations with floating point numbers. Now here we will check whether the floating point numbers are following the associativity rule or not.The answer is NO. The floating point numbers does not follow the associativity rules in some cases. Here we will see some examples.Example Code#include using namespace std; main() {    float x = -500000000;    float y = 500000000;    float z = 1;    cout

Advertisements