what is the Difference between arrays and pointers?

What is the Difference between arrays and pointers?

- Pointers are used to manipulate data using the address. Pointers use
* operator to access the data pointed to by them
- Arrays use subscripted variables to access and manipulate data.
Array variables can be equivalently written using pointer expression.

Program to show that array and pointers are different
#include  

int main()
{
   int arr[] = {10, 20, 30, 40, 50, 60};
   int *ptr = arr;
   
   // sizof(int) * (number of element in arr[]) is printed
   printf("Size of arr[] %ld\n", sizeof(arr));

   // sizeof a pointer is printed which is same for all type
   // of pointers (char *, void *, etc)
   printf("Size of ptr %ld", sizeof(ptr));
   return 0;
}

Output:
Size of arr[] 24
Size of ptr 8
Assigning any address to an array variable is not allowed.

Keywords:
relation between array and pointer in c
differentiate between array of pointers and pointers to array
difference between array and pointer in c in hindi
difference between array and pointer in hindi
which of arrays or pointers are faster
difference between array and structure
array vs pointer in c
difference between pointer and reference

How pointers are initialized?

How pointers are initialized?

Pointer variable are initialized by one of the following two ways
- Static memory allocation
- Dynamic memory allocation

Keywords:
how to initialize a pointer c++
declaration and initialization in c
pointers in c
initialize pointer with address
declaration and initialization of pointer variable in c
pointer arithmetic in c
syntax of pointer initialization
pointer declaration in c++

what is far pointer?where will you use far pointer?

What is far pointer?when will you use far pointer?

Sometimes you can get away with using a small memory model in
most of a given program. There might be just a few things that don’t fit
in your small data and code segments. When that happens, you can
use explicit far pointers and function declarations to get at the rest of
memory. A far function can be outside the 64KB segment most
functions are shoehorned into for a small-code model. (Often, libraries
are declared explicitly far, so they’ll work no matter what code model
the program uses.) A far pointer can refer to information outside the
64KB data segment. Typically, such pointers are used with farmalloc()
and such, to manage a heap separate from where all the rest of the
data lives. If you use a small-data, large-code model, you should
explicitly make your function pointers far.
Keywords:
far pointer syntax in c
far pointer can access which memory location
how to use far pointer
dangling pointer complex pointer near pointer far pointer huge pointer
complex pointer in c
pointers in c
types of pointers in c
wild pointer in c

What is a null pointer?

What is a null pointer?

There are times when it’s necessary to have a pointer that doesn’t
point to anything. The macro NULL, defined in , has a value that’s
guaranteed to be different from any valid pointer. NULL is a literal zero,
possibly cast to void* or char*. Some people, notably C++
programmers, prefer to use 0 rather than NULL.
The null pointer is used in three ways:
1) To stop indirection in a recursive data structure
2) As an error value
3) As a sentinel value

Difference between const char* p and char const* p?

Difference between const char* p and char const* p?

In const char* p, the character pointed by ‘p’ is constant, so u cant change the value of character pointed by p but u can make ‘p’ refer to some other location. in char const* p,

the ptr ‘p’ is constant not the character referenced by it, so u cant make ‘p’ to reference to any other location but u can change the value of the char pointed by ‘p’.

Keywords:
const char
const char* string
difference between char *p and char *p
const pointer
difference between const char and string
pointer to const char array
static const char in c
const char* in cpp

how to reduce executable size?

How to reduce executable size?

Size of executable can be reduced by Dynamic linking of libraries

Keywords:
c++ reduce executable size
gcc reduce executable size
gcc strip
is it possible to reduce executable file size in c++ and how
how to reduce the size of your application(executable) in c
gcc smallest executable
linux strip command
stm32 reduce code size

How to check whether a linked list is circular?

 How to check whether a linked list is circular?

Create two pointers, and set both to the start of the list. Update them as increment first pointer by one and second pointer by two if its a circular link list they will defenitely meet at some point

Below is a sample program to check circular link list:-
while (pointer1) {
pointer1 = pointer1->next;
pointer2 = pointer2->next;
if (pointer2) pointer2=pointer2->next;
if (pointer1 == pointer2) {
print ("circular");
}}
Keywords:
circular linked list
circular linked list java
check if linked list is palindrome
circular linked list with one node
floyd's cycle-finding algorithm c++
circular linked list in c
reverse a linked list
circular linked list in c++

What is the output of printf("%d")?

What is the output of printf("%d")?

printf() Function
What is the output of printf("%d")?

1. When we write printf("%d",x); this means compiler will print the
value of x. But as here, there is nothing after %d so compiler will show
in output window garbage value.
2. When we use %d the compiler internally uses it to access the
argument in the stack (argument stack). Ideally compiler determines
the offset of the data variable depending on the format specification
string. Now when we write printf("%d",a) then compiler first accesses
the top most element in the argument stack of the printf which is %d
and depending on the format string it calculated to offset to the actual
data variable in the memory which is to be printed. Now when only %d
will be present in the printf then compiler will calculate the correct
offset (which will be the offset to access the integer variable) but as
the actual data object is to be printed is not present at that memory
location so it will print what ever will be the contents of that memory
location.
3. Some compilers check the format string and will generate an error
without the proper number and type of arguments for things like
printf(...) and scanf(...).

