Showing posts with label Interview Questions. Show all posts
Showing posts with label Interview Questions. Show all posts

C Program to Swap two variables without using third variable ?

How to write a C Program to Swap two variables without using third variable in C Programming Language ?


#include<stdio.h>
int main(){
    int a=5,b=10;
/* Type (a)*/
    a=b+a;
    b=a-b;
    a=a-b;
    printf("a= %d  b=  %d",a,b);

/* Type (a)*/
    a=5;
    b=10;
    a=a+b-(b=a);
    printf("\na= %d  b=  %d",a,b);
/* Type (a)*/
    a=5;
    b=10;
    a=a^b;
    b=a^b;
    a=b^a;
    printf("\na= %d  b=  %d",a,b);
   
/* Type (a)*/
    a=5;
    b=10;
    a=b-~a-1;
    b=a+~b+1;
    a=a+~b+1;
    printf("\na= %d  b=  %d",a,b);
   
/* Type (a)*/
    a=5,
    b=10;
    a=b+a,b=a-b,a=a-b;
    printf("\na= %d  b=  %d",a,b);
    return 0;
}


You may also learn these C Program/Code :

C Program To Swap Two Numbers Without Using Third Variable


Write a c program to print Hello world without using any semicolon ?

How to write a C Program to Print Hello World without using any semicolon ?


Solution - Type (a):
void main(){
    if(printf("Hello world")){
    }
}

Solution - Type (b):
void main(){
    while(!printf("Hello world")){
    }
}

Solution - Type (C):
void main(){
    switch(printf("Hello world")){
    }
}

What does static variable mean in C Programming?

What does static variable mean in c language with example in C Programming Language ?


As we have seen, external variables have global scope across the entire program (provided extern declarations are used is files other than where the variable is defined), and a lifetime over the the entire program run. The storage class, static, similarly provides a lifetime over the entire program, however; it provides a way to limit the scope of such variables, Static storage class is declared with the keyword static as the class specifier when the variable is defined. These variables are automatically initialized to zero upon memory allocation just as external variables are. Static storage class can be specified for automatic as well as external variables.


Static automatic variables continue to exist even after the block in which they are defined terminates. Thus, the value of a static variable in a function is retained between repeated function calls to the same function. The scope of static automatic variables is identical to that of automatic variables, i.e. it is local to the block in which it is defined; however, the storage allocated becomes permanent for the duration of the program. Static variables may be initialized in their declarations; however, the initializers must be constant expressions, and initialization is done only once at compile time when memory is allocated for the static variable.

A function-scope or block-scope variable that is declared as static is visible only within that scope. Furthermore, static variables only have a single instance. In the case of function- or block-scope variables, this means that the variable is not “automatic” and thus retains its value across function invocations.

In computer programming, a static variable is a variable that has been allocated statically—whose lifetime or "extent" extends across the entire run of the program. This is in contrast to the more ephemeral automatic variables (local variables are generally automatic), whose storage is allocated and deallocated on the call stack; and in contrast to objects whose storage is dynamically allocated in heap memory.

When the program (executable or library) is loaded into memory, static variables are stored in the data segment of the program's address space (if initialized), or the BSS segment (if uninitialized), and are stored in corresponding sections of object files prior to loading.

The static keyword is used in C and related languages both for static variables and other concepts.

Scope



In terms of scope and extent, static variables have extent the entire run of the program, but may have more limited scope. A basic distinction is between a static global variable, which has global scope and thus is in context throughout the program, and a static local variable, which has local scope. A static local variable is different from local variable. It is initialized only once no matter how many times that function in which it resides is called. It may be used as a count variable. A static variable may also have module scope or some variant, such as internal linkage in C, which is a form of file scope or module scope.

In object-oriented programming, there is also the concept of a static member variable, which is a "class variable" of a statically defined class – a member variable of a given class which is shared across all instances (objects), and is accessible as a member variable of these objects. Note however that a class variable of a dynamically defined class, in languages where classes can be defined at run time, is allocated when the class is defined and is not static.

Example

An example of static local variable in C:
#include <stdio.h>

void func() {
 static int x = 0; 
 /* x is initialized only once across five calls of func() and
   the variable will get incremented five 
   times after these calls. The final value of x will be 5. */
 x++;
 printf("%d\n", x); // outputs the value of x
}

int main() { //int argc, char *argv[] inside the main is optional in the particular program
 func(); // prints 1
 func(); // prints 2
 func(); // prints 3
 func(); // prints 4
 func(); // prints 5
        return 0;
}



    References


    Accept by User C Interview Questions

    C Program to accept n number, C Program to accept matrix from user these are the questions which will be ask to you in any interview. So you have to prepare these question to crack any interview in the world. C Questions are very often asked by the interviewer. C is the simplest and the hardest language in the world since it is invented by Dennis Ritchie. Stay Focused Stay Learning at NaimCProgram.

    Array C Program Interview Questions

    Arrays in C act to store related data under a single variable name with an index, also known as a subscript. It is easiest to think of an array as simply a list or ordered grouping for variables of the same type. Interviewer may ask you some typical question related to Arrays. These C Arrays Question will help you to reach your goal to get your first job. Stay Focused Stay Learning at NaimCProgram.

    Enter by User Programs C Interview Questions

    Something is entered by the user and you have to find out what or you have to calculate the number these C Programming Questions are very easy to answer during the interview. So before going to give your first interview you'll be prepared a lot. Programmer job is to find the most suitable solutions for the client and that what we do. Here, I have a list of programs related to Enter by User. These Questions are very often ask by the interviewer. Stay Focused Stay Learning at NaimCProgram.


  • C Program Exchange User Entered Text Between 2 Modules
  • C Program to accept n numbers from user & find out the maximum element out of them by using dynamic memory allocation
  • C Program to accept string from user & convert all lower case letters into upper case letters & vice versa
  • C Program to accept m*n matrix from user and display the elements of given matrix using function
  • C Program to accept n numbers from user store these numbers into an array & calculate average of n numbers
  • C program to accept n number from user,store these numbers into an array & count the no of occurrences of each number
  • C Program to accept n numbers from user store these numbers into an array & reverse an array elements using function
  • C Program to accept a string from user, delete all vowels from that string & display the result
  • Write a c program to accept a string from user & generate following pattern (e.g, input "abcd")
  • C Program to accept 5 names from user & store these names into an array. Sort these array elements in alphabetical order
  • C Program to accept n numbers from user,store these numbers into an array & sort the number of an array
  • C Program to accept n numbers from user, store these numbers into an array. Find out Maximum & Minimum number from an Array
  • C Program that first prompts you for the number of customers you would like to enter in
  • Accept n number from user, store these number into an array and calculate the average of n number
  • C Program To Read A Parenthesised Infix Expression From The User And Check Whether It Is Well Parenthesised Or Not
  • 5 Digit No. Enter by User Calculate the Sum Of It's Digits
  • Display All Prime Numbers Between Two Interval Entered By User
  • Number Program in C Interview Questions

    These C Program related to numbers will be the next question for you may be asked by the interviewer. Sometimes you will be puzzled answering these question so it's time to learn C Programming Interview Question related to Numbers. And best of luck for your Interview. Stay Focused and Stay Learning Programs at NaimCProgram.