C Articles - Page 106 of 134

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

2K+ 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

633 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

462 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

136 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

516 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

304 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

Undefined Behaviour in C and C++

Tapas Kumar Ghosh
Updated on 16-Jun-2025 16:53:18

399 Views

In C/C++, undefined behavior refers to unexpected program output. When we write any program based on C or C++, sometimes we do not get the output as expected. This may be due to undefined behavior which cannot be predictable. For reference, sometimes it will show correct result, sometimes it will give the wrong result, and sometimes it may crash. Common Examples of Undefined Behavior in C/C++ Now, we have list of example that shows undefined behavior in C/C++ programs: Divide by Zero Using Uninitialized Variable NULL Pointer ... Read More

How to check if a variable is NULL in C/C++?

Tapas Kumar Ghosh
Updated on 11-Jun-2025 14:20:52

27K+ Views

In C/C++, there is no special method for comparing NULL values. We can use if statements to check whether a variable is null or not.Here, we will attempt to open a file in read mode that does not exist on the system. In this case, the function will return a NULL pointer. We can check whether it is NULL or not using an if statement. C Example to Check Variable is NULL or Not In this example, we check whether the file is exist or not using NULL. #include int main() { //try to open ... Read More

Advertisements