Keywords:
c interview questions
c++ faq
faq on pointers in c
faq on arrays in c
faq videos
c tutorial
faqs on strings in c
steve summit
faq on pointers in c
faq on arrays in c
c++ faq
programming in c textbook
c tutorial
c programming resources
c language reference
cinterviews.com
printf(%d main)
printf(%d 1)
online c compiler
output of printf d
how printf and scanf works
return type of printf() in c

What does static variable mean?

What does static variable mean?

There are 3 main uses for the static.

1. If you declare within a function:
It retains the value between function calls

2.If it is declared for a function name:
By default function is extern..so it will be visible from other files if the function declaration is as static..it is invisible for the outer files

3. Static for global variables:
By default we can use the global variables from outside files If it is static global..that variable is limited to with in the file.

Static variable sample program:-
#include  int t = 10;  

main(){

    int x = 0
    void funct1();
    funct1();            
    printf("After first call \n");
    funct1();            
    printf("After second call \n");
    funct1();            
    printf("After third call \n");

}
void funct1()
{
    static int y = 0;  
    int z = 10;             
    printf("value of y %d z %d",y,z);
    y=y+10;
}


output :- 
value of y 0 z 10 After first call
value of y 10 z 10 After second call
value of y 20 z 10 After third call


Keywords:
static variables java
static variable c
static variable in c++
static variable vs global variable
global static variable in c
static variable in c#
static function
static variable in python
c interview questions
c++ faq
faq on pointers in c
faq on arrays in c
faq videos
c tutorial
faqs on strings in c
steve summit
faq on pointers in c
faq on arrays in c
c++ faq
programming in c textbook
c tutorial
c programming resources
c language reference
cinterviews.com


for what daemon is responsible for in linux?

For what daemon is responsible for in linux?


Question: what daemon is responsible for in linux?
Ans: syslogd

The syslogd daemon is responsible for tracking  system information and saving it to specified log files.
Keywords:
daemon process in linux
linux daemon list
daemon etymology
linux daemon command
what is super daemon in linux
daemon process geeksforgeeks
daemon user in linux
where are daemons located in linux

what is kernal upgradation

What is kernal upgradation?

kernel updation is a process of to upgrade the kernel file with a new or latest version of kernel & use of them as a default kernel.or we can say as 

Kernel updation is upgrading kernel version. To update kernel either you can enroll yourself to the official website of red hat or you can simply download the latest kernel version and then running rpm -u kernel version. But this method is not recommended, it is better to install the new kernel and let the older version of it remain untouched so that you can revert back the changes.
Keywords:
sap kernel upgrade in distributed environment
kernel upgrade in linux
difference between kernel upgrade and kernel patch in sap
hana kernel upgrade
sapcpe command
sap kernel 753
sap kernel upgrade testing

sap kernel latest version
linux kernel version history
linux kernel tutorial
linux kernel architecture
linux kernel book
linux kernel changelog
linux kernel pdf
linux kernel development
linux kernel source browser

command to find all of the files which have been accessed within the last 30 days in linux

Command to find all of the files which have been accessed within the last 30 days in linux


Question : command to find all of the files which have been accessed within the last 30 days?
Answer :find / -type f -atime -30 > December.files This command will find all the files under root, which is ‘/’, with file type is file. ‘-atime -30′ will give all the files accessed less than 30 days ago. And the output will put into a file call December.files.

Keywords:
how to find last 30 days files in linux
find files modified in last 2 days linux
linux find files modified in last hour
linux find files modified after a certain time
unix list files created between specific date and time
how to find files created on a specific date in unix
find files created in last 2 days
how to find files modified on a specific date in unix


What is LILO in Linux

What is LILO in linux?

Question :What is LILO in linux?
Answer :LILO stands for Linux boot loader. It will load the MBR, master boot record, into the memory, and tell the system which partition and hard drive to boot from.

Boot process:
When LILO loads itself, it displays the name LIO where each word specifies some actions
If it displays nothing then it does not load any part of LILO.
L: This is the first stage of the bootloader that has been loaded. If the process stops here it denotes that there were problems in the second stage. This may occur due to some incorrect disk parameter specified in the configuration file of lilo or some media problems also.
LI: It indicates that the second stage boot loader has been loaded and could not be executed. It can occur due to problems similar to L.
LIL: At this stage, the second stage boot loader has been completed in its execution. If it fails, this stage indicates that there were media problems or map file specified in the configuration file has some problems.
LIL?: This means that the second stage boot loader loaded at an incorrect address.
LIL-: This indicates that the descriptor table is corrupted.
LILO: All parts are successfully loaded.

Keywords:
installing lilo in linux
lilo in linux tutorialspoint
running lilo in linux
lilo boot loader download
lilo vs grub
grub in linux
lilo in linux in hindi
elilo linux


What is the main advantage of creating links to a file instead of copies of the file in linux

What is the main advantage of creating links to a file instead of copies of the file in linux?

Question :What is the main advantage of creating links to a file instead of copies of the file?
Answer :The main advantage is not really that it saves disk space (though it does that too) but, rather, that a change of permissions on the file is applied to all the link access points. The link will show permissions of lrwxrwxrwx but that is for the link itself and not the access to the file to which the link points. Thus if you want to change the permissions for a command, such as su, you only have to do it on the original. With copies you have to find all of the copies and change permission on each of the copies.
Keywords:
linux hard link
soft link linux
how to check hard link in linux
why use hard links linux
linux symbolic link explained
difference hard soft link linux
linux hard link directory example
how to identify soft link and hardlink
linux hard link directory
how to create hard link in linux
linux soft link directory
use of hard link in linux
linux hard link directory tree
linux hard link directory example
how to check hard link in linux
soft link in linux

