Found 7347 Articles for C++

How to check if input is numeric in C++?

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

5K+ Views

Here we will see how to check whether a given input is numeric string or a normal string. The numeric string will hold all characters that are in range 0 – 9. The solution is very simple, we will simply go through each characters one by one, and check whether it is numeric or not. If it is numeric, then point to the next, otherwise return false value.Example#include using namespace std; bool isNumeric(string str) {    for (int i = 0; i < str.length(); i++)       if (isdigit(str[i]) == false)          return false; //when ... Read More

How to find the size of an int[] in C/C++?

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

7K+ Views

In this section, we will see how we can get the size of an integer array in C or C++? The size of int[] is basically counting the number of elements inside that array. To get this we can use the sizeof() operator. If the array name is passed inside the sizeof(), then it will return total size of memory blocks that are occupied by the array. Now if we divide it by the size of each element, then we can get number of elements.Let us see the following example to get the better idea about it.Example#include using namespace ... Read More

How to implement a copy constructors in C++?

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

410 Views

Here we will see how the copy constructors are implemented in C++. Before discussing that we should know what is the copy constructor.The copy constructor is a constructor which creates an object by initializing it with an object of the same class, which has been created previously. The copy constructor is used to −Initialize one object from another of the same type.Copy an object to pass it as an argument to a function.Copy an object to return it from a function.If a copy constructor is not defined in a class, the compiler itself defines one. If the class has pointer ... Read More

Remove function in C/C++

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

203 Views

The C library function int remove(const char *filename) deletes the given filename so that it is no longer accessible.Following is the declaration for remove() function.int remove(const char *filename)This function takes the filename. This is the C string containing the name of the file to be deleted. On success, zero is returned. On error, -1 is returned, and errno is set appropriately.Example#include #include int main () {    int ret;    FILE *fp;    char filename[] = "file.txt";    fp = fopen(filename, "w");    fprintf(fp, "%s", "This is tutorialspoint.com");    fclose(fp);    ret = remove(filename);    if(ret == 0) { ... Read More

Rename function in C/C++

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

286 Views

The C library function int rename(const char *old_filename, const char *new_filename) causes the filename referred to by old_filename to be changed to new_filenameFollowing is the declaration for rename() function.int rename(const char *old_filename, const char *new_filename)The parameters are old_filename − This is the C string containing the name of the file to be renamed and/or moved, new_filename − This is the C string containing the new name for the file.On success, zero is returned. On error, -1 is returned, and errno is set appropriately.Example#include int main () {    int ret;    char oldname[] = "file.txt";    char newname[] = ... Read More

Operations on struct variables in C

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

1K+ Views

Here we will see what type of operations can be performed on struct variables. Here basically one operation can be performed for struct. The operation is assignment operation. Some other operations like equality check or other are not available for stack.Example#include typedef struct { //define a structure for complex objects    int real, imag; }complex; void displayComplex(complex c){    printf("(%d + %di)", c.real, c.imag); } main() {    complex c1 = {5, 2};    complex c2 = {8, 6};    printf("Complex numbers are:");    displayComplex(c1);    displayComplex(c2); }OutputComplex numbers are: (5 + 2i) (8 + 6i)This works fine as ... Read More

Restrict keyword in C

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

1K+ Views

Here we will see what is the restrict keyword in C. The restrict keyword first introduced in C99 version. Let us see what is actually this restrict keyword.The restrict keyword is used for pointer declarations as a type quantifier of the pointer.This keyword does not add new functionalities. Using this the programmer can inform about an optimization that compiler can make.When the restrict keyword is used with a pointer p, then it tells the compiler, that ptr is only way to access the object pointed by this. So compiler will not add any additional checks.If the programmer uses restrict keyword ... Read More

__func__ identifier in C

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

241 Views

Here we will see what is the __func__ C.Basically the __func__ or __FUNCTION__ (Some old versions of C and C+ + supports __func__). This macro is used to get the name of the current function.Example#include void TestFunction(){    printf("Output of __func__ is: %s", __func__ ); } main() {    printf("Output of __func__ is: %s", __func__ );    TestFunction(); }OutputOutput of __func__ is: main Output of __func__ is: TestFunction

Difference between while(1) and while(0) in C/C++

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

3K+ Views

Here we will see what are the differences between while(1) and while(0) in C or C++. The while is a loop of C or C++. Using this loop we can check one condition, and the statements inside the loop will be executed while the condition is true.The while(1) or while(any non-zero value) is used for infinite loop. There is no condition for while. As 1 or any non-zero value is present, then the condition is always true. So what are present inside the loop that will be executed forever. To come out from this infinite loop, we have to use ... Read More

How a Preprocessor works in C/C++?

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

295 Views

Here we will see how the preprocessors are working in C or C++. Let us see what are the preprocessors.The preprocessors are the directives, which give instructions to the compiler to preprocess the information before actual compilation starts.All preprocessor directives begin with #, and only white-space characters may appear before a preprocessor directive on a line. Preprocessor directives are not C++ statements, so they do not end in a semicolon (;).You already have seen a #include directive in all the examples. This macro is used to include a header file into the source file.There are number of preprocessor directives supported ... Read More

Advertisements