Sei sulla pagina 1di 17

C Test:

1. How will u print TATA alone from TATA POWER using string copy and
concate commands in C?
2. If switch(n)
case 1:printf("CASE !");
case(2):printf("default");
break;
What will be printed?

9. How will u divide two numbers in a MACRO?

int a,b;
1.main()
2.{
3.scanf("............",&a,&b);
4.if...........{
5.printf("Print A");
6.else
7.Printf("...........");
8.endif}
9..........
A.What will come in the 9 dash?
B.What will happen if we replace Print A as Print X?
C.What will come in the 3 dash?
D.What will happen if we interchange 4 and 7?
E.What will come in the 4 dash?

What is the output ?


main()
{
int i=5;
if(i>5);
{
i=100;
printf(" %d ",i);
}
}
a. 100

Explain pointers in simple terminology as if I dont know C ??

11. What are the uses of pointers ?

12. Different parameter passing methods and their explanations ??


13. Parameter passing in C ??

14. Is C a block structured language ?

15. What do U mean by block structure ??

16. How many levels of nesting are possible in C ?

17. Difference between the pointers in C & Pascal ??

18. Difference between local and global variable ?? Differences in how


they are stored ?

19. Can a function call another ? Until what level ? How do the calls
occur ?

1. What will print out?

main()
{
char *p1=“name”;
char *p2;
p2=(char*)malloc(20);
memset (p2, 0, 20);
while(*p2++ = *p1++);
printf(“%sn”,p2);

Answer:empty string.

2. What will be printed as the result of the operation below:

main()
{
int x=20,y=35;
x=y++ + x++;
y= ++y + ++x;
printf(“%d%dn”,x,y);

Answer : 5794

3. What will be printed as the result of the operation below:


main()
{
int x=5;
printf(“%d,%d,%dn”,x,x< <2,x>>2);

Answer: 5,20,1

4. What will be printed as the result of the operation below:

#define swap(a,b) a=a+b;b=a-b;a=a-b;

void main()
{
int x=5, y=10;
swap (x,y);
printf(“%d %dn”,x,y);
swap2(x,y);
printf(“%d %dn”,x,y);
}

int swap2(int a, int b)


{
int temp;
temp=a;
b=a;
a=temp;
return 0;

Answer: 10, 5
10, 5

5. What will be printed as the result of the operation below:

main()
{
char *ptr = ” Cisco Systems”;
*ptr++; printf(“%sn”,ptr);
ptr++;
printf(“%sn”,ptr);

}
Answer:Cisco Systems
isco systems

6. What will be printed as the result of the operation below:

main()
{
char s1[]=“Cisco”;
char s2[]= “systems”;
printf(“%s”,s1);
}

Answer: Cisco

7. What will be printed as the result of the operation below:

main()
{
char *p1;
char *p2;

p1=(char *)malloc(25);
p2=(char *)malloc(25);

strcpy(p1,”Cisco”);
strcpy(p2,“systems”);
strcat(p1,p2);

printf(“%s”,p1);

Answer: Ciscosystems

8. The following variable is available in file1.c, who can access it?:


9. static int average;

Answer: all the functions in the file1.c can access the variable.

10. WHat will be the result of the following code?

#define TRUE 0 // some code

while(TRUE)
{
// some code

Answer: This will not go into the loop as TRUE is defined as 0.

11. What will be printed as the result of the operation below:

int x;
int modifyvalue()
{
return(x+=10);
}

int changevalue(int x)
{
return(x+=1);
}

void main()
{
int x=10;
x++;
changevalue(x);
x++;
modifyvalue();
printf("First output:%dn",x);

x++;
changevalue(x);
printf("Second output:%dn",x);
modifyvalue();
printf("Third output:%dn",x);

Answer: 12 , 13 , 13

12. What will be printed as the result of the operation below:

main()
{
int x=10, y=15;
x = x++;
y = ++y;
printf(“%d %dn”,x,y);
}

Answer: 11, 16

13. What will be printed as the result of the operation below:

main()
{
int a=0;
if(a==0)
printf(“Cisco Systemsn”);
printf(“Cisco Systemsn”);

Answer: Two lines with “Cisco Systems” will be printed.

Difference between arrays and pointers in


C
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.

Pointers are not integers. A pointer is an address. It is merely a positive number and not
an integer.

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

he main difference lies in the pattern of sequence...In array each block hold only the
value whereas in pointer each block holds the value as well as the address of the next
block.. This address of the next block is not present in array..Thus arrays are generally
serialwise eg: A[0],A[1],A[2],... where A is the array head.. whereas pointers are
generally arranged in hapazad way in memory unit...
Null and void pointers in C?

There is 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

A void pointer is a C convention for “a raw address.” The compiler has no idea what type
of object a void Pointer “really points to.” If you write int *ip; ip points to an int. If you
write void *p; p doesn’t point to a void!
In C and C++, any time you need a void pointer, you can use another pointer type. For
example, if you have a char*, you can pass it to a function that expects a void*. You don’t
even need to cast it. In C (but not in C++), you can use a void* any time you need any
kind of pointer, without casting. (In C++, you need to cast it). A void pointer is used for
working with raw memory or for passing a pointer to an unspecified type. Some C code
operates on raw memory. When C was first invented, character pointers (char *) were
used for that. Then people started getting confused about when a character pointer was a
string, when it was a character array, and when it was raw memory.

ENUMS

Enums are found in many Programming Languages:

Short for enumeration, an enum variable type can be found in C (Ansi, not the original
K&R), C++ and C#. The idea is that instead of using an int to represent a set of values, a
type with a restricted set of values in used instead.

For example if we use the colors of the rainbow, which are

1. Red
2. Orange
3. Yellow
4. Green
5. Blue
6. Indigo
7. Violet

If enums didn't exist, you might use a #define (in C) or const in C++/C# to specify these
values. Eg
#define red 1
#define orange 2

const int red=1;

 Find out more about What is a Programming Language?

Too many Ints to Count!:

The problem with this is that there are many more ints than colors. If violet has the value
7, and the program assigns a value of 15 to a variable then it is clearly a bug but might
not be detected as 15 is a valid value for an int.

Enums to the Rescue


An enum is a user-defined type consisting of a set of named constants called enumerators.
The colors of the rainbow would be mapped like this.:
enum rainbowcolors {
red,
orange,
yellow,
green,
blue,
indigo,
violet)
}

Now internally, the compiler will use an int to hold these and if no values are supplied,
red will be 0, orange is 1 etc.

What is the benefit of using an enum rather than a #define constant?


The use of an enumeration constant (enum) has many advantages over using the
traditional symbolic constant
style of #define. These advantages include a lower maintenance requirement, improved
program readability,
and better debugging capability.

1) The first advantage is that enumerated constants are generated automatically by the
compiler. Conversely, symbolic constants must be manually assigned values by the
programmer.
For instance, if you had an enumerated constant type for error codes that could occur in
your program, your
enum definition could look something like this:
enum Error_Code
{
OUT_OF_MEMORY,
INSUFFICIENT_DISK_SPACE,
LOGIC_ERROR,
FILE_NOT_FOUND
};
In the preceding example, OUT_OF_MEMORY is automatically assigned the value of 0
(zero) by the compiler
because it appears first in the definition. The compiler then continues to automatically
assign numbers to
the enumerated constants, making INSUFFICIENT_DISK_SPACE equal to 1,
LOGIC_ERROR equal to 2, and FILE_NOT_FOUND equal to 3, so on.
If you were to approach the same example by using symbolic constants, your code would
look something like this:
#define OUT_OF_MEMORY 0
#define INSUFFICIENT_DISK_SPACE 1
#define LOGIC_ERROR 2
#define FILE_NOT_FOUND 3
values by the programmer. Each of the two methods arrives at the same result: four
constants assigned numeric values to represent error codes. Consider the maintenance
required, however, if you were to add two constants to represent the error codes
DRIVE_NOT_READY and CORRUPT_FILE. Using the enumeration constant method,
you simply would put these two constants anywhere in the enum definition. The compiler
would generate two unique values for these constants. Using the symbolic constant
method, you would have to manually assign two new numbers to these constants.
Additionally, you would want to ensure that the numbers you assign to these constants
are unique.
2) Another advantage of using the enumeration constant method is that your programs are
more readable and
thus can be understood better by others who might have to update your program later.
3) A third advantage to using enumeration constants is that some symbolic debuggers can
print the value of an
enumeration constant. Conversely, most symbolic debuggers cannot print the value of a
symbolic constant.
This can be an enormous help in debugging your program, because if your program is
stopped at a line that
uses an enum, you can simply inspect that constant and instantly know its value. On the
other hand, because
most debuggers cannot print #define values, you would most likely have to search for
that value by manually
looking it up in a header file.

What is a macro, and how do you use it?


A macro is a preprocessor directive that provides a mechanism for token replacement in
your source code. Macros are created by using the #define statement.

Here is an example of a macro: Macros can also utilize special operators such as the
stringizing operator (#) and the concatenation operator (##).The stringizing operator can
be used to convert macro parameters to quoted strings, as in the following example:

#define DEBUG_VALUE(v) printf(#v ? is equal to %d.n?, v)

In your program, you can check the value of a variable by invoking the DEBUG_VALUE
macro:

...
int x = 20;
DEBUG_VALUE(x);
...

The preceding code prints ?x is equal to 20.? on-screen. This example shows that the
stringizing operator used with macros can be a very handy debugging tool.

What is the difference between far and near?

Some compilers for PC compatibles use two types of pointers.


near pointers are 16 bits long and can address a 64KB range. far pointers are 32 bits long
and can address a 1MB range.

Near pointers operate within a 64KB segment. There?s one segment for function
addresses and one segment for data. far pointers have a 16-bit base (the segment address)
and a 16-bit offset. The base is multiplied by 16, so a far pointer is effectively 20 bits
long. Before you compile your code, you must tell the compiler which memory model to
use. If you use a smallcode memory model, near pointers are used by default for function
addresses.

That means that all the functions need to fit in one 64KB segment. With a large-code
model, the default is to use far function addresses. You?ll get near pointers with a small
data model, and far pointers with a large data model. These are just the defaults; you can
declare variables and functions as explicitly near or far.

far pointers are a little slower. Whenever one is used, the code or data segment register
needs to be swapped out. far pointers also have odd semantics for arithmetic and
comparison. For example, the two far pointers in the preceding example point to the same
address, but they would compare as different! If your program fits in a small-data, small-
code memory model, your life will be easier.

The pointer which can point or access whole the residence memory of RAM i.e. which
can access all 16 segments is known as far pointer.The size of far pointer is 4 bytes.

The pointer which can points only 64KB data segment or segment number 8 is known as
near pointer.The size of near pointer is 2 bytes

Difference between :- 1) NULL pointer and NULL macro ?

Both are very different.


NULL macro is
#define NULL 0
it means the macro NULL will be replaced by 0 while preprocessing
But the NULL pointer means it points to nowhere i.e. contains 0.
It contains 0 means it may be dangerous to use such pointer without assigning proper
address to it otherwise NULL pointer may try to access reset address may cause the
program to crash.

What does it mean when a pointer is used in an if statement?

Any time a pointer is used as a condition, it means ?Is this a non-null pointer?? A pointer
can be used in an if, while, for, or do/while statement, or in a conditional expression.

A pointer in an if statement is normal, and may have many meanings, not just limited to
non-null. It may mean
- whether two pointers are the same;
- whether the pointer pointed value is a special value, like if((*p)==1);
- if the pointer is char *, more string related function calling may appeared, like
if(strcmp(a,b))

What is :- pointer , NULL pointer , dangling pointer , far pointer , near pointer ,
huge pointer , generic pointer , smart pointer ?

A null pointer has a reserved value, often but not necessarily the value zero, indicating
that it refers to no object.
In most C programming environments malloc returns a null pointer if it is unable to
allocate the memory region requested, which notifies the caller that there is insufficient
memory available.

Dangling pointers and wild pointers in computer programming are pointers that do not
point to a valid object of the appropriate type. Dangling pointers arise when an object is
deleted or deallocated, without modifying the value of the pointer, so that the pointer still
points to the memory location of the deallocated memory. As the system may reallocate
the previously freed memory to another process, if the original program then dereferences
the (now) dangling pointer, unpredictable behavior may result, as the memory may now
contain completely different data. This is especially the case if the program writes data to
memory pointed by a dangling pointer, a silent corruption of unrelated data may result,
leading to subtle bugs that can be extremely difficult to find, or cause segmentation
faults.

{
char *cp = NULL;
{
char c;
cp = &c;
} /* c falls out of scope */
/* cp is now a dangling pointer */
}
Solution to dangling pointer:
char *cp = malloc ( A_CONST );
/* ... */
free ( cp ); /* cp now becomes a dangling pointer */
cp = NULL; /* cp is no longer dangling */

far pointer is a pointer which includes a segment selector, making it possible to point to
addresses outside of the current segment. Far pointers have a size of 4 bytes. They store
both the segment and the offset of the address the pointer is referencing. A far pointer has
an address range of 0 - 1M bytes. A far pointer can be incremented and decremented
using arithmetic operators. When a far pointer is incremented or decremented ONLY the
offset of the pointer is actually incremented or decremented. The segment is never
incremented by the arithmetic operators.
Near pointers have a size of 2 bytes. They only store the offset of the address the pointer
is referencing. An address consisting of only an offset has a range of 0 - 64K bytes. A
near pointer can be incremented and decremented using arithmetic operators (+, -, ++,
and --) through the entire address range. Any attempt to increment a near pointer that has
a value of 64K (0xffff) will result in a value of 0.

HUGE pointers have a size of 4 bytes. They store both the segment and the offset of the
address the pointer is referencing. A huge pointer has an address range of 0 - 1M bytes. A
huge pointer can be incremented and decremented using arithmetic operators. The only
difference between a far pointer and a huge pointer is that a huge pointer is normalized by
the compiler. A normalized pointer is one that has as much of the address as possible in
the segment, meaning that the offset is never larger than 15. A huge pointer is normalized
only when pointer arithmetic is performed on it. It is not normalized when an assignment
is made.

Generic Pointers
When a variable is declared as being a pointer to type void it is known as a generic
pointer. Since you cannot have a variable of type void, the pointer will not point to any
data and therefore cannot be dereferenced. It is still a pointer though, to use it you just
have to cast it to another kind of pointer first. Hence the term Generic pointer.
This is very useful when you want a pointer to point to data of different types at different
times.

Smart pointers are objects which store pointers to dynamically allocated (heap) objects.
Smart pointers are particularly useful in the face of exceptions as they ensure proper
destruction of dynamically allocated objects. They can also be used to keep track of
dynamically allocated objects shared by multiple owners

How are pointer variables initialized?

Pointer variable are initialized by one of the following two ways<br><br>?Static memory
allocation<br><br>?Dynamic memory allocation

Pointers are again variables which hold addresses. Hence, there is no special way of
initializing pointer variables. However, towards achieving that perfect code, most
programmers initialize the pointers to NULL if they wish to allocate memory at run time
using malloc().

Can you add pointers together? Why would you?


No, you can?t add pointers together. If you live at 1332 Lakeview Drive, and your
neighbor lives at 1364
Lakeview, what?s 1332+1364? It?s a number, but it doesn?t mean anything. If you try to
perform this type of calculation with pointers in a C program, your compiler will
complain.

The only time the addition of pointers might come up is if you try to add a pointer and the
difference of two pointers.

What is a void pointer?

A void pointer is a C convention for ?a raw address.? The compiler has no idea what type
of object a void Pointer ?really points to.? If you write

int *ip;

ip points to an int. If you write

void *p;

p doesn?t point to a void!

In C and C++, any time you need a void pointer, you can use another pointer type. For
example, if you have a char*, you can pass it to a function that expects a void*. You don?
t even need to cast it. In C (but not in C++), you can use a void* any time you need any
kind of pointer, without casting. (In C++, you need to cast it).

A void pointer is used for working with raw memory or for passing a pointer to an
unspecified type.
Some C code operates on raw memory. When C was first invented, character pointers
(char *) were used for that. Then people started getting confused about when a character
pointer was a string, when it was a character array, and when it was raw memory.

What is a pointer variable?

A pointer variable is a variable that may contain the address of another variable or any
valid address in the memory

Is **p and &(*p) same?

No. &(*p)-holds only address of the value stored in a variable. *p-refers the value..**p
refers pointer to pointer..

What is the difference between NULL and NUL?

NULL is a macro defined in <stddef.h> for the null pointer.


NUL is the name of the first character in the ASCII character set. It corresponds to a zero
value. There?s no
standard macro NUL in C, but some people like to define it.
The digit 0 corresponds to a value of 80, decimal. Don?t confuse the digit 0 with the
value of ?? (NUL)!
NULL can be defined as ((void*)0), NUL as ??.

What is a const pointer?

There are cases when you need to define a constant pointer to a variable/object; for
instance, when taking a function address, or when you want to protect a pointer from
unintended modifications such as assignment of new address, pointer arithmetic, etc. In
fact, an object?s this is a constpointer. A constant pointer is declared:

Constant Pointer is a pointer which can not be reinitialized or cant be asigned a new
address.
It is also called a reference.

Can a file other than a .h file be included with #include?

The preprocessor will include whatever file you specify in your #include statement.
Therefore, if you have the line

#include <macros.inc>

in your program, the file macros.inc will be included in your precompiled program. It is,
however, unusual programming practice to put any file that does not have a .h or .hpp
extension in an #include statement.

You should always put a .h extension on any of your C files you are going to include.
This method makes it easier for you and others to identify which files are being used for
preprocessing purposes. For instance, someone modifying or debugging your program
might not know to look at the macros.inc file for macro definitions. That person might try
in vain by searching all files with .h extensions and come up empty. If your file had been
named macros.h, the search would have included the macros.h file, and the searcher
would have been able to see what macros you defined in it.

Can static variables be declared in a header file?

You can?t declare a static variable without defining it as well (this is because the storage
class modifiers
static and extern are mutually exclusive). A static variable can be defined in a header file,
but this would cause each source file that included the header file to have its own private
copy of the variable, which is probably not what was intended.

Static variables can be declared in a header file. However, their definition should also be
provided in the same header file. But , doing this makes the static variable as a private
copy of the header file ie it cannot be used elsewhere. In normal cases, this is not
intended from a header file. It is generally a ad idea to use a static variable in a header
file.

What is the difference between compile time error and run time error?

Compile time is semantic and syntax error, whereas run time is memory allocation error
e.g. segmentation fault.

How to type a string without using printf function?

//printing a string without printf#includeint main(){ char *str="shobhit"; while((*str)!


=NULL) { putchar(*str); str++; } return 0;}

Potrebbero piacerti anche