What Happens when you execute a program in unix

What Happens when you execute a program in unix?

Question :What Happens when you execute a program in unix ?
Answer :When you execute a program on your UNIX system, the system creates a special environment for that program. This environment contains everything needed for the system to run the program as if no other program were running on the system. Each process has process context, which is everything that is unique about the state of the program you are currently running. Every time you execute a program the UNIX system does a fork, which performs a series of operations to create a process context and then execute your program in that context. The steps include the following: • Allocate a slot in the process table, a list of currently running programs kept by UNIX. • Assign a unique process identifier (PID) to the process. • iCopy the context of the parent, the process that requested the spawning of the new process. • Return the new PID to the parent process. This enables the parent process to examine or control the process directly. After the fork is complete, UNIX runs your program.
Keywords:
What happens when you execute a command in unix?
How do you execute a program in a command line?
What happens when you run a program?
How do you execute a program in a command line unix?
how to run c program in unix vi editor
unix commands
unix programming book
how to compile c program in unix
how to run a program in unix terminal
unix shell
bash shell scripting
unix programming language

What Happens when you execute a command in unix

Question :What Happens when you execute a command in unix ?
Answer :When you enter 'ls' command to look at the contents of your current working directory, UNIX does a series of things to create an environment for ls and the run it: The shell has UNIX perform a fork. This creates a new process that the shell will use to run the ls program. The shell has UNIX perform an exec of the ls program. This replaces the shell program and data with the program and data for ls and then starts running that new program. The ls program is loaded into the new process context, replacing the text and data of the shell. The ls program performs its task, listing the contents of the current directory.

What is a Daemon?

What is a Daemon?

Question :What is a Daemon?
Answer :A daemon is a process that detaches itself from the terminal and runs, disconnected, in the background, waiting for requests and responding to them. It can also be defined as the background process that does not belong to a terminal session. Many system functions are commonly performed by daemons, including the sendmail daemon, which handles mail, and the NNTP daemon, which handles USENET news. Many other daemons may exist. Some of the most common daemons are: • init: Takes over the basic running of the system when the kernel has finished the boot process. • inetd: Responsible for starting network services that do not have their own stand-alone daemons. For example, inetd usually takes care of incoming rlogin, telnet, and ftp connections. • cron: Responsible for running repetitive tasks on a regular schedule.

Keywords:


What is 'ps' command for?How would you kill a process?

What is 'ps' command for?How would you kill a process?

Question1 :What is 'ps' command for?
Answer :The ps command prints the process status for some or all of the running processes. The information given are the process identification number (PID),the amount of time that the process has taken to execute so far etc.

Question 2:How would you kill a process?
Answer :The kill command takes the PID as one argument; this identifies which process to terminate. The PID of a process can be got using 'ps' command. -9 is the option given

Keywords:
ps -ef command output
ps aux command in linux
ps -ef command explanation
ps -ef command example
ps command in unix
what is tty in ps command
kill command in linux
force kill process linux command line
kill process linux by name
linux kill process by pid
force kill process linux terminal
force kill process linux pid
linux kill all processes by user
kill program linux command line
kill all process in linux

What is an advantage of executing a process in background

What is an advantage of executing a process in background


Question :What is an advantage of executing a process in background?
Answer :The most common reason to put a process in the background is to allow you to do something else interactively without waiting for the process to complete. At the end of the command you add the special background symbol, &. This symbol tells your shell to execute the given command in the background. Example: cp *.* ../backup& (cp is for copy)
Keywords:
advantages and disadvantages of background process in linux
advantage of background process
background process in unix
a process that runs in the background without the need for user interaction is known as a
managing system services and background processes
processes in linux
interview questions on process management in linux
shell process in unix

How do you execute one program from within another in unix

How do you execute one program from within another in unix

Question :How do you execute one program from within anotherin unix?
Answer :The system calls used for low-level process creation are execlp() and execvp(). The execlp call overlays the existing program with the new one , runs that and exits. The original program gets back control only when an error occurs. execlp(path,file_name,arguments..); //last argument must be NULL A variant of execlp called execvp is used when the number of arguments is not known in advance. execvp(path,argument_array); //argument array should be terminated by NULL
Keywords:
return value from one shell script to another
how to pass variable from one function to another in shell script
bash function call another function
calling functions in shell script
unix function with argument
bash function run command
write function in unix
factorial program in shell script using function

What is IPC What are the various schemes available

What is IPC What are the various schemes available

Question :What is IPC? What are the various schemes available?
Answer :The term IPC (Inter-Process Communication) describes various ways by which different process running on some operating system communicate between each other. Various schemes available are as follows: Pipes: One-way communication scheme through which different process can communicate. The problem is that the two processes should have a common ancestor (parent-child relationship). However this problem was fixed with the introduction of named-pipes (FIFO). Message Queues : Message queues can be used between related and unrelated processes running on a machine. Shared Memory: This is the fastest of all IPC schemes. The memory to be shared is mapped into the address space of the processes (that are sharing). The speed achieved is attributed to the fact that there is no kernel involvement. But this scheme needs synchronization. Various forms of synchronisation are mutexes, condition-variables, read-write locks, record-locks, and semaphores

Keywords:
ipc between processes on different systems
inter process communication types
interprocess communication in distributed system

writing Solaris Device drivers tutorial

writing Solaris Device drivers tutorial

writing solaris device drivers

