Found 1401 Articles for C

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

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

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

514 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

331 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

55 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

367 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

Format specifiers in C

Smita Kapse
Updated on 31-Aug-2023 02:07:15

118K+ Views

The format specifiers are used in C for input and output purposes. Using this concept the compiler can understand that what type of data is in a variable during taking input using the scanf() function and printing using printf() function. Here is a list of format specifiers.Format SpecifierType%cCharacter%dSigned integer%e or %EScientific notation of floats%fFloat values%g or %GSimilar as %e or %E%hiSigned integer (short)%huUnsigned Integer (short)%iUnsigned integer%l or %ld or %liLong%lfDouble%LfLong double%luUnsigned int or unsigned long%lli or %lldLong long%lluUnsigned long long%oOctal representation%pPointer%sString%uUnsigned int%x or %XHexadecimal representation%nPrints nothing%%Prints % characterThese are the basic format specifiers. We can add some other parts with ... Read More

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

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

2K+ Views

In C++, there is one good feature called inline function. This kind of functions are like the macros of C or C++. To use inline functions, we have to specify the inline keyword. We can use this type of functions anywhere, but we should follow some guideline.When inline can be used?Inline functions can be used in the place of macros (#define)For small functions we can use inline functions. It creates faster code and smaller executables.When functions are small and called very often, we can use inline.When we should avoid the use of inline?We should not use functions that are I/O ... Read More

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

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

189 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++

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

220 Views

Here we will see some C and C++ Codes. and try to guess the results. The codes will generate some runtime errors.1. The Divide By Zero error is undefined.Example Code#include using namespace std; int main() {    int x = 10, y = 0;    int z = x / y;    cout

Advertisements