Found 1339 Articles for C

Swapping two variable value without using third variable in C/C++

Samual Sam
Updated on 26-Jun-2020 09:12:20

540 Views

The following is an example of swapping two variables.Example Live Demo#include int main() {    int a, b;    printf("Enter the value of a : ");    scanf("%d", &a);    printf("Enter the value of b : ");    scanf("%d", &b);    a += b -= a = b - a;    printf("After Swapping : %d\t%d", a, b);    return 0; }OutputEnter the value of a : 23 Enter the value of b : 43 After Swapping : 4323In the above program, two variables a and b are declared and initialized dynamically at run time.int a, b; printf("Enter the value of ... Read More

Initialization of variable sized arrays in C

karthikeya Boyini
Updated on 26-Jun-2020 08:59:10

7K+ Views

Variable sized arrays are data structures whose length is determined at runtime rather than compile time. These arrays are useful in simplifying numerical algorithm programming. The C99 is a C programming standard that allows variable sized arrays.A program that demonstrates variable sized arrays in C is given as follows −Example Live Demo#include int main(){    int n;    printf("Enter the size of the array: ");    scanf("%d", &n);    int arr[n];    for(int i=0; i

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));

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

Revathi Satya Kondra
Updated on 02-May-2025 13:39:59

729 Views

In C/C++, both exit() and _Exit() are used to terminate a program. The exit() performs cleanup like flushing output, closing files, and calling functions, while _Exit() ends the program immediately without doing any cleanup. Now, let us learn the difference between exit() and _Exit() individually in C/C++. C++ exit() Function The exit() function is used to clean up before terminating the program. It calls functions registered with atexit(), flushes file buffers, and returns control to the operating system. Syntax Following is the syntax for exit() function: void exit(int status_value); Example In this example, we print "Program is running..." and ... Read More

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

karthikeya Boyini
Updated on 23-Sep-2024 18:19:14

2K+ Views

return statement The C++ 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 #include using namespace std; class Method { public: Method() { cout

C function to Swap strings

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

745 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

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

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

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;Here, datatype − The datatype ... Read More

Advertisements