follow the below link to have a pdf to write device drivers for solaris
http://docs.google.com/fileview?id=0B-9-LUWuq8ZJMjQ1NTFmMGYtYTMxZi00M2Q0LTkyYzEtZGVjZTM4MmUxNmJl&hl=en

c macro faqs

C macro faqs

Question1: What is the difference between a string copy (strcpy) and a memory copy (memcpy)? When should each be used?
Answer: The strcpy() function is designed to work exclusively with strings. It copies each byte of the source string to the destination string and stops when the terminating null character () has been moved. On the other hand, the memcpy () function is designed to work with any type of data. Because not all data ends with a null character, you must provide the memcpy () function with the number of bytes you want to copy from the source to the destination.

Question2: How can you check to see whether a symbol is defined?
Answer: You can use the #ifdef and #ifndef preprocessor directives to check whether a symbol has been defined
(#ifdef) or whether it has not been defined (#ifndef).

Question3: How do you override a defined macro?
Answer: You can use the #undef preprocessor directive to undefine (override) a previously defined macro.

Question4: What is #line used for?
Answer: The #line preprocessor directive is used to reset the values of the _ _LINE_ _ and _ _FILE_ _ symbols,
respectively. This directive is commonly used in fourth-generation languages that generate C language source files.

Question5: What is a pragma?
Answer :The #pragma preprocessor directive allows each compiler to implement compiler-specific features that can be turned on and off with the #pragma statement. For instance, your compiler might support a feature called loop optimization. This feature can be invoked as a command-line option or as a #pragma directive.

To implement this option using the #pragma directive, you would put the following line into your code:

#pragma loop_opt(on)
Conversely, you can turn off loop optimization by inserting the following line into your code:

Keywords:

c interview questions
c++ faq
faq on pointers in c
faq on arrays in c
faq videos
c tutorial
faqs on strings in c
steve summit
faq on pointers in c
faq on arrays in c
c++ faq
programming in c textbook
c tutorial
c programming resources
c language reference
cinterviews.com
c macro function
c macro
types of macros in c
macros in c geeksforgeeks
macros in c tutorialspoint
c define macro function
function-like macros in c
macro substitution in c
difference between strcpy() and memcpy with example
strcpy vs memcpy performance
replace strcpy with memcpy
c copy string using memcpy
difference between memcpy and memmove
memcpy in c
memcpy implementation
strcpy vs strncpy
how to solve undefined symbol error in c++
programming symbols list
undefined reference to c
programming symbols names
linker error undefined reference to function in c
symbols in computer science
what is the meaning of symbol in c
undefined reference to variable in c++
how do you override a defined macro
override macro
c override macro
c redefine macro
c macro expansion
using macros in c
gcc macro expansion
c macro string
program to draw a line in c
line in c
draw a line in c without graphics
c program to draw a circle
program to draw a line in computer graphics in c++
line function in computer graphics syntax
line() in c
rectangle function in computer graphics
pragma in c geeksforgeeks
what is pragma in c language
pragma pack in c
pragma in embedded c
pragma c
pragma in c structure padding
pragma once
gcc pragma


c faqs on pointers

C faqs on pointers

1)What are pointers really good for, anyway?
2)I'm trying to declare a pointer and allocate some space for it, but it's not working. What's wrong with this code?
char *p;
*p = malloc(10);
3)Does *p++ increment p, or what it points to?
4)I'm trying to use pointers to manipulate an array of ints. What's wrong with this code?
int array[5], i, *ip;
for(i = 0; i < 5; i++) array[i] = i;
ip = array;
printf("%d\n", *(ip + 3 * sizeof(int)));
I expected the last line to print 3, but it printed garbage.
5)I have a char * pointer that happens to point to some ints, and I want to step it over them. Why doesn't  ((int *)p)++;  work?
6)Why can't I perform arithmetic on a void * pointer?
7)I've got some code that's trying to unpack external structures, but it's crashing with a message about an ``unaligned access.'' What does this mean?
8)I have a function which accepts, and is supposed to initialize, a pointer:
void f(int *ip)
{
static int dummy = 5;
ip = &dummy;
}
But when I call it like this:
int *ip;
f(ip);
the pointer in the caller remains unchanged.
9)Suppose I want to write a function that takes a generic pointer as an argument and I want to simulate passing it by reference. Can I give the formal parameter type void **, and do something like this?
void f(void **);
double *dp;
f((void **)&dp);
10)I have a function extern int f(int *);
which accepts a pointer to an int. How can I pass a constant by reference? A call like

f(&5);
doesn't seem to work.
11)Does C even have ``pass by reference''?
12)I've seen different syntax used for calling functions via pointers. What's the story?
13)What's the total generic pointer type? My compiler complained when I tried to stuff function pointers into a void *.
14)How are integers converted to and from pointers? Can I temporarily stuff an integer into a pointer, or vice versa?
15)How do I convert an int to a char *? I tried a cast, but it's not working.
16)What's wrong with this declaration?
char* p1, p2;
I get errors when I try to use p2.
17)What are ``near'' and ``far'' pointers?
keywords:
c interview questions
c++ faq
faq on pointers in c
faq on arrays in c
faq videos
c tutorial
faqs on strings in c
steve summit
faq on pointers in c
faq on arrays in c
c++ faq
programming in c textbook
c tutorial
c programming resources
c language reference
cinterviews.com
use of pointers in c
pointer in c example
pointers in c pdf
use of pointers in c++
types of pointers in c
pointers in c geeks for geeks
double pointers in c
what is pointer in c
advantages of pointers in c
pointers to manipulate an array of ints
arithmetic operations on void pointers
void pointer function
void pointer in c++
pointer arithmetic
cast void pointer to int
void*' is not a pointer-to-object type
how to print void pointer value in c
generic pointer in c
(void *) in c
convert pointer to integer c++
how to convert pointer into integer
int to pointer cast warning
integer to pointer casting
c++ get pointer as integer
conversion from pointer to smaller integer
how to convert char pointer to integer in c
uintptr_t

