Found 1401 Articles for C

Initialization of a multidimensional array in C

Samual Sam
Updated on 26-Jun-2020 08:59:47

328 Views

Array is a collection of same type of elements at contiguous memory location. The lowest address corresponds to the first element while highest corresponds to last element. Array index starts with zero(0) and ends with the size of array minus one(array size - 1). Array size must be integer greater than zero.Let us see an example, If array size = 10 First index of array = 0 Last index of array = array size - 1 = 10-1 = 9Multi-dimensional arrays are arrays of array. The data is stored in tabular form in row major order.The following is the syntax ... Read More

How to print a variable name in C?

karthikeya Boyini
Updated on 26-Jun-2020 09:00:16

1K+ Views

The following is an example to print variable name.Example Live Demo#include #define VariableName(name) #name int main() {    int name;    char ch;    printf("The variable name : %s", VariableName(name));    printf("The variable name : %s", VariableName(ch));    return 0; }OutputThe variable name : name The variable name : chIn the above program, the variable names are printed by defining the method before main()#define VariableName(name) #nameTwo variables of different datatypes are declared. By using the defined function, variable names are printed.int name; char ch; printf("The variable name : %s", VariableName(name)); printf("The variable name : %s", VariableName(ch));

Pointer to an Array in C

Samual Sam
Updated on 14-Sep-2023 21:15:58

26K+ Views

Pointers are variables which stores the address of another variable. When we allocate memory to a variable, pointer points to the address of the variable. Unary operator (*) is used to declare a variable and it returns the address of the allocated memory. Pointers to an array points the address of memory block of an array variable.The following is the syntax of array pointers.datatype *variable_name[size];Here, datatype − The datatype of variable like int, char, float, etc.variable_name − This is the name of variable given by user.size − The size of array variable.The following is an example of array pointers.Example Live Demo#include ... Read More

exit() vs _Exit() in C/C++

Samual Sam
Updated on 26-Jun-2020 08:43:18

510 Views

exit()The function exit() is used to terminate the calling function immediately without executing further processes. As exit() function calls, it terminates processes. It calls the constructor of class only. It is declared in “stdlib.h” header file in C language. It does not return anything.The following is the syntax of exit()void exit(int status_value);Here, status_value − The value which is returned to parent process.The following is an example of exit()Example Live Demo#include #include int main() {    int x = 10;    printf("The value of x : %d", x);    exit(0);    printf("Calling of exit()");    return 0; }OutputThe value of ... Read More

return statement vs exit() in main() C++

karthikeya Boyini
Updated on 26-Jun-2020 08:44:19

1K+ Views

return statementThe return statement terminates the execution of function and it returns the control to the calling function. It calls the constructor as well as the destructor. It returns an integer value for “int main()”.The following is the syntax of return statement.return expression;Here,expression − The expression or any value to be returned.The following is an example of return statement.Example Live Demo#include using namespace std; class Method {    public:    Method() {       cout

C function to Swap strings

Samual Sam
Updated on 26-Jun-2020 08:44:51

597 Views

The following is an example to swap strings.Example Live Demo#include #include int main() {    char st1[] = "My 1st string";    char st2[] = "My 2nd string";    char swap;    int i = 0;    while(st1[i] != '\0') {       swap = st1[i];       st1[i] = st2[i];       st2[i] = swap;       i++;    }    printf("After swapping s1 : %s", st1);    printf("After swapping s2 : %s", st2);    return 0; }OutputAfter swapping s1 : My 2nd string After swapping s2 : My 1st stringIn the above program, two ... Read More

Initialization of static variables in C

karthikeya Boyini
Updated on 26-Jun-2020 08:45:18

4K+ Views

When static keyword is used, variable or data members or functions can not be modified again. It is allocated for the lifetime of program. Static functions can be called directly by using class name.Static variables are initialized only once. Compiler persist the variable till the end of the program. Static variable can be defined inside or outside the function. They are local to the block. The default value of static variable is zero. The static variables are alive till the execution of the program.Here is the syntax of static variables in C language, static datatype variable_name = value;Here, datatype − ... Read More

Return values of printf() and scanf() in C

Samual Sam
Updated on 26-Jun-2020 08:25:18

11K+ Views

The printf() and scanf() functions are required for output and input respectively in C. Both of these functions are library functions and are defined in the stdio.h header file.Details about the return values of the printf() and scanf() functions are given as follows −The printf() functionThe printf() function is used for printing the output. It returns the number of characters that are printed. If there is some error then it returns a negative value.A program that demonstrates this is as follows −Example Live Demo#include int main(){    char str[] = "THE SKY IS BLUE";    printf("The value returned ... Read More

Return type of getchar(), fgetc() and getc() in C

karthikeya Boyini
Updated on 26-Jun-2020 08:30:45

2K+ Views

Details about getchar(), fgetc() and getc() functions in C programming are given as follows −The getchar() functionThe getchar() function obtains a character from stdin. It returns the character that was read in the form of an integer or EOF if an error occurs.A program that demonstrates this is as follows −Example Live Demo#include int main (){    int i;    printf("Enter a character: ");    i = getchar();    printf("The character entered is: ");    putchar(i);    return(0); }OutputThe output of the above program is as follows −Enter a character: G The character entered is: GNow ... Read More

Default values of static variables in C

Samual Sam
Updated on 26-Jun-2020 08:31:52

3K+ Views

When static keyword is used, variable or data members or functions can not be modified again. It is allocated for the lifetime of program. Static functions can be called directly by using class name.Static variables are initialized only once. Compiler persist the variable till the end of the program. Static variable can be defined inside or outside the function. They are local to the block. The default value of static variable is zero. The static variables are alive till the execution of the program.Here is the syntax of static variables in C language, static datatype variable_name;Here, datatype − The datatype ... Read More

Advertisements