Last active
March 4, 2022 14:38
-
-
Save skrishnakanth/77cac7d38892b3633494ef17d62ecc3a to your computer and use it in GitHub Desktop.
C programs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// [binary-Hex-Octal-dec] conversions | |
//10101010 | |
#include <stdio.h> | |
int main() | |
{ | |
unsigned long int val=10101011; | |
long int rem=0x00,i=1,out=0; | |
while(val !=0) | |
{ | |
rem= val%10; | |
// printf("val:%x\n",val); | |
// printf("reminder:%d\n",rem); | |
out=out+(rem*i); | |
i=i*2; | |
val=val/10; | |
} | |
printf("the DEC val:%d\n",out); | |
printf("the HEX val:%X\n",out); | |
printf("the OCT val:%o",out); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int main() | |
{ | |
unsigned long int input=0,result =0; | |
int clear_bit=2; | |
input=0xff; | |
result=input; | |
result&=~(1<<clear_bit); // result|=(1<<clear_bit) // result^=(1<<clear_bit) | |
clear_bit+=4; | |
result&=~(1<<clear_bit); | |
printf("the resutl:%lo",result); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> //Diamond Pattern !!! | |
void main() | |
{ | |
int i=0,j=0,k=0,space=0,dec=9,inc=1,q=0; | |
for(k=5;k>0;k--) | |
{ | |
dec-=2; | |
for(space=dec;space>=0;space--) | |
{ | |
printf(" "); | |
} | |
for(i=1;i<=inc;i++) | |
{ | |
printf("%d ",i); | |
} | |
q=i-1; | |
while(q>1) | |
{ | |
printf("%d ",q-1); | |
q--; | |
} | |
inc++; | |
printf("\n"); | |
} | |
dec=-1; | |
inc=5; | |
for(k=5;k>0;k--) | |
{ | |
dec+=2; | |
for(space=dec;space>=0;space--) | |
{ | |
printf(" "); | |
} | |
for(i=1;i<inc;i++) | |
{ | |
printf("%d ",i); | |
} | |
q=i-1; | |
while(q>1) | |
{ | |
printf("%d ",q-1); | |
q--; | |
} | |
inc--; | |
printf("\n"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Right shift and Left shift | |
#include <stdio.h> | |
#include <stdint.h> | |
int main() | |
{ | |
signed int val=16,shift =0,k=1; | |
// shift=(val<<k); | |
for (int i=sizeof(val)*8-1;i>=0;i--) | |
{ | |
printf("%d",(val & (1<<i)) >>i); | |
if(i%4 == 0) | |
printf(" "); | |
} | |
//printf("output:%d",shift); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdint.h> | |
union check{ | |
uint8_t a; | |
uint32_t arr[2]; | |
}; | |
void main() | |
{ | |
union check t1; | |
t1.a=1; | |
if(t1.arr[0] == 1) | |
printf("Little Endian"); | |
else if(t1.arr[0] == 0) | |
printf("Big Endian"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Nibble shift of a value | |
#include <stdio.h> | |
int main() | |
{ | |
unsigned int val=0xab,out=0; | |
out=(val & 0xf0)>>4 |(val & 0x0f)<<4 ; | |
for(int i=sizeof(out)*8-1;i>=0;i--) | |
{ | |
printf("%d",(out & (1<<i)) >> i); | |
if(i%4==0) | |
printf(" "); | |
} | |
printf("\nThe Nibble shift value:%X",out); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 5 \n 5 4 \n 5 4 3 \n 5 4 3 2 \n 5 4 3 2 1 */ | |
#include <stdio.h> | |
void main() | |
{ | |
int i=0,j=0,k=0,count=5; | |
for (i=1;i<=5;i++) | |
{ | |
count--; | |
for(j=5;j>count;j--) | |
{ | |
printf("%d ",j); | |
} | |
printf("\n"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> /* 5 \n 4 5 \n 3 4 5 \n 2 3 4 5 \n 1 2 3 4 5 */ | |
void main() | |
{ | |
int i=0,j=0,count=0; | |
for(int i=5;i>=1;i--) | |
{ | |
count++; | |
for(j=i;j<=5;j++) | |
{ | |
printf("%d ",j); | |
} | |
printf("\n"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> /* 1 2 3 4 5 \n 1 2 3 4 \n 1 2 3 \n 1 2 \n 1 */ | |
void main() | |
{ | |
int i=0,j=0,count=5; | |
for(i=1;i<=5;i++) | |
{ | |
for(j=1;j<=count;j++) | |
{ | |
printf("%d ",j); | |
} | |
count--; | |
printf("\n"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* pointers example a=a+b | |
b=|a-b| | |
*/ | |
#include <stdio.h> | |
#include <math.h> | |
void update(int *a,int *b) { | |
// Complete this function | |
int sum=0,diff=0,n; | |
sum=*a+*b; | |
diff=*a-*b; | |
if(diff*=-1 > diff) //diff=abs(diff); | |
{ | |
diff=diff*-1; | |
} | |
*a=sum; | |
*b=diff; | |
} | |
int main() { | |
int a=5, b=10; | |
int *pa = NULL, *pb = NULL; | |
pa=&a;pb=&b; | |
printf("the address of a:%d and b :%d\n",pa,pb); | |
update(pa, pb); | |
printf("%d\n%d", a, b); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
char* reverse(char *sp) | |
{ | |
int i=0,j=0,count=0; | |
static char test[100]={0}; | |
while(sp[i] !='\0') | |
{ | |
count++; | |
i++; | |
} | |
i=count; | |
while(i>0 ) | |
{ | |
test[j]=sp[i-1]; | |
// sp[j]=test[i-1]; | |
i--; | |
j++; | |
} | |
char *tes=test; | |
return tes; | |
} | |
void main() | |
{ | |
unsigned char val[]="Hello krishnakanth how are you !!!"; | |
char *sp; | |
sp=val; | |
reverse(sp); | |
printf("the string reverse:%s",reverse(sp)); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Reverse a number*/ | |
#include <stdio.h> | |
int main () | |
{ | |
int output=0,input=0,rem=0;; | |
printf("Enter the number:"); | |
scanf("%d",&input); | |
while(input != 0) | |
{ | |
rem=input%10; | |
output=output*10+rem; | |
input=input/10; | |
} | |
printf("\n the output is:%d", output); | |
printf("\n rem:%d", rem); | |
getchar(); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* string reverse */ | |
#include <stdio.h> | |
#include <string.h> | |
void main() | |
{ | |
char input[100]; | |
char output[100]; | |
int count=0,i=0; | |
printf("enter the string:"); | |
scanf("%s",input); | |
int val=strlen(input); | |
while(input[i] != '\0') | |
{ | |
count++; | |
i++; | |
} | |
printf("val:%d\n",val); | |
printf("count:%d\n",count); | |
for(int i=val-1,j=0;i>=0,j<=val-1;i--,j++) | |
output[j]=input[i]; | |
printf("\n"); | |
printf("reverse of the string:"); | |
printf("%s",output); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> // String rev using function call and pointers | |
void strrev(char *ch) | |
{ | |
char rev[100]; | |
int i=0,count=0,set=0; | |
while(ch[i] != '\0') | |
{ | |
// count++; | |
i++; | |
} | |
while(i>0) | |
{ | |
rev[count]=ch[i-1]; | |
i--; | |
count++; | |
} | |
printf("size of a string:%d\n",i); | |
printf("the orginal string:%s\n",ch); | |
printf("the rev of string:%s\n",rev); | |
printf("last val:%d",ch[count]); | |
} | |
void main() | |
{ | |
unsigned char str[]="hello krishnakanth S";char *a; | |
a=str; | |
strrev(a); | |
char a[10]="Hello"; | |
strrev(a); | |
printf("%s",a); //olleH | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdint.h> | |
struct date{ | |
uint16_t d:5; | |
uint8_t m:4; | |
uint16_t y; | |
};//__attribute__((packed)) | |
void main() | |
{ | |
struct date d1; | |
uint8_t size= sizeof(d1); | |
d1.d=15; | |
d1.m=12; | |
d1.y=2021; | |
printf("the date mentioned as: %d/%d/%d.",d1.d,d1.m,d1.y); | |
printf("\nsize of structure:%d", size); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdint.h> | |
union test{ | |
uint32_t a; | |
struct { | |
uint8_t p; | |
uint8_t q; | |
uint8_t r; | |
uint8_t s; | |
}z; | |
struct { | |
uint8_t m:8; | |
uint8_t n:4; | |
uint8_t o:4; | |
uint8_t p:4; | |
uint8_t q:4; | |
}y; | |
struct { | |
uint8_t b:1; | |
}x; | |
}; | |
int main() | |
{ | |
uint16_t a = 0xAB; | |
union test t2; | |
printf("%d\n", sizeof(t2.x)); | |
//1010 1011 | |
union test t1; | |
t1.a = 0xABCDEF11; | |
printf("%x %x %x %x \n", t1.z.p, t1.z.q, t1.z.r, t1.z.s ); | |
printf("%d %d %d %d %d\n", t1.y.m, t1.y.n, t1.y.o,t1.y.p,t1.y.q); | |
// i =7 | |
// 1 | |
// i =6 | |
// 0 == > a & (1<<i) >> i | |
// 0010 0000 | |
for(int i=sizeof(a)*8 -1 ; i>=0; i--) | |
{ | |
printf("%d", (a & (1<<i)) >> i); | |
if(i%4 == 0) | |
printf(" "); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment