Interview logo

Part - 1 (15)C programming interview questions and answers

Easily to get knowledge and make worth full preparation

By Sedhu SPublished about a year ago 4 min read
Like

What is a pointer and how is it different from a variable? A pointer is a variable that stores the memory address of another variable. It allows a program to manipulate the memory directly rather than indirectly through a variable.

What is the difference between a call by value and a call by reference? In a call by value, a copy of the value of the variable is passed to the function. Changes made to the parameter within the function do not affect the original variable outside of the function. In a call by reference, the memory address of the variable is passed to the function. Changes made to the parameter within the function will affect the original variable outside of the function.

What is the difference between a structure and a union in C? A structure is a collection of variables of different data types, while a union is a collection of variables that share the same memory location. This means that when one variable in a union is modified, the others are also affected.

What is the difference between a static variable and a global variable in C? A static variable is only visible within the function or block of code where it is defined and retains its value between function calls. A global variable is visible throughout the entire program and retains its value between function calls.

What is the use of the 'const' keyword in C? The 'const' keyword is used to declare a variable as a constant, meaning its value cannot be changed after it has been initialized. This can be useful for ensuring that certain values in a program remain unchanged, such as mathematical constants or program settings.

How do you dynamically allocate memory in C? Memory can be dynamically allocated in C using the malloc() and calloc() functions. malloc() takes a single argument, the number of bytes to allocate, and returns a pointer to the beginning of the allocated memory. calloc() takes two arguments, the number of elements and the size of each element, and also returns a pointer to the beginning of the allocated memory.

What is the difference between a while loop and a do-while loop in C? A while loop repeatedly executes a block of code as long as a given condition is true. A do-while loop also repeatedly executes a block of code, but the condition is checked at the end of the loop. This means that the block of code will always be executed at least once in a do-while loop.

What is the purpose of the 'enum' keyword in C? The 'enum' keyword is used to define an enumerated data type, which allows a variable to be assigned one of a predefined set of values. This can make the code more readable and reduce the likelihood of errors by limiting the possible values of a variable.

How do you use pointers to pass an array to a function in C? An array can be passed to a function by passing a pointer to the first element of the array. The pointer can then be used to access and manipulate the elements of the array within the function.

What is the difference between the '&' and '' operators in C? The '&' operator is the address-of operator and is used to obtain the memory address of a variable. The '' operator is the dereference operator and is used to access the value stored at a memory address.

How can you use the 'sizeof' operator in C? The 'sizeof' operator is used to determine the size, in bytes, of a variable or data type. It can be used to allocate memory dynamically, to check the size of an array, or to determine the size of a struct or union.

What is the purpose of the 'typedef' keyword in C? The 'typedef' keyword is used to create an alias for a data type. This can make the code more readable and reduce the likelihood of errors by making the code more self-explanatory.

How do you use the '#define' preprocessor directive in C? The '#define' preprocessor directive is used to define a constant or a macro in C. It can be used to define symbolic constants, such as mathematical constants or program settings, or to create macros that can be used to simplify complex expressions or statements.

How do you use the '#include' preprocessor directive in C? The '#include' preprocessor directive is used to include the contents of another file into the current source file. This can be used to include standard library headers, such as <stdio.h>, or to include headers for user-defined functions or data types.

How do you handle errors and exceptions in C? Errors and exceptions in C can be handled using the 'return' statement, the 'goto' statement and the setjmp() and longjmp() functions. The 'return' statement can be used to exit a function and return a specific value or error code. The 'goto' statement can be used to jump to a specific point in the code in case of an error. The setjmp() and longjmp() functions can be used to save and restore the context of a program, allowing for a non-local jump to a specific point in the code in case of an error.

Documentary
Like

About the Creator

Reader insights

Be the first to share your insights about this piece.

How does it work?

Add your insights

Comments

There are no comments for this story

Be the first to respond and start the conversation.

Sign in to comment

    Find us on social media

    Miscellaneous links

    • Explore
    • Contact
    • Privacy Policy
    • Terms of Use
    • Support

    © 2024 Creatd, Inc. All Rights Reserved.