c faqs

C faqs

Question1: Difference between arrays and pointers?
 Answer: Pointers are used to manipulate data using the address. Pointers use * operator to access the data pointed to by them
 Arrays use subscripted variables to access and manipulate data. Array variables can be equivalently written using pointer expression.

Question2: What is the purpose of realloc ( )?
 Answer: The function realloc (ptr,n) uses two arguments. The first argument ptr is a pointer to a block of memory for which the size is to be altered. The second argument n specifies the
new size. The size may be increased or decreased. If n is greater than the old size and if sufficient space is not available subsequent to the old region, the function realloc ( )
may create a new region and all the old data are moved to the new region.

Question3: What is static memory allocation and dynamic memory allocation?
Answer: Static memory allocation: The compiler allocates the required memory space for a declared variable. By using the address of operator, the reserved address is obtained and this address may be assigned to a pointer variable. Since most of the declared variable has static memory, this way of assigning pointer value to a pointer variable is known as static memory allocation. Memory is assigned during compilation time.

Dynamic memory allocation: It uses functions such as malloc ( ) or calloc ( ) to get memory dynamically. If these functions are used to get memory dynamically and the values returned by these functions are assigned to pointer variables, such assignments are known as dynamic memory allocation. Memory is assigned during run time.

Question4: How are pointer variables initialized?
Answer: Pointer variable are initialized by one of the following two ways
              Ø Static memory allocation
              Ø Dynamic memory allocation

Question5: What is a pointer variable?
Answer: A pointer variable is a variable that may contain the address of another variable or any valid address in the memory.

Question6: What is a pointer value and address?
Answer: A pointer value is a data object that refers to a memory location. Each memory location is numbered in the memory. The number attached to a memory location is called the address of the location.
Keywords:
difference between arrays and pointers
purpose of realloc in c
static memory allocation and dynamic memory allocation in c
pointer variable initialization in c
pointer variable in c
what is a pointer value and address in c
difference between array and pointer in c in tabular form
differentiate between array of pointers and pointers to array
relation between array and pointer in c
difference between array and pointer in c in hindi
array of pointers in c
difference between array and pointer in hindi
which of arrays or pointers are faster
difference between array and structure
static memory allocation in c geeksforgeeks
difference between static and dynamic memory allocation? - quora
advantages and disadvantages of static and dynamic memory allocation
dynamic memory allocation in c++
static memory allocation in c++ with example program
when is memory allocated and deallocated in c? answer both static and dynamic memory.
static and dynamic memory in computer organization
dynamic memory allocation in c pdf
declaration and initialization in c
how to initialize a pointer c++
accessing a variable through its pointer
pointer arithmetic in c
pointer to pointer in c
pointer and array in c
pointer operator in c
pointer assignment in c
pointer in c example
pointers in c pdf
use of pointers in c
double pointers in c
types of pointers in c
pointers in c geeks for geeks
list of c programs on pointers
pointers and arrays in c
use of pointers in c
pointers in c geeks for geeks
list of c programs on pointers
what is pointer in c++
pointers in c pdf
pointer in c example
types of pointers in c
double pointers in c

c faqs question and answers

C faqs question and answers

C interview question1:How do I write code that executes certain function only at program termination?
Answer: Use atexit( ) function as shown in following program.
#include
main( )
{
int ch ;
void fun ( void ) ;
atexit ( fun ) ;
// code
}
void fun( void )
{
printf ( "\nTerminate program......" ) ;
getch( ) ;
}
C interview question2:What are memory models?
Answer: The compiler uses a memory model to determine how much memory is allocated to the program. The PC divides memory into blocks called segments of size 64 KB. Usually, program uses one segment for code and a second segment for data. A memory model defines the number of segments the compiler can use for each. It is important to know which memory model can be used for a program. If we use wrong memory model, the program might not have enough memory to execute. The problem can be solved using larger memory model. However, larger the memory model, slower is your program execution. So we must choose the smallest memory model that satisfies our program needs. Most of the compilers support memory models like tiny, small, medium, compact, large and huge.
C interview question3:How does C compiler store elements in a multi-dimensional array?
Answer:The compiler maps multi-dimensional arrays in two ways—Row major order and Column order. When the compiler places elements in columns of an array first then it is called column-major order. When the compiler places elements in rows of an array first then it is called row-major order. C compilers store multidimensional arrays in row-major order. For example, if there is a multi-dimensional array a[2][3], then according row-major order, the elements would get stored in memory following order:
a[0][0], a[0][1], a[0][2], a[1][0], a[1][1], a[1][2]
C interview question4:If the result of an _expression has to be stored to one of two variables, depending on a condition, can we use conditional operators as shown below?
( ( i < 10 ) ? j : k ) = l * 2 + p ;
Answer: No! The above statement is invalid. We cannot use the conditional operators in this fashion. The conditional operators like most operators, yields a value, and we cannot assign the value of an _expression to a value. However, we can use conditional operators as shown in following code snippet.
main( )
{
int i, j, k, l ;
i = 5 ; j = 10 ; k = 12, l = 1 ;
* ( ( i < 10 ) ? &j : &k ) = l * 2 + 14 ;
printf ( "i = %d j = %d k = %d l = %d", i, j, k, l ) ;
}
The output of the above program would be as given below:
i = 5 j = 16 k = 12 l = 1
C interview question5:How can I find the day of the week of a given date?
Answer: The following code snippet shows how to get the day of week from the given date. 
dayofweek ( int yy, int mm, int dd )
{
/*Monday = 1 and Sunday = 0 */
/* month number >= 1 and <= 12, yy > 1752 or so */
static int arr[ ] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 } ;
yy = yy - mm < 3 ;
return ( yy + yy / 4 - yy / 100 + yy / 400 + arr[ mm - 1] + dd ) % 7 ;
}
void main( )
{
printf ( "\n\n\nDay of week : %d ", dayofweek ( 2002, 5, 18 ) ) ;
}
C interview question6: What's the difference between these two declarations?
struct str1 { ... } ;
typedef struct { ... } str2 ;
Answer : The first form declares a structure tag whereas the second declares a typedef. The main difference is that the second declaration is of a slightly more abstract type -- its users don't necessarily know that it is a structure, and the keyword struct is not used when declaring instances of it.
C interview question7: How do I print the contents of environment variables?
Answer: The following program shows how to achieve this:
main( int argc, char *argv[ ], char *env[ ] )
{
int i = 0 ;
clrscr( ) ;
while ( env[ i ] )
printf ( "\n%s", env[ i++ ] ) ;
}
main( ) has the third command line argument env, which is an array of pointers to the strings. Each pointer points to an environment variable from the list of environment variables.
keywords:
memory models in embedded c
data memory in microcontroller
memory models in c
ram in embedded system
composing memory in embedded system
memory mapping in 8051
external memory in embedded system
memory organization of 8051
data in embedded c
program termination in c
how to end a program in c
exit(0) in c
c exit codes
exit() function in c example
how to exit c program output screen
exit(-1) in c
c quit
explain exit statement in c with example
multi dimensional array in c
two dimensional array in c definition
three dimensional array in c
two dimensional array in c++
two dimensional array in c using pointers
two dimensional array in c pdf
two dimensional string array in c
sum of elements in 2d array in c
if statement
logical operators
if statement python
boolean expression
conditional statement
if statement logic
if else statement
python if greater than or equal to
conditional operator c#
conditional operator java
conditional operator javascript
conditional operator c++
conditional operator in c
list operators used in if conditional statement in python
ternary operator
list operators used in if conditional statement in java
conditional operator in python



c answers

C answers

C interview question1: What is a stack ?
Answer: The stack is a region of memory within which our programs temporarily store data as they execute. For example, when a program passes parameters to functions, C places the parameters on the stack. When the function completes, C removes the items from the stack. Similarly, when a function declares local variables, C stores the variable's values on the stack during the function's execution. Depending on the program's use of functions and parameters, the amount of stack space that a program requires will differ.

C interview question2:Allocating memory for a 3-D array
Answer: #include "alloc.h"
#define MAXX 3
#define MAXY 4
#define MAXZ 5
main( )
{
int ***p, i, j, k ;
p = ( int *** ) malloc ( MAXX * sizeof ( int ** ) ) ;
for ( i = 0 ; i < MAXX ; i++ )
{
p[i] = ( int ** ) malloc ( MAXY * sizeof ( int * ) ) ;
for ( j = 0 ; j < MAXY ; j++ )
p[i][j] = ( int * ) malloc ( MAXZ * sizeof ( int ) ) ;
}
for ( k = 0 ; k < MAXZ ; k++ )
{
for ( i = 0 ; i < MAXX ; i++ )
{
for ( j = 0 ; j < MAXY ; j++ )
{
p[i][j][k] = i + j + k ;
printf ( "%d ", p[i][j][k] ) ;
}
printf ( "\n" ) ;
}
printf ( "\n\n" ) ;
}
}

C interview question3:How to distinguish between a binary tree and a tree?
Answer: A node in a tree can have any number of branches. While a binary tree is a tree structure in which any node can have at most two branches. For binary trees we distinguish between the subtree on the left and subtree on the right, whereas for trees the order of the subtrees is irrelevant.

Consider two binary trees, but these binary trees are different. The first has an empty right subtree while the second has an empty left subtree. If the above are regarded as trees (not the binary trees), then they are same despite the fact that they are drawn differently. Also, an empty binary tree can exist, but there is no tree having zero nodes.

C interview question4:How do I use the function ldexp( ) in a program?
Answer: The math function ldexp( ) is used while solving the complex mathematical equations. This function takes two arguments, a double value and an int respectively. The order in which ldexp( ) function performs calculations is ( n * pow ( 2, exp ) ) where n is the double value and exp is the integer. The following program demonstrates the use of this function.
#include
void main( )
{
double ans ;
double n = 4 ;
ans = ldexp ( n, 2 ) ;
printf ( "\nThe ldexp value is : %lf\n", ans ) ;
}
Here, ldexp( ) function would get expanded as ( 4 * 2 * 2 ), and the output would be the ldexp value is : 16.000000

