Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
nt main()
{
printf("Enter the two numbers: \n");
int a,b;
scanf("%d",&a);
scanf("%d",&b);
printf("Sum is: %d",add(a,b));
}
Explanation:
Enter the two numbers:
38 99
38 99--------00100110,1100011
68 69--------01000100,1000101
136 1--------10001000,1
0 137--------00000000,10001001
Sum is: 137
4.How to find whether a given number is even or odd without using % (modulus)
operator ?
//---------------------//
bool isOdd(int num)
{
return num&1?true:false;
}
//---------------------//
5.How to use malloc?
#include <stdlib.h> //it is necessary to include this header file for reasons I will mention later
#include<stdio.h>
int main(void){
{ int i = 0;
int *a = 0; //a is visible only within this block
const int num = 10;
a = malloc(sizeof(int)*num); //allocates memory large enough to hold 10 int values
????????????????????
//---------------------//
#include<stdio.h>
int main()
{
int class;
class=10;
printf("%d",class);
}
//---------------------//
//---------------------//
#include<stdlib.h>
int main()
{
int *array=malloc(sizeof(int)*100);
}
//---------------------//
This is valid in C because a pointer of type void* can be assigned to any other pointer
without cast, but this is not in valid C++ because will have to give an explicit cast to it as
in:
int *array=(int*)malloc(sizeof(int)*100);
/---------------------//
#include<stdio.h>
int main()
{
static int i=5;
if(i>0)
printf("%d\n",i);
else
{
i--;
main();
return 0;
}
}
//---------------------//
This code is valid in C because recursion of main function is legal in C language whereas
it is illegal in C++ language.
//---------------------//
These qualifiers are not available in the new 32 and 64 bit compilers (like VC++ and gcc)
because they use flat memory model∞ where memory is not divided into different
segments, so these compilers do not have or require such qualifiers
struct tmpStruct {
char name[20];
int id;
float f;
}varStruct;
union tmpUnion {
char name[20];
int id;
float f;
}varUnion;
varUnion.id = 50;
varUnion.f = 100.00f;
printf("%d\n", varUnion.id); // Gives undefined result as last assigned member was
varUnion.f.
varStruct.id = 50;
varStruct.f = 100.00f;
printf("%d\n", varStruct.id); // Prints 50
Here the variable 'varStruct' will be allocated with total number of bytes required by its
members, along with padding bits if it is required (ie. sizeof(name) + sizeof(id) +
sizeof(f) + Padding). So here each member will be having its own memory location. On
the other hand, the variable 'varUnion' will be allocated only with the largest member of
it(ie. sizeof(name) + Padding). As a result, for a union only the member which was last
assigned can be accessed. For example, the following code may give undefined results:
9.How to find whether the given number is a power of 2 in single a single step
(without using loops) ?
bool isPowerOf2(int num){return ((num>0) && (num & (num1))==0);}
10.How can we return more than one value from a function?
??????????????????????????????????????
shrinivas
PREM
int a=10;
int main()
{ int b=20;
static int c=30;
int *p;
p = &c;
p;
*p = a++ + b + c;
printf("%d", a);
}
13. Add two no with out using ‘+’.
#include<stdio.h>
#include<conio.h>
void main()
{
int num,num1;
printf("Enter the two numbers : ");
scanf("%d %d",&num,&num1);
while(num1>0)
{
num1--;
num++;
}
printf("The sum of these two number is = ",num);
}
in view of programmer:
exit(0) : normal termination
exit(1): abnormal termination
exit(0) terminates ur application with writing all updated inforamation to the OS but
exit(1) terminates ur application without updating the info to OS
a)System calls are the operating systems routine used internally to interact in low
level, While funtions are generally used to return the values required by the user
at user level.
A:#include<stdio.h>
int main()
{
int a,b;
printf("\n Enter the 2 numbers:");
scanf("%d%d",&a,&b);
b=(a+b)-(a=b);
printf("\n A = %d \n B = %d\n",a,b);
}
Q.What is the difference between null pointer and void pointer?
A.
a)Null pointers returns null value and void pointer returns no
value.
b)NULL POINTER :
VOID POINTER :
the pointer is a one which can only point to a particular
memory location of it's own type.... but when the pointer
is given void it can be mde to point any location of
different type
Command use: