Arjun Suresh (talk | contribs) |
Arjun Suresh (talk | contribs) |
||
Line 17: | Line 17: | ||
|type="{}" | |type="{}" | ||
/} | /} | ||
− | + | { 215 } | |
{ | { |
<quiz display="simple"> { <syntaxhighlight lang="c">
int fib(int n){
if(n<=2)return 1; else return fib(n-1)+fib(n-2);
} main(){ int n; printf("enter the number "); scanf("%d",&n); printf("fib(%d)=%d",n,fib(n)); } </syntaxhighlight> |type="{}" /} { 215 }
{ <syntaxhighlight lang="c">
struct node { struct node *lc,*rc; int data; };//tree node int ino[25],pre[25],count=0,val; int left=0,right,val1;//global variables
inorder(struct node *root){ if(root==NULL)return; inorder(root->lc); printf("%d ",root->data); inorder(root->rc); }//inorder traversal postorder(struct node *root){ if(root==NULL)return; postorder(root->lc); postorder(root->rc); printf("%d ",root->data); }//post order traversal int find(int val,int no){ int i,j,k; left=count,right=0; for(j=0;j<count;j++){ if(ino[j]==val)break; } for(i=0;i<no;i++){ val1=pre[i]; for(k=0;k<count;k++){ if(ino[k]==val1)break; } printf("k=%d\n",k); if(k>j && k<left)left=k; if(k<j && k>right){ printf("HELLO"); right=k; }} }//finds position in tree for val to be inserted struct node *insert(struct node *root,int val,int left,int right){ if(root==NULL){ root=(struct node *)malloc(sizeof(struct node)); root->lc=root->rc=NULL; root->data=val; return root; } if(root->data==ino[left] && root->lc==NULL){ root->lc=insert(root->lc,val,left,right); return root; } if(root->data==ino[right] && root->rc==NULL){ root->rc=insert(root->rc,val,left,right); return root; } if(root->lc)root->lc=insert(root->lc,val,left,right); if(root->rc)root->rc=insert(root->rc,val,left,right); return root; }//inserts to the tree main(){ struct node *root=NULL; int prepos=0,inpos=0; int pos,i; printf("Enter the inorder traversal output\n"); while(scanf("%d",&ino[count++])!=EOF);//press cntl+D on end of input count=0; printf("Enter the preorder traversal output\n"); while(scanf("%d",&pre[count++])!=EOF);//press cntl+D on end of input count--; for(i=0;i<count;i++){ find(pre[i],i); val=pre[i]; printf("left=%d right=%d val=%d\n",left,right,val); root=insert(root,val,left,right); } printf("POST ORDER\n"); postorder(root); printf("\nINORDER\n"); inorder(root); } </syntaxhighlight> |type="{}" /}
{
<syntaxhighlight lang="c">
struct node { int val; struct node *next; }; struct node *insert(int val,struct node *head){ struct node *temp; temp=malloc(sizeof(struct node)); temp->val=val; if(head==NULL)return temp; temp->next=head; return temp; }
void print(struct node *head){
if(head==NULL)return; printf("%d %d ",head->val, head); print(head->next); } struct node *head1; struct node *reverse_rec(struct node *t1,struct node *t2){
if(t2->next==NULL){ t2->next=t1; head1=t2; return; } reverse_rec(t1->next,t2->next); t2->next=t1; return; }
struct node *reverse(struct node *head){ struct node *stack[20],*t1=head; int top=0; while(t1!=NULL){ stack[top++]=t1; t1=t1->next; } top--; head=stack[top]; while(top>0){ stack[top]->next=stack[top-1]; top--; } stack[top]->next=NULL; return head; }
main(){ int val; struct node *head=NULL; printf("\nenter elements to be inserted to list\n");
while(scanf("%d",&val)!=EOF)head=insert(val,head); printf("\nlist\n"); print(head); printf("\nlist with reversal using stack\n"); head=reverse(head); printf("\n"); print(head); printf("\nlist with reversal using recursion\n"); reverse_rec(head,head->next); head->next=NULL; printf("\n"); print(head1);
} </syntaxhighlight> |type="{}" /}
{
<syntaxhighlight lang="c">
int main(){ int a[100][100],b[100][100],c[100][100],i=0,j=0,k=0,n,x,y; //program for doing matrix multiplication where arrays are allocated dynamically using malloc.It uses Double pointer printf("Enter size of square matrix\n"); scanf("%d",&n); for(i=0;i<n;i++) for(j=0;j<n;j++) { if((i+j)%2==0){a[i][j]=i*n+j; x=n+2;y=n-2; } else{
a[i][j]=i+j;
x=n-3;y=n+3; } x=x+2*n; y=y-n; }
for(i=0;i<n;i++) for(j=0;j<n;j++) { if((i+j)%2==0)b[i][j]=i*n+j; else b[i][j]=i+j; } for(i=0;i<n;i++) for(j=0;j<n;j++) c[i][j]=0;
printf("MATRIX A=\n"); for(i=0;i<n;i++){ for(j=0;j<n;j++) printf("%d ",a[i][j]); printf("\n"); } printf("MATRIX B=\n"); for(i=0;i<n;i++){ for(j=0;j<n;j++) printf("%d ",b[i][j]); printf("\n"); } printf("MULADD(A,B)=\n"); for( i=0;i<n;i++) for(j=0;j<n;j++) for(k=0;k<n;k++){ if((i+j)%2==0)c[i][j]=c[i][j]+a[i][k]*b[k][j]; else c[i][j]=c[i][j]+i+j; } for( i=0;i<n;i++){ for(j=0;j<n;j++) printf("%d ",c[i][j]); printf("%d %d\n",x,y);
} } //program showing how structure size vary with order of declaration //of variable. char,int,char=12 byes int,char,char=8 bytes struct node{ char c; int a; char d; };//this structure will occuppy 12 bytes with 4 byte memory allignment struct node1{ int a; char b; char c; };//this structure will occupy 8 bytes on memory with 4 byte allighment struct node2{ long x; char y; char y3; }; main(){ printf("%d %d %d ",sizeof(struct node),sizeof(struct node1),sizeof(struct node2)); } </syntaxhighlight> |type="{}" /}
{
<syntaxhighlight lang="c">
main(){ char b[4]="yyy"; char a[3]="xxx"; //string should extra one byte for \0 printf("a=%s\n b=%s\n",a,b); } </syntaxhighlight> |type="{}" /}
{
<syntaxhighlight lang="c">
main(){ char x,y; printf("Enter the characters\n"); scanf("%c %c",&x,&y); printf(" x=%c, y=%c\n ",x,y); x=x^y; y=x^y; x=x^y; /* x=x+y; y=x-y; x=x-y;
printf("swaped x=%c, y=%c\n ",x,y);
} </syntaxhighlight> |type="{}" /}
{
<syntaxhighlight lang="c">
int DO(char *ptr){
if(*ptr=='\0')return;
DO(ptr+1); DO(ptr+1); printf("%c",*ptr); } main(){
char *ptr="abc";
DO(ptr); }
enum hello{ uh=1,e,l=3,o }; enum helloq{ h=1,ue,ul=3,uo//if replace h with uh it will show errors while compilng }; // uh is used in hello main(){ enum hello x; enum helloq y; y=h; x=uo; printf("%5.2i\n%-5.2i\n",x,y); } </syntaxhighlight> |type="{}" /}
{
<syntaxhighlight lang="c">
int main(){ int i,j,k; j=8;k=9; i=(((++j<k++)&&(k--<10))? 2:1); //i=(((++j<k++)||(k--<10))? 2:1); printf("%d %d %d",i,j,k); } // output 1 9 10 main(){ int fib1=1,fib2=1,fib3; int n,i; printf("Enter the number "); scanf("%d",&n); if(n<=2)printf("(fib(%d)=%d",n,1); for(i=1;i<=n-2;i++){ fib3=fib1+fib2; fib1=fib2; fib2=fib3; } printf("fib(%d)=%d",n,fib3); } </syntaxhighlight> |type="{}" /}
{
<syntaxhighlight lang="c">
//program explaining how to use function pointers int sum(int a,int b){ return a+b; } int mul(int a,int b){ return a*b; } main(){ int (*fp)(int,int);//pointer to a function taking as argument two integer and returns int,sum() and mul() are possibile candidates in this file. int y; fp=sum; y=fp(3,4); printf("%d\n",y); fp=mul; y=fp(3,4); printf("%d\n",y); }
{
<syntaxhighlight lang="c">
main(){ int i=10; printf("%d\n",i=i==11); printf("%d\n",i=++i==11); } //output 0 0 </syntaxhighlight> |type="{}" /}
{
<syntaxhighlight lang="c">
struct test_struct {
int val;//value stored in list node struct test_struct *next;//pointer to next node in list
};
struct test_struct *head = NULL;//always points to first node in list struct test_struct *curr = NULL;//always points to last node of list ////////////////////////////////////////////////////////////// //insert a node in to empty list and make list head point to this node ////////////////////////////////////////////////////////////// struct test_struct* create_list(int val) {
printf("\n creating list with headnode as [%d]\n",val); struct test_struct *ptr = (struct test_struct*)malloc(sizeof(struct test_struct)); if(NULL == ptr) { printf("\n Node creation failed \n"); return NULL; } ptr->val = val; ptr->next = NULL;
head = curr = ptr; return ptr;
} /////////////////////////////////////////////////////////////////////// //add a new node with value val to list. if add_to_end==1 new node addded to end of list //if add_to_end==0 new node added to end of list. //////////////////////////////////////////////////////////////////////////////////////// struct test_struct* add_to_list(int val, bool add_to_end) {
if(NULL == head) { return (create_list(val)); }
if(add_to_end) printf("\n Adding node to end of list with value [%d]\n",val); else printf("\n Adding node to beginning of list with value [%d]\n",val);
struct test_struct *ptr = (struct test_struct*)malloc(sizeof(struct test_struct)); if(NULL == ptr) { printf("\n Node creation failed \n"); return NULL; } ptr->val = val; ptr->next = NULL;
if(add_to_end) { curr->next = ptr; curr = ptr; } else { ptr->next = head; head = ptr; } return ptr;
} //////////////////////////////////////////////////////////////////////////////// //search for an element with value val in list ////////////////////////////////////////////////// struct test_struct* search_in_list(int val, struct test_struct **prev) {
struct test_struct *ptr = head; struct test_struct *tmp = NULL; bool found = false;
printf("\n Searching the list for value [%d] \n",val);
while(ptr != NULL) { if(ptr->val == val) { found = true; break; } else { tmp = ptr; ptr = ptr->next; } }
if(true == found) { if(prev) *prev = tmp; return ptr; } else { return NULL; }
} ///////////////////////////////////////////////////////////////////////// //delete a node with value val from list //////////////////////////////////////////////////////////////////////// int delete_from_list(int val) {
struct test_struct *prev = NULL; struct test_struct *del = NULL;
printf("\n Deleting value [%d] from list\n",val);
del = search_in_list(val,&prev); if(del == NULL) { return -1; } else { if(prev != NULL) prev->next = del->next;
if(del == curr) { curr = prev; } else if(del == head) { head = del->next; } }
free(del); del = NULL;
return 0;
} /////////////////////////////////////////////////// //prints all nodes in the list ////////////////////////////////////////////////// void print_list(void) {
struct test_struct *ptr = head;
printf("\n -------Printing list Start------- \n"); while(ptr != NULL) { printf(" [%d] ",ptr->val); ptr = ptr->next; } printf("\n -------Printing list End------- \n");
return;
}
int main(void) {
int i = 0, ret = 0; struct test_struct *ptr = NULL;
//list empty here
print_list();
//list with values 5,6,7,8,9 .elements added to end of list
for(i = 5; i<10; i++) add_to_list(i,true);
print_list();
getchar(); //list with values 1,2,3,4,5,6,7,8,9.elements added to front of list
for(i = 4; i>0; i--) add_to_list(i,false);
print_list();
getchar();
for(i = 1; i<10; i += 4) {
//searches in list for valyes 1,5,9
ptr = search_in_list(i, NULL); if(NULL == ptr) { printf("\n Search [val = %d] failed, no such element found\n",i); } else { printf("\n Search passed [val = %d]\n",ptr->val); }
print_list();
getchar(); //deletes from list for valyes 1,5,9
ret = delete_from_list(i); if(ret != 0) { printf("\n delete [val = %d] failed, no such element found\n",i); } else { printf("\n delete [val = %d] passed \n",i); }
print_list();
getchar();
}
return 0;
}
{
<syntaxhighlight lang="c">
//way to represent hexa and octal number in C. 077=7*8+7=63 main(){ int x=0x777; int y=077; printf("%d %d\n",x,y); } </syntaxhighlight> |type="{}" /}
{
<syntaxhighlight lang="c">
int a1[]={6,7,8,18,34,67}; int a2[]={23,56,28,29}; int a3[]={-12,27,-31}; int *x[]={a1,a2,a3}; void print (int *a[]){ printf("%d\n",a[0][2]); printf("%d\n",*a[2]); printf("%d\n",*++a[0]);//a[0] will now point from a1[0]to a1[1] printf("%d\n",*(++a)[0]);//a will now point from a1 to a2 printf("%d\n",a[-1][1]);//a[-1] is now a1 a[-1][1]= a1[1+1]=a[2] } main(){ print(x); } // output 8 -12 7 23 8
int a[]={1,2,3,4,5,6,7,8}; main(){ int *p=&a[4]; int *q=&a[0],i; printf("BEFORE MODIFICATION\n"); for(i=0;i<8;i++)printf(" a[%d]=%d\n",i,a[i]); printf("Exectuted following statements\n p=&a[4];q=&a[0];\n"); printf("*(q++)=*(p++)+100; *++q=*++p;\n");
printf(" AFTER MODIFICATION\n");
for(i=0;i<8;i++)printf(" a[%d]=%d\n",i,a[i]); getchar(); printf(" NOW "); printf("*q=%d\n",*q);
printf(" AFTER THE MODIFICATION OF"); printf(" *++q=*++p;\n"); for(i=0;i<8;i++)printf(" a[%d]=%d\n",i,a[i]); printf("*q=%d\n",*q); }
{ <syntaxhighlight lang="c">
main(){ char h[12]="hello,world"; printf("%s\n",h); printf("%10s\n",h); printf("%.10s\n",h); printf("%-10s\n",h); printf("%.15s\n",h); printf("%15.10s\n",h); printf("%-15.10s\n",h); } </syntaxhighlight> |type="{}" /}
{
<syntaxhighlight lang="c">
//arithmetic operation on pointer is allowed.this program will print 4 main(){ int *p,*q; int a[10],b=4; p=(int *)malloc(sizeof(int)); q=(int *)malloc(sizeof(int)); p=a; q=&a[4]; //printf("%-*d",b,(q-p)); printf("%-4d",(q-p));//both printfs are equivalent } </syntaxhighlight> |type="{}" /}
{
<syntaxhighlight lang="c">
//function with variable number of arguments in c int FindMax ( int amount, ...) {
int i,val,greater; va_list vl; va_start(vl,amount); greater=va_arg(vl,int); printf("%d\n",greater); for (i=1;i<amount;i++) { val=va_arg(vl,int);
printf("%d\n",val);
greater=(greater>val)?greater:val; }
va_end(vl);
va_start(vl,amount);
greater=va_arg(vl,int); printf("%d\n",greater); for (i=1;i<amount;i++) { val=va_arg(vl,int);
printf("%d\n",val);
greater=(greater>val)?greater:val; }
va_end(vl); return greater;
}
int main () {
int m; m= FindMax (7,702,422,631,834,892,104,772); printf ("The greatest one is: %d\n",m); m= FindMax (4,702,422,631,834); printf ("The greatest one is: %d\n",m); return 0;
} </syntaxhighlight> |type="{}" /}
{
<syntaxhighlight lang="c">
//program showing bit integers.flag will have a size 4 struct flag{ unsigned int key:1; unsigned int sta:1; char c; unsigned int glo:1; unsigned int glo1:1; }; struct node{ int x; char c; int y; int z; }; struct node1{ int x; int y; int z; char c; }; struct charx{ char a; }; main(){ printf("flag size=%d\n",sizeof(struct flag)); printf("short size=%d\n",sizeof(short)); printf("long size=%d\n",sizeof(long)); printf("node size=%d\n",sizeof(struct node)); printf("node1 size=%d\n",sizeof(struct node1)); printf("charx size=%d\n",sizeof(struct charx)); } </syntaxhighlight> |type="{}" /}
{
<syntaxhighlight lang="c">
main(){ char a[]="gate2011"; printf("\n%s\n", a+a[3]-a[1]); } </syntaxhighlight> |type="{}" /}
{
<syntaxhighlight lang="c">
unsigned int foo (unsigned int n, unsigned int r) { if (n >0) return n%r+ foo( n / r, r ); else return 0; } main(){ printf("\nfoo(513,2)=%d\n",foo(513,2)); printf("\nfoo(345,10)=%d\n",foo(345,10)); } </syntaxhighlight> |type="{}" /}
{
<syntaxhighlight lang="c">
void f (int *p, int * q) {
p=q; *q=2;
} int i = 0, j =1; int main ( ){ f(&i,&j); printf ("i=%d j=%d \n", i, j) ; return 0; } </syntaxhighlight> |type="{}" /}
{
<syntaxhighlight lang="c">
int f(int * a, int n){ if (n<= 0)return 0; else if(*a% 2== 0) return *a + f(a+1,n-1);
else return *a-f(a+1, n-1); }
int main ( ) {
int a[ ] = {12, 7, 13, 4, 11, 6};
printf ("\nf(a,6)=%d\n", f(a,6)); return 0; } </syntaxhighlight> |type="{}" /}
{
<syntaxhighlight lang="c">
main(){ char inChar = 'A'; switch ( inChar ) { case 'A' : printf("Choice A\n") ; case 'B' : case 'C' : printf("Choice B") ; case 'D' : case 'E' : default : printf( "No Choice" ) ;
}
} </syntaxhighlight> |type="{}" /}
{
<syntaxhighlight lang="c">
struct node{ struct node *left; struct node *right; int val; };
typedef struct node * treeptr; struct node *insert(struct node *root,int val){ if(root==NULL){ root=(struct node *)malloc(sizeof(struct node)); root->left=root->right=NULL; root->val=val; return root; } if(root->val>val)root->left=insert(root->left,val); if(root->val<val)root->right=insert(root->right,val); if(root->val==val)printf("NODE ALREADY PRESENT\n"); return root; }
int max(int a,int b){ if(a>b)return a; return b; } int height (treeptr n){ int h1,h2;
if (n == NULL) return -1;
if (n->left == NULL){ if (n->right == NULL) return 0; else return 1+height(n->right); } else { h1 = height (n->left); if (n->right == NULL) return (1+h1); else { h2 = height (n->right); } return 1+max(h1,h2); } }
main(){ int val; struct node *root=NULL; printf("Enter the elements to be inserted to BST\n"); while(scanf("%d",&val)!=EOF)root=insert(root,val); printf("height of tree=%d",height(root)); } </syntaxhighlight> |type="{}" /}
{
<syntaxhighlight lang="c">
int a, b, c = 0; void prtFun(void); main( ) { static int a = 1;/*line1*/ //auto int a = 1;/*Line 1 */
prtFun( ); a+=1; prtFun(); printf("\n %d %d", a, b); } void prtFun(void) { static int a = 2;/*Line 2 */ //register int a = 2;/*Line 2 */ int b = 1; a+=++b; printf("\n %d %d" , a, b); }
int f (int &x, int c) { c = c - 1; if (c==0) return 1; x = x + 1; return f(x,c) * x; } main(){ int p=5; printf("\np=5f(p,p)%d\n",f(p,p)); } </syntaxhighlight> |type="{}" /}
{ What is the output of the following program?
<syntaxhighlight lang="c">
int fib(int n){ if(n<=2)return 1; else return fib(n-1)+fib(n-2); } main(){ int n; printf("enter the number "); scanf("%d",&n); printf("fib(%d)=%d",n,fib(n)); } </syntaxhighlight> |type="{}" /} { Abc }
</quiz>
<quiz display="simple"> { <syntaxhighlight lang="c">
int fib(int n){
if(n<=2)return 1; else return fib(n-1)+fib(n-2);
} main(){ int n; printf("enter the number "); scanf("%d",&n); printf("fib(%d)=%d",n,fib(n)); } </syntaxhighlight> |type="{}" /} { 215 }
{ <syntaxhighlight lang="c">
struct node { struct node *lc,*rc; int data; };//tree node int ino[25],pre[25],count=0,val; int left=0,right,val1;//global variables
inorder(struct node *root){ if(root==NULL)return; inorder(root->lc); printf("%d ",root->data); inorder(root->rc); }//inorder traversal postorder(struct node *root){ if(root==NULL)return; postorder(root->lc); postorder(root->rc); printf("%d ",root->data); }//post order traversal int find(int val,int no){ int i,j,k; left=count,right=0; for(j=0;j<count;j++){ if(ino[j]==val)break; } for(i=0;i<no;i++){ val1=pre[i]; for(k=0;k<count;k++){ if(ino[k]==val1)break; } printf("k=%d\n",k); if(k>j && k<left)left=k; if(k<j && k>right){ printf("HELLO"); right=k; }} }//finds position in tree for val to be inserted struct node *insert(struct node *root,int val,int left,int right){ if(root==NULL){ root=(struct node *)malloc(sizeof(struct node)); root->lc=root->rc=NULL; root->data=val; return root; } if(root->data==ino[left] && root->lc==NULL){ root->lc=insert(root->lc,val,left,right); return root; } if(root->data==ino[right] && root->rc==NULL){ root->rc=insert(root->rc,val,left,right); return root; } if(root->lc)root->lc=insert(root->lc,val,left,right); if(root->rc)root->rc=insert(root->rc,val,left,right); return root; }//inserts to the tree main(){ struct node *root=NULL; int prepos=0,inpos=0; int pos,i; printf("Enter the inorder traversal output\n"); while(scanf("%d",&ino[count++])!=EOF);//press cntl+D on end of input count=0; printf("Enter the preorder traversal output\n"); while(scanf("%d",&pre[count++])!=EOF);//press cntl+D on end of input count--; for(i=0;i<count;i++){ find(pre[i],i); val=pre[i]; printf("left=%d right=%d val=%d\n",left,right,val); root=insert(root,val,left,right); } printf("POST ORDER\n"); postorder(root); printf("\nINORDER\n"); inorder(root); } </syntaxhighlight> |type="{}" /}
{
<syntaxhighlight lang="c">
struct node { int val; struct node *next; }; struct node *insert(int val,struct node *head){ struct node *temp; temp=malloc(sizeof(struct node)); temp->val=val; if(head==NULL)return temp; temp->next=head; return temp; }
void print(struct node *head){
if(head==NULL)return; printf("%d %d ",head->val, head); print(head->next); } struct node *head1; struct node *reverse_rec(struct node *t1,struct node *t2){
if(t2->next==NULL){ t2->next=t1; head1=t2; return; } reverse_rec(t1->next,t2->next); t2->next=t1; return; }
struct node *reverse(struct node *head){ struct node *stack[20],*t1=head; int top=0; while(t1!=NULL){ stack[top++]=t1; t1=t1->next; } top--; head=stack[top]; while(top>0){ stack[top]->next=stack[top-1]; top--; } stack[top]->next=NULL; return head; }
main(){ int val; struct node *head=NULL; printf("\nenter elements to be inserted to list\n");
while(scanf("%d",&val)!=EOF)head=insert(val,head); printf("\nlist\n"); print(head); printf("\nlist with reversal using stack\n"); head=reverse(head); printf("\n"); print(head); printf("\nlist with reversal using recursion\n"); reverse_rec(head,head->next); head->next=NULL; printf("\n"); print(head1);
} </syntaxhighlight> |type="{}" /}
{
<syntaxhighlight lang="c">
int main(){ int a[100][100],b[100][100],c[100][100],i=0,j=0,k=0,n,x,y; //program for doing matrix multiplication where arrays are allocated dynamically using malloc.It uses Double pointer printf("Enter size of square matrix\n"); scanf("%d",&n); for(i=0;i<n;i++) for(j=0;j<n;j++) { if((i+j)%2==0){a[i][j]=i*n+j; x=n+2;y=n-2; } else{
a[i][j]=i+j;
x=n-3;y=n+3; } x=x+2*n; y=y-n; }
for(i=0;i<n;i++) for(j=0;j<n;j++) { if((i+j)%2==0)b[i][j]=i*n+j; else b[i][j]=i+j; } for(i=0;i<n;i++) for(j=0;j<n;j++) c[i][j]=0;
printf("MATRIX A=\n"); for(i=0;i<n;i++){ for(j=0;j<n;j++) printf("%d ",a[i][j]); printf("\n"); } printf("MATRIX B=\n"); for(i=0;i<n;i++){ for(j=0;j<n;j++) printf("%d ",b[i][j]); printf("\n"); } printf("MULADD(A,B)=\n"); for( i=0;i<n;i++) for(j=0;j<n;j++) for(k=0;k<n;k++){ if((i+j)%2==0)c[i][j]=c[i][j]+a[i][k]*b[k][j]; else c[i][j]=c[i][j]+i+j; } for( i=0;i<n;i++){ for(j=0;j<n;j++) printf("%d ",c[i][j]); printf("%d %d\n",x,y);
} } //program showing how structure size vary with order of declaration //of variable. char,int,char=12 byes int,char,char=8 bytes struct node{ char c; int a; char d; };//this structure will occuppy 12 bytes with 4 byte memory allignment struct node1{ int a; char b; char c; };//this structure will occupy 8 bytes on memory with 4 byte allighment struct node2{ long x; char y; char y3; }; main(){ printf("%d %d %d ",sizeof(struct node),sizeof(struct node1),sizeof(struct node2)); } </syntaxhighlight> |type="{}" /}
{
<syntaxhighlight lang="c">
main(){ char b[4]="yyy"; char a[3]="xxx"; //string should extra one byte for \0 printf("a=%s\n b=%s\n",a,b); } </syntaxhighlight> |type="{}" /}
{
<syntaxhighlight lang="c">
main(){ char x,y; printf("Enter the characters\n"); scanf("%c %c",&x,&y); printf(" x=%c, y=%c\n ",x,y); x=x^y; y=x^y; x=x^y; /* x=x+y; y=x-y; x=x-y;
printf("swaped x=%c, y=%c\n ",x,y);
} </syntaxhighlight> |type="{}" /}
{
<syntaxhighlight lang="c">
int DO(char *ptr){
if(*ptr=='\0')return;
DO(ptr+1); DO(ptr+1); printf("%c",*ptr); } main(){
char *ptr="abc";
DO(ptr); }
enum hello{ uh=1,e,l=3,o }; enum helloq{ h=1,ue,ul=3,uo//if replace h with uh it will show errors while compilng }; // uh is used in hello main(){ enum hello x; enum helloq y; y=h; x=uo; printf("%5.2i\n%-5.2i\n",x,y); } </syntaxhighlight> |type="{}" /}
{
<syntaxhighlight lang="c">
int main(){ int i,j,k; j=8;k=9; i=(((++j<k++)&&(k--<10))? 2:1); //i=(((++j<k++)||(k--<10))? 2:1); printf("%d %d %d",i,j,k); } // output 1 9 10 main(){ int fib1=1,fib2=1,fib3; int n,i; printf("Enter the number "); scanf("%d",&n); if(n<=2)printf("(fib(%d)=%d",n,1); for(i=1;i<=n-2;i++){ fib3=fib1+fib2; fib1=fib2; fib2=fib3; } printf("fib(%d)=%d",n,fib3); } </syntaxhighlight> |type="{}" /}
{
<syntaxhighlight lang="c">
//program explaining how to use function pointers int sum(int a,int b){ return a+b; } int mul(int a,int b){ return a*b; } main(){ int (*fp)(int,int);//pointer to a function taking as argument two integer and returns int,sum() and mul() are possibile candidates in this file. int y; fp=sum; y=fp(3,4); printf("%d\n",y); fp=mul; y=fp(3,4); printf("%d\n",y); }
{
<syntaxhighlight lang="c">
main(){ int i=10; printf("%d\n",i=i==11); printf("%d\n",i=++i==11); } //output 0 0 </syntaxhighlight> |type="{}" /}
{
<syntaxhighlight lang="c">
struct test_struct {
int val;//value stored in list node struct test_struct *next;//pointer to next node in list
};
struct test_struct *head = NULL;//always points to first node in list struct test_struct *curr = NULL;//always points to last node of list ////////////////////////////////////////////////////////////// //insert a node in to empty list and make list head point to this node ////////////////////////////////////////////////////////////// struct test_struct* create_list(int val) {
printf("\n creating list with headnode as [%d]\n",val); struct test_struct *ptr = (struct test_struct*)malloc(sizeof(struct test_struct)); if(NULL == ptr) { printf("\n Node creation failed \n"); return NULL; } ptr->val = val; ptr->next = NULL;
head = curr = ptr; return ptr;
} /////////////////////////////////////////////////////////////////////// //add a new node with value val to list. if add_to_end==1 new node addded to end of list //if add_to_end==0 new node added to end of list. //////////////////////////////////////////////////////////////////////////////////////// struct test_struct* add_to_list(int val, bool add_to_end) {
if(NULL == head) { return (create_list(val)); }
if(add_to_end) printf("\n Adding node to end of list with value [%d]\n",val); else printf("\n Adding node to beginning of list with value [%d]\n",val);
struct test_struct *ptr = (struct test_struct*)malloc(sizeof(struct test_struct)); if(NULL == ptr) { printf("\n Node creation failed \n"); return NULL; } ptr->val = val; ptr->next = NULL;
if(add_to_end) { curr->next = ptr; curr = ptr; } else { ptr->next = head; head = ptr; } return ptr;
} //////////////////////////////////////////////////////////////////////////////// //search for an element with value val in list ////////////////////////////////////////////////// struct test_struct* search_in_list(int val, struct test_struct **prev) {
struct test_struct *ptr = head; struct test_struct *tmp = NULL; bool found = false;
printf("\n Searching the list for value [%d] \n",val);
while(ptr != NULL) { if(ptr->val == val) { found = true; break; } else { tmp = ptr; ptr = ptr->next; } }
if(true == found) { if(prev) *prev = tmp; return ptr; } else { return NULL; }
} ///////////////////////////////////////////////////////////////////////// //delete a node with value val from list //////////////////////////////////////////////////////////////////////// int delete_from_list(int val) {
struct test_struct *prev = NULL; struct test_struct *del = NULL;
printf("\n Deleting value [%d] from list\n",val);
del = search_in_list(val,&prev); if(del == NULL) { return -1; } else { if(prev != NULL) prev->next = del->next;
if(del == curr) { curr = prev; } else if(del == head) { head = del->next; } }
free(del); del = NULL;
return 0;
} /////////////////////////////////////////////////// //prints all nodes in the list ////////////////////////////////////////////////// void print_list(void) {
struct test_struct *ptr = head;
printf("\n -------Printing list Start------- \n"); while(ptr != NULL) { printf(" [%d] ",ptr->val); ptr = ptr->next; } printf("\n -------Printing list End------- \n");
return;
}
int main(void) {
int i = 0, ret = 0; struct test_struct *ptr = NULL;
//list empty here
print_list();
//list with values 5,6,7,8,9 .elements added to end of list
for(i = 5; i<10; i++) add_to_list(i,true);
print_list();
getchar(); //list with values 1,2,3,4,5,6,7,8,9.elements added to front of list
for(i = 4; i>0; i--) add_to_list(i,false);
print_list();
getchar();
for(i = 1; i<10; i += 4) {
//searches in list for valyes 1,5,9
ptr = search_in_list(i, NULL); if(NULL == ptr) { printf("\n Search [val = %d] failed, no such element found\n",i); } else { printf("\n Search passed [val = %d]\n",ptr->val); }
print_list();
getchar(); //deletes from list for valyes 1,5,9
ret = delete_from_list(i); if(ret != 0) { printf("\n delete [val = %d] failed, no such element found\n",i); } else { printf("\n delete [val = %d] passed \n",i); }
print_list();
getchar();
}
return 0;
}
{
<syntaxhighlight lang="c">
//way to represent hexa and octal number in C. 077=7*8+7=63 main(){ int x=0x777; int y=077; printf("%d %d\n",x,y); } </syntaxhighlight> |type="{}" /}
{
<syntaxhighlight lang="c">
int a1[]={6,7,8,18,34,67}; int a2[]={23,56,28,29}; int a3[]={-12,27,-31}; int *x[]={a1,a2,a3}; void print (int *a[]){ printf("%d\n",a[0][2]); printf("%d\n",*a[2]); printf("%d\n",*++a[0]);//a[0] will now point from a1[0]to a1[1] printf("%d\n",*(++a)[0]);//a will now point from a1 to a2 printf("%d\n",a[-1][1]);//a[-1] is now a1 a[-1][1]= a1[1+1]=a[2] } main(){ print(x); } // output 8 -12 7 23 8
int a[]={1,2,3,4,5,6,7,8}; main(){ int *p=&a[4]; int *q=&a[0],i; printf("BEFORE MODIFICATION\n"); for(i=0;i<8;i++)printf(" a[%d]=%d\n",i,a[i]); printf("Exectuted following statements\n p=&a[4];q=&a[0];\n"); printf("*(q++)=*(p++)+100; *++q=*++p;\n");
printf(" AFTER MODIFICATION\n");
for(i=0;i<8;i++)printf(" a[%d]=%d\n",i,a[i]); getchar(); printf(" NOW "); printf("*q=%d\n",*q);
printf(" AFTER THE MODIFICATION OF"); printf(" *++q=*++p;\n"); for(i=0;i<8;i++)printf(" a[%d]=%d\n",i,a[i]); printf("*q=%d\n",*q); }
{ <syntaxhighlight lang="c">
main(){ char h[12]="hello,world"; printf("%s\n",h); printf("%10s\n",h); printf("%.10s\n",h); printf("%-10s\n",h); printf("%.15s\n",h); printf("%15.10s\n",h); printf("%-15.10s\n",h); } </syntaxhighlight> |type="{}" /}
{
<syntaxhighlight lang="c">
//arithmetic operation on pointer is allowed.this program will print 4 main(){ int *p,*q; int a[10],b=4; p=(int *)malloc(sizeof(int)); q=(int *)malloc(sizeof(int)); p=a; q=&a[4]; //printf("%-*d",b,(q-p)); printf("%-4d",(q-p));//both printfs are equivalent } </syntaxhighlight> |type="{}" /}
{
<syntaxhighlight lang="c">
//function with variable number of arguments in c int FindMax ( int amount, ...) {
int i,val,greater; va_list vl; va_start(vl,amount); greater=va_arg(vl,int); printf("%d\n",greater); for (i=1;i<amount;i++) { val=va_arg(vl,int);
printf("%d\n",val);
greater=(greater>val)?greater:val; }
va_end(vl);
va_start(vl,amount);
greater=va_arg(vl,int); printf("%d\n",greater); for (i=1;i<amount;i++) { val=va_arg(vl,int);
printf("%d\n",val);
greater=(greater>val)?greater:val; }
va_end(vl); return greater;
}
int main () {
int m; m= FindMax (7,702,422,631,834,892,104,772); printf ("The greatest one is: %d\n",m); m= FindMax (4,702,422,631,834); printf ("The greatest one is: %d\n",m); return 0;
} </syntaxhighlight> |type="{}" /}
{
<syntaxhighlight lang="c">
//program showing bit integers.flag will have a size 4 struct flag{ unsigned int key:1; unsigned int sta:1; char c; unsigned int glo:1; unsigned int glo1:1; }; struct node{ int x; char c; int y; int z; }; struct node1{ int x; int y; int z; char c; }; struct charx{ char a; }; main(){ printf("flag size=%d\n",sizeof(struct flag)); printf("short size=%d\n",sizeof(short)); printf("long size=%d\n",sizeof(long)); printf("node size=%d\n",sizeof(struct node)); printf("node1 size=%d\n",sizeof(struct node1)); printf("charx size=%d\n",sizeof(struct charx)); } </syntaxhighlight> |type="{}" /}
{
<syntaxhighlight lang="c">
main(){ char a[]="gate2011"; printf("\n%s\n", a+a[3]-a[1]); } </syntaxhighlight> |type="{}" /}
{
<syntaxhighlight lang="c">
unsigned int foo (unsigned int n, unsigned int r) { if (n >0) return n%r+ foo( n / r, r ); else return 0; } main(){ printf("\nfoo(513,2)=%d\n",foo(513,2)); printf("\nfoo(345,10)=%d\n",foo(345,10)); } </syntaxhighlight> |type="{}" /}
{
<syntaxhighlight lang="c">
void f (int *p, int * q) {
p=q; *q=2;
} int i = 0, j =1; int main ( ){ f(&i,&j); printf ("i=%d j=%d \n", i, j) ; return 0; } </syntaxhighlight> |type="{}" /}
{
<syntaxhighlight lang="c">
int f(int * a, int n){ if (n<= 0)return 0; else if(*a% 2== 0) return *a + f(a+1,n-1);
else return *a-f(a+1, n-1); }
int main ( ) {
int a[ ] = {12, 7, 13, 4, 11, 6};
printf ("\nf(a,6)=%d\n", f(a,6)); return 0; } </syntaxhighlight> |type="{}" /}
{
<syntaxhighlight lang="c">
main(){ char inChar = 'A'; switch ( inChar ) { case 'A' : printf("Choice A\n") ; case 'B' : case 'C' : printf("Choice B") ; case 'D' : case 'E' : default : printf( "No Choice" ) ;
}
} </syntaxhighlight> |type="{}" /}
{
<syntaxhighlight lang="c">
struct node{ struct node *left; struct node *right; int val; };
typedef struct node * treeptr; struct node *insert(struct node *root,int val){ if(root==NULL){ root=(struct node *)malloc(sizeof(struct node)); root->left=root->right=NULL; root->val=val; return root; } if(root->val>val)root->left=insert(root->left,val); if(root->val<val)root->right=insert(root->right,val); if(root->val==val)printf("NODE ALREADY PRESENT\n"); return root; }
int max(int a,int b){ if(a>b)return a; return b; } int height (treeptr n){ int h1,h2;
if (n == NULL) return -1;
if (n->left == NULL){ if (n->right == NULL) return 0; else return 1+height(n->right); } else { h1 = height (n->left); if (n->right == NULL) return (1+h1); else { h2 = height (n->right); } return 1+max(h1,h2); } }
main(){ int val; struct node *root=NULL; printf("Enter the elements to be inserted to BST\n"); while(scanf("%d",&val)!=EOF)root=insert(root,val); printf("height of tree=%d",height(root)); } </syntaxhighlight> |type="{}" /}
{
<syntaxhighlight lang="c">
int a, b, c = 0; void prtFun(void); main( ) { static int a = 1;/*line1*/ //auto int a = 1;/*Line 1 */
prtFun( ); a+=1; prtFun(); printf("\n %d %d", a, b); } void prtFun(void) { static int a = 2;/*Line 2 */ //register int a = 2;/*Line 2 */ int b = 1; a+=++b; printf("\n %d %d" , a, b); }
int f (int &x, int c) { c = c - 1; if (c==0) return 1; x = x + 1; return f(x,c) * x; } main(){ int p=5; printf("\np=5f(p,p)%d\n",f(p,p)); } </syntaxhighlight> |type="{}" /}
{ What is the output of the following program?
<syntaxhighlight lang="c">
int fib(int n){ if(n<=2)return 1; else return fib(n-1)+fib(n-2); } main(){ int n; printf("enter the number "); scanf("%d",&n); printf("fib(%d)=%d",n,fib(n)); } </syntaxhighlight> |type="{}" /} { Abc }
</quiz>