C interview question5:Can we get the mantissa and exponent form of a given number?
Answer:The function frexp( ) splits the given number into a mantissa and exponent form. The function takes two arguments, the number to be converted as a double value and an int to store the exponent form. The function returns the mantissa part as a double value. Following example demonstrates the use of this function.
#include
void main( )
{
double mantissa, number ;
int exponent ;
number = 8.0 ;
mantissa = frexp ( number, &exponent ) ;
printf ( "The number %lf is ", number ) ;
printf ( "%lf times two to the ", mantissa ) ;
printf ( "power of %d\n", exponent ) ;
return 0 ;
}
Keywords:
c interview questions
c++ faq
faq on pointers in c
faq on arrays in c
faq videos
c tutorial
faqs on strings in c
steve summit
faq on pointers in c
faq on arrays in c
c++ faq
programming in c textbook
c tutorial
c programming resources
c language reference
cinterviews.com

c faq questions

C faq questions

C interview question1:If we have declared an array as global in one file and we are using it in another file then why doesn't the sizeof operator works on an extern array?
Answer: An extern array is of incomplete type as it does not contain the size. Hence we cannot use sizeof operator, as it cannot get the size of the array declared in another file. To resolve this use any of one the following two solutions:
1. In the same file declare one more variable that holds the size of array. For example,
array.c
int arr[5] ;
int arrsz = sizeof ( arr ) ;
myprog.c
extern int arr[] ;
extern int arrsz ;
2. Define a macro which can be used in an array
declaration. For example,
myheader.h
#define SZ 5
array.c
#include "myheader.h"
int arr[SZ] ;
myprog.c
#include "myheader.h"
extern int arr[SZ] ;

