DISHA NIRDESHAN

Enlightens your career

Wednesday, June 8, 2011

Differences among concepts

What is the difference between 'break' and 'continue' statements?

Differences between 'break' and 'continue' statements:

break

continue

1. break is a keyword used to terminate the loop or exit from the block.The control jumps to next statement after the loop or block.

1. continue is a keyword used for skipping the current iteration and go to next iteration of the loop

2.Syntax:

{

Statement 1;

Statement 2;

Statement n;

break;

}

2.Syntax:

{

Statement 1;

continue;

Statement 2;

}

3. break can be used with for, while, do- while, and switch statements.When break is used in nested loops i.e. within the inner most loop then only the innermost loop is terminated.

3. This statement when occurs in a loop does not terminate it but skips the statements after this continue statement. The control goes to the next iteration. continue can be used with for, while and do-while.

4. Example:

i = 1, j = 0;

while(i<=5){

 

i=i+1;

if(i== 2)

 

break;

j=j+1;

 

}

4. Example:

i = 1, j = 0;

while(i<=5){

 

i=i+1;

if(i== 2)

 

continue;

j=j+1;

 

Is it better to use malloc() or calloc()?

Both the malloc() and the calloc() functions are used to allocate dynamic memory. Each operates slightly different from the other. malloc() takes a size and returns a pointer to a chunk of memory at least that big:
void *malloc( size_t size );
calloc() takes a number of elements, and the size of each, and returns a pointer to a chunk of memory at least big enough to hold them all:
void *calloc( size_t numElements, size_t sizeOfElement );
There's one major difference and one minor difference between the two functions. The major difference is that malloc() doesn't initialize the allocated memory. The first time malloc() gives you a particular chunk of memory, the memory might be full of zeros. If memory has been allocated, freed, and reallocated, it probably has whatever junk was left in it. That means, unfortunately, that a program might run in simple cases (when memory is never reallocated) but break when used harder (and when memory is reused). calloc() fills the allocated memory with all zero bits. That means that anything there you're going to use as a char or an int of any length, signed or unsigned, is guaranteed to be zero. Anything you're going to use as a pointer is set to all zero bits. That's usually a null pointer, but it's not guaranteed.Anything you're going to use as a float or double is set to all zero bits; that's a floating-point zero on some types of machines, but not on all.
The minor difference between the two is that calloc() returns an array of objects; malloc() returns one object. Some people use calloc() to make clear that they want an array.

 

What is the difference between a string and an array?

An array is an array of anything. A string is a specific kind of an array with a well-known convention to determine its length.
There are two kinds of programming languages: those in which a string is just an array of characters, and those in which it's a special type. In C, a string is just an array of characters (type char), with one wrinkle: a C string always ends with a NUL character. The "value" of an array is the same as the address of (or a pointer to) the first element; so, frequently, a C string and a pointer to char are used to mean the same thing.
An array can be any length. If it's passed to a function, there's no way the function can tell how long the array is supposed to be, unless some convention is used. The convention for strings is NUL termination; the last character is an ASCII NUL ('') character.


What are the differences between structures and unions?

Structures and Unions are used to store members of different data types.

STRUCTURE

UNION

a)Declaration:

struct

  {

   data type member1;

   data type member2;

  };

a)Declaration:

union

  {

   data type member1;

   data type member2;

  };

b)Every structure member is allocated memory when a structure variable is defined.
Example:

struct emp {

 char name[5];

 

 int age;

 float sal;

 

};

 

 

struct emp e1;

Memory allocated for structure is 5+4+4=13 bytes(assuming sizeof int is 4, float is 4, char is 1). 5 byte for name, 4 bytes for age and 4 bytes for sal.

b)The memory equivalent to the largest item is allocated commonly for all members.
Example:

union emp1 {

 char name[5];

 

 int age;

 float sal;

 

};

 

 

union emp1 e2;

Memory allocated to a union is equal to size of the largest member. In this case, char name[5] is the largest-sized member. Hence memory allocated to this union is 5 bytes.

c)All structure variables can be initialized at a time

struct st {

 int a;

 

 float b;

};

 

struct st s = { .a=4, .b=10.5 };

Structure is used when all members are to be independently used in a program.

c)Only one union member can be initialized at a time

union un {

 int a;

 

 float b;

};

 

 

union un un1 = { .a=10 };

Union is used when members of it are not required to be accessed at the same time.



=================================================================================================================

What is the difference between getchar and scanf functions for reading strings?

Differences between getchar and scanf functions for reading strings:

scanf

getchar

1. Entering of each character should be followed by return key.

1. Need not type return key.

2. Continuous stream of characters cannot be directly supplied using scanf function.

2. Continuous stream of characters can be directly supplied using getchar function

3. Scanf function can be used to provide data at execution time irrespective of its type(int, char, float).
Example:

#include<stdio.h>

int  main() {

 

char a[10];

printf("Enter a: \n");

 

scanf("%s",a);

return 0;

 

}

3. getchar() function is used only with character type.
Example:

#include<stdio.h>

int main() {

 

char a;

printf("Enter any character: \n");

 

a = getchar();

printf("Character entered:%c \n",a);

 

return 0;

}

4. scanf() returns the number of items read successfully. A return value 0 indicates that no fields were read. EOF(end of file) is returned in case of an error or if end-of-file/end-of-string character is encountered.

4. getchar() returns the character entered as the value of the function. It returns EOF in case of an error. It is recommeded to use getchar instead of scanf.

 



0 comments :