C interview question2:
How do I write printf( ) so that the width of a field can be specified at runtime?
Answer: This is shown in following code snippet.
main( )
{
int w, no ;
printf ( "Enter number and the width for the
number field:" ) ;
scanf ( "%d%d", &no, &w ) ;
printf ( "%*d", w, no ) ;
}

Here, an '*' in the format specifier in printf( ) indicates that an int value from the argument list should be used for the field width.

C interview question3:How to find the row and column dimension of a given 2-D array?
Answer: Whenever we initialize a 2-D array at the same place where it has been declared, it is not necessary to mention the row dimension of an array. The row and column dimensions of such an array can be determined programmatically as shown in following program.
void main( )
{
int a[][3] = { 0, 1, 2,9,-6, 8,7, 5, 44,23, 11,15 } ;
int c = sizeof ( a[0] ) / sizeof ( int ) ;
int r = ( sizeof ( a ) / sizeof ( int ) ) / c ;
int i, j ;
printf ( "\nRow: %d\nCol: %d\n", r, c ) ;
for ( i = 0 ; i < r ; i++ )
{
for ( j = 0 ; j < c ; j++ )
printf ( "%d ", a[i][j] ) ;
printf ( "\n" ) ;
}
}
C interview quesion4:What is access( ) function...
Answer:The access( ) function checks for the existence of a file and also determines whether it can be read,written to or executed. This function takes two arguments the filename and an integer indicating the access mode. The values 6, 4, 2, and 1 checks for read/write, read, write and execute permission of a given file, whereas value 0 checks whether the file exists or not. Following program demonstrates how we can use access( ) function to check if a given file exists.
#include
main( )
{
char fname[67] ;
printf ( "\nEnter name of file to open" ) ;
gets ( fname ) ;
if ( access ( fname, 0 ) != 0 )
{
printf ( "\nFile does not exist." ) ;
return ;
}
}
C interview question5:How do I convert a floating-point number to a string?
Answer: Use function gcvt( ) to convert a floating-point number to a string. Following program demonstrates the use of this function. #include main( )
{
char str[25] ;
float no ;
int dg = 5 ; /* significant digits */
no = 14.3216 ;
gcvt ( no, dg, str ) ;
printf ( "String: %s\n", str ) ;
}
Keywords:
c interview questions
c++ faq
faq on pointers in c
faq on arrays in c
faq videos
c tutorial
faqs on strings in c
steve summit
faq on pointers in c
faq on arrays in c
c++ faq
programming in c textbook
c tutorial
c programming resources
c language reference
cinterviews.com

c interview answers

C interview answers

C interview question1: What is atexit() ?
Answer:Function atexit( ) recevies parameter as the address of function of the type void fun ( void ). The function whose address is passed to atexit( ) gets called before the termination of program. If atexit( ) is called for more than one function then the functions are called in "first in last out" order. You can verify that from the output.


#include
void fun1( )

{

printf("Inside fun1\n");

}

void fun2( )

{

printf("Inside fun2\n");

}

main( )

{

atexit ( fun1 ) ;

/* some code */
atexit ( fun2 ) ;
printf ( "This is the last statement of
program?\n" );
}

C interview question2 How do I write a user-defined function, which deletes each character in a string str1, which matches any character in string str2?
Answer: The function is as shown below:

Compress ( char str1[], char str2[] )
{
int i, j, k ;
for ( i = k = 0 ; str1[i] != ‘\0’ ; i++ )
{
for ( j = 0 ; str2[j] != ‘\0’ && str2[j] !=
str1[i] ; j++ );
if ( str2[j] == ‘\0’ )
str1[k++] = str1[I] ;
}
str1[k] = ‘\0’
}

C interview question3:How does free( ) know how many bytes to free?
Answer: The malloc( ) / free( ) implementation remembers the size of each block allocated and returned, so it is not necessary to remind it of the size when freeing.

C interview question4:What is the use of randomize( ) and srand( ) function?
Answer: While generating random numbers in a program, sometimes we require to control the series of numbers that random number generator creates. The process of assigning the random number generators starting number is called seeding the generator. The randomize( ) and srand( ) functions are used to seed the random number generators. The randomize( ) function uses PC's clock to produce a random seed, whereas the srand( ) function allows us to specify the random number generator's starting value.

C interview question5:How do I determine amount of memory currently available for allocating?
Answer: We can use function coreleft( ) to get the amount of memory available for allocation. However, this function does not give an exact amount of unused memory. If, we are using a small memory model, coreleft( ) returns the amount of unused memory between the top of the heap and stack. If we are using a larger model, this function returns the amount of memory between the highest allocated memory and the end of conventional memory. The function returns amount of memory in terms of bytes.

C interview question6:How does a C program come to know about command line arguments?
Answer: When we execute our C program, operating system loads the program into memory. In case of DOS, it first loads 256 bytes into memory, called program segment prefix. This contains file tables,environment segment, and command line information. When we compile the C program the compiler inserts additional code that parses the command, assigning it to the argv array, making the arguments easily accessible within our C program.

C interview question7:When we open a file, how does functions like fread( )/fwrite( ), etc. get to know from where to read or to write the data?
Answer: When we open a file for read/write operation using function like fopen( ), it returns a pointer to the structure of type FILE. This structure stores the file pointer called position pointer, which keeps track of current location within the file. On opening file for read/write operation, the file pointer is set to the start of the file. Each time we read/write a character, the position pointer advances one character. If we read one line of text at a step from the file, then file pointer advances to the start of the next line. If the file is opened in append mode, the file pointer is placed at the very end of the file. Using fseek( ) function we can set the file pointer to some other place within the file.

Keywords:

c interview questions
c++ faq
faq on pointers in c
faq on arrays in c
faq videos
c tutorial
faqs on strings in c
steve summit
faq on pointers in c
faq on arrays in c
c++ faq
programming in c textbook
c tutorial
c programming resources
c language reference

c frequently asked questions

C frequently asked questions

C interview question:The sizeof( ) function doesn’t return the size of the block of memory pointed to by a pointer. Why?

Answer:The sizeof( ) operator does not know that malloc( ) has been used to allocate a pointer. sizeof( ) gives us the size of pointer itself. There is no handy way to find out the size of a block allocated by malloc( ).


C interview qustion: What is FP_SEG And FP_OFF…
Answer:Sometimes while working with far pointers we need to break a far address into its segment and offset. In such situations we can use FP_SEG and FP_OFF macros. Following program illustrates the use of these two macros.
#include
main( )
{
unsigned s, o ;
char far *ptr = "Hello!" ;
s = FP_SEG ( ptr ) ;
o = FP_OFF ( ptr ) ;
printf ( "\n%u %u", s, o ) ;
}

C interview question:How do I write a program to convert a string containing number in a hexadecimal form to its equivalent decimal?
Answer: The following program demonstrates this:
main( )
{
char str[] = "0AB" ;
int h, hex, i, n ;
n = 0 ; h = 1 ;
for ( i = 0 ; h == 1 ; i++ )
{
if ( str[i] >= '0' && str[i] <= '9' )
hex = str[i] - '0' ;
else
{
if ( str[i] >= 'a' && str[i] <= 'f' )
hex = str[i] - 'a' + 10 ;
else
if ( str[i] >= 'A' && str[i] <= 'F' )
hex = str[i] - 'A' + 10 ;
else
h = 0 ;
}
if ( h == 1 )
n = 16 * n + hex ;
}
printf ( "\nThe decimal equivalent of %s is %d",
str, n ) ;
}
The output of this program would be the decimal equivalent of 0AB is 171.

C interview question:How do I write code that reads the segment register settings?
Answer: We can use segread( ) function to read segment register settings. There are four segment registers—code segment, data segment, stack segment and extra segment. Sometimes when we use DOS and BIOS services in a program we need to know the segment register's value. In such a situation we can use segread( ) function. The following program illustrates the use of this function.
#include
main( )
{
struct SREGS s ;
segread ( &s ) ;
printf ( "\nCS: %X DS: %X SS: %X ES: %X",s.cs,
s.ds, s.ss, s.es ) ;
}

C interview question:What is environment and how do I get environment for a specific entry?
Answer: While working in DOS, it stores information in a memory region called environment. In this region we can place configuration settings such as command path, system prompt, etc. Sometimes in a program we need to access the information contained in environment. The function getenv( ) can be used when we want to access environment for a specific entry. Following program demonstrates the use of this function.

#include
main( )
{
char *path = NULL ;
path = getenv ( "PATH" ) ;
if ( *path != NULL )
printf ( "\nPath: %s", path ) ;
else
printf ( "\nPath is not set" ) ;
}

C interview question:How do I display current date in the format given below?
Saturday October 12, 2002
Answer: Following program illustrates how we can display date in above given format.

#include
main( )
{
struct tm *curtime ;
time_t dtime ;
char str[30] ;
time ( &dtime ) ;
curtime = localtime ( &dtime ) ;
strftime ( str, 30, "%A %B %d, %Y", curtime ) ;
printf ( "\n%s", str ) ;
}

Here we have called time( ) function which returns current time. This time is returned in terms of seconds, elapsed since 00:00:00 GMT, January 1, 1970. To extract the week day, day of month, etc.from this value we need to break down the value to a tm structure. This is done by the function localtime( ). Then we have called strftime( ) function to format the time and store it in a string str.