this is my new blog this blog is better then my others blog because i add all programms add in categories
cheak out once
more programms
http://chelpdesk.blogspot.com/
c++ programming tutorials | education | education international | university | TOP WORLD UNIVERSITY 2014 |education wold
Wednesday, 26 February 2014
Tuesday, 25 February 2014
to find out transport of a matrix
//Write a c program to find out transport of a matrix
#include<stdio.h>
int main(){
int a[10][10],b[10][10],i,j,k=0,m,n;
printf("\nEnter the row and column of matrix");
scanf("%d %d",&m,&n);
printf("\nEnter the First matrix->");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("\nThe matrix is\n");
for(i=0;i<m;i++){
printf("\n");
for(j=0;j<m;j++){
printf("%d\t",a[i][j]);
}
}
for(i=0;i<m;i++)
for(j=0;j<n;j++)
b[i][j]=0;
for(i=0;i<m;i++){
for(j=0;j<n;j++){
b[i][j]=a[j][i];
printf("\n%d",b[i][j]);
}
}
printf("\n\nTraspose of a matrix is -> ");
for(i=0;i<m;i++){
printf("\n");
for(j=0;j<m;j++){
printf("%d\t",b[i][j]);
}
}
return 0;
}
int main(){
int a[10][10],b[10][10],i,j,k=0,m,
printf("\nEnter the row and column of matrix");
scanf("%d %d",&m,&n);
printf("\nEnter the First matrix->");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("\nThe matrix is\n");
for(i=0;i<m;i++){
printf("\n");
for(j=0;j<m;j++){
printf("%d\t",a[i][j]);
}
}
for(i=0;i<m;i++)
for(j=0;j<n;j++)
b[i][j]=0;
for(i=0;i<m;i++){
for(j=0;j<n;j++){
b[i][j]=a[j][i];
printf("\n%d",b[i][j]);
}
}
printf("\n\nTraspose of a matrix is -> ");
for(i=0;i<m;i++){
printf("\n");
for(j=0;j<m;j++){
printf("%d\t",b[i][j]);
}
}
return 0;
}
program which takes password from user
Write a c program which takes password from user
#include<stdio.h>
#define MAX 500
#define MAX 500
int main(){
char password[MAX];
char p;
int i=0;
char p;
int i=0;
printf("Enter the password:");
while((p=getch())!= 13){
password[i++] = p;
printf("*");
}
while((p=getch())!= 13){
password[i++] = p;
printf("*");
}
password[i] = '\0';
if(i<6)
printf("\nWeak password");
printf("\nWeak password");
printf("\nYou have entered: %s",password);
return 0;
}
}
Sample output:
Enter the password:*******
You have entered: fgt67m,
Enter the password:*******
You have entered: fgt67m,
Write a scanf function in c which accept sentence from user
Write a scanf function in c which accept sentence from user
#include<stdio.h>
#define MAX 500
#define MAX 500
int main(){
char arr[MAX];
printf("Enter any sentence which can include spaces.\n");
printf("To exit press enter key.\n");
scanf("%[^\n]s",arr);
printf("To exit press enter key.\n");
scanf("%[^\n]s",arr);
printf("You had entered: \n");
printf("%s",arr);
printf("%s",arr);
return 0;
}
}
Sample output:
Enter any sentence which can include spaces.
To exit press enter key.
this code make by ameerhamza my blog letsdoprogamminginc.bloagspot.com
You had entered:
this code make by ameerhamza my blog letsdoprogamminginc.bloagspot.com
To exit press enter key.
this code make by ameerhamza my blog letsdoprogamminginc.bloagspot.com
You had entered:
this code make by ameerhamza my blog letsdoprogamminginc.bloagspot.com
Write a scanf function in c which accept paragraph from user
Write a scanf function in c which accept paragraph from user
#include<stdio.h>
#define MAX 500
#define MAX 500
int main(){
char arr[MAX];
printf("Enter any paragraph which can include spaces or new line.\n");
printf("To exit press the tab key.\n");
scanf("%[^\t]s",arr);
printf("To exit press the tab key.\n");
scanf("%[^\t]s",arr);
printf("You had entered: \n");
printf("%s",arr);
printf("%s",arr);
return 0;
}
}
Sample output:
Enter any paragraph which can include spaces or new line.
To exit, press the tab key.
C is powerful language.
To exit, press the tab key.
C is powerful language.
Print prime numbers between 1-300 using break and continue in c
Print prime numbers between 1-300 using break and continue in c
Definition of prime number:
A natural number greater than one has not any other divisors except 1 and itself. In other word we can say which has only two divisors 1 and number itself. For example: 5
Their divisors are 1 and 5.
Their divisors are 1 and 5.
Note: 2 is only even prime number.
Example of prime numbers : 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199 etc.
#include <math.h>
#include <stdio.h>
main(){
int i, j;
i = 2;
while ( i < 300 ){
j = 2;
while ( j < sqrt(i) ){
if ( i % j == 0 )
break;
else{
++j;
continue;
}
}
if ( j > sqrt(i) )
printf("%d\t", i);
++i;
}
return 0;
}
#include <stdio.h>
main(){
int i, j;
i = 2;
while ( i < 300 ){
j = 2;
while ( j < sqrt(i) ){
if ( i % j == 0 )
break;
else{
++j;
continue;
}
}
if ( j > sqrt(i) )
printf("%d\t", i);
++i;
}
return 0;
}
Palindrome in c without using string function
Palindrome in c without using string function
#include<stdio.h>
int main(){
char str[100];
int i=0,j=-1,flag=0;
char str[100];
int i=0,j=-1,flag=0;
printf("Enter a string: ");
scanf("%s",str);
scanf("%s",str);
while(str[++j]!='\0');
j--;
j--;
while(i<j)
if(str[i++] != str[j--]){
flag=1;
break;
}
if(str[i++] != str[j--]){
flag=1;
break;
}
if(flag == 0)
printf("The string is a palindrome");
else
printf("The string is not a palindrome");
printf("The string is a palindrome");
else
printf("The string is not a palindrome");
return 0;
}
}
How to get the ASCII value of a character in c
How to get the ASCII value of a character in c
#include<stdio.h>
int main(){
char c;
char c;
printf("Enter any character: ");
scanf("%c",&c);
scanf("%c",&c);
printf("ASCII value of given character: %d",c);
return 0;
}
return 0;
}
Sample output:
Enter any character: a
ASCII value of given character: 97
Enter any character: a
ASCII value of given character: 97
Sunday, 23 February 2014
Ramdom number generator
Ramdom number generator
how to gnerator ramdom number in c
printf("this code woke only turbo c++");
#include<stdio.h>
int main()
{
int n,max,num,c;
printf("Enter the number of random number you want");
scanf("%d",&n);
printf("Enter the maxmum value of random number\n");
scanf("%d",&max);
printf("Randem numer from 0 to %d are \n",n,max);
randomize();
for(c=1;c<=n;c++)
{
num=random(max);
printf("%d\n",num);
}
}
how to gnerator ramdom number in c
printf("this code woke only turbo c++");
#include<stdio.h>
int main()
{
int n,max,num,c;
printf("Enter the number of random number you want");
scanf("%d",&n);
printf("Enter the maxmum value of random number\n");
scanf("%d",&max);
printf("Randem numer from 0 to %d are \n",n,max);
randomize();
for(c=1;c<=n;c++)
{
num=random(max);
printf("%d\n",num);
}
}
pointer programms
check given string palindrom number or not
#include<stdio.h>
#include<string.h>
int main()
{
char*str,*rev;
char i,j;
printf("\nEntr a string");
scanf("%s",str);
for(i=strlen(str)-1,j=0;j>=0;i--,j++)
rev[j]=str[i];
rev[j]='\0';
if(strcmp(rev,str))
printf("\nThe string is not a palindrme");
else
printf("\nthe string is palindrom");
return 0;
}
//Hexdecimal Equivalent using pointer
#include<stdio.h>
#include<conio.h>
//#include<alloc.h>
int main()
{
int num,rem[20],*ptr;
printf("Enter number:\n");
scanf("%d",&num);
ptr=rem;
while(num!=0)
{
*ptr=num%16;
num=num/16;
ptr++;
}
ptr--;
while(ptr>=rem){
if(*ptr<10)
printf("%d",*ptr);
else
printf("%c",*ptr+55);
ptr--;
}
}
#include<stdio.h>
#include<conio.h>
int main()
{
long *n,*n1,*n2,*m1,*m2,r;
printf("this code by ameer hamza if u want more programm plesse vist my blog\n");
n=(long*)malloc(sizeof(long));
n1=(long*)malloc(sizeof(long));
n2=(long*)malloc(sizeof(long));
m1=(long*)malloc(sizeof(long));
m2=(long*)malloc(sizeof(long));
printf("\nEnter n ");
scanf("%ld",n);
*n1=*n2=0;
*m1=*m2=1;
while(*n!=0)
{
r=*n%10;
*n=*n/10;
if(r%2==0)//even
{
r=r * *m2;
*n2=*n2 + r;
*m2=*m2 * 10;
}//if
else
{
r=r* *m1;
*n1=*n1 + r;
*m1=*m1 * 10;
}//else
}//while
printf("http//letsdoprogramminginc.blogspot.com\n");
printf("\n n1=%ld n2=%ld",*n1,*n2);
}
//reverse the content of array using pointer at variable loacation
#include<stdio.h>
#include<conio.h>
int main()
{
int a[20],*ptr1,*ptr2,n,t;
printf("\nEnter n ");
scanf("%d",&n);
//read n number
printf("Enter %d number\n",n);
for(ptr1=a;ptr1<=a+n;ptr1++)
{
scanf("%d",ptr1);
}
//reverse the odder of n numbers
ptr1=a;
ptr2=a+n-1;
while(ptr1<ptr2)
{
t=*ptr1;
*ptr1=*ptr2;
ptr1++;
ptr2--;
}
printf("\n the resultant array is \n");
for(ptr1=a;ptr1<=a+n-1;ptr1++)
{
printf("%4d",*ptr1);
}
printf("for more programms vist my blog\n");
printf("letsdoprogramminginc.blogspot.com");
}
//reverse the content of array using pointer at fixed loacation
#include<stdio.h>
#include<conio.h>
int main()
{
int *ptr,n,i,j,t,a[100];
printf("Enter n");
scanf("%d",&n);
ptr=a;
printf("\nEnter %d number \n",n);
for(i=0;i<n;i++)
{
scanf("%d",ptr+i);
}
i=0;
j=n-1;
while(i<j)
{
t=*(ptr+1);
*(ptr+i)=*(ptr+j);
*(ptr+j)=t;
i++;
j++;
}
printf("the resultant array is \n");
for(i=0;i<n;i++)
{
printf("%4d",*(ptr+1));
}
}
#include<stdio.h>
#include<string.h>
int main()
{
char*str,*rev;
char i,j;
printf("\nEntr a string");
scanf("%s",str);
for(i=strlen(str)-1,j=0;j>=0;i--,j++)
rev[j]=str[i];
rev[j]='\0';
if(strcmp(rev,str))
printf("\nThe string is not a palindrme");
else
printf("\nthe string is palindrom");
return 0;
}
//Hexdecimal Equivalent using pointer
#include<stdio.h>
#include<conio.h>
//#include<alloc.h>
int main()
{
int num,rem[20],*ptr;
printf("Enter number:\n");
scanf("%d",&num);
ptr=rem;
while(num!=0)
{
*ptr=num%16;
num=num/16;
ptr++;
}
ptr--;
while(ptr>=rem){
if(*ptr<10)
printf("%d",*ptr);
else
printf("%c",*ptr+55);
ptr--;
}
}
#include<stdio.h>
#include<conio.h>
int main()
{
long *n,*n1,*n2,*m1,*m2,r;
printf("this code by ameer hamza if u want more programm plesse vist my blog\n");
n=(long*)malloc(sizeof(long));
n1=(long*)malloc(sizeof(long));
n2=(long*)malloc(sizeof(long));
m1=(long*)malloc(sizeof(long));
m2=(long*)malloc(sizeof(long));
printf("\nEnter n ");
scanf("%ld",n);
*n1=*n2=0;
*m1=*m2=1;
while(*n!=0)
{
r=*n%10;
*n=*n/10;
if(r%2==0)//even
{
r=r * *m2;
*n2=*n2 + r;
*m2=*m2 * 10;
}//if
else
{
r=r* *m1;
*n1=*n1 + r;
*m1=*m1 * 10;
}//else
}//while
printf("http//letsdoprogramminginc.blogspot.com\n");
printf("\n n1=%ld n2=%ld",*n1,*n2);
}
//reverse the content of array using pointer at variable loacation
#include<stdio.h>
#include<conio.h>
int main()
{
int a[20],*ptr1,*ptr2,n,t;
printf("\nEnter n ");
scanf("%d",&n);
//read n number
printf("Enter %d number\n",n);
for(ptr1=a;ptr1<=a+n;ptr1++)
{
scanf("%d",ptr1);
}
//reverse the odder of n numbers
ptr1=a;
ptr2=a+n-1;
while(ptr1<ptr2)
{
t=*ptr1;
*ptr1=*ptr2;
ptr1++;
ptr2--;
}
printf("\n the resultant array is \n");
for(ptr1=a;ptr1<=a+n-1;ptr1++)
{
printf("%4d",*ptr1);
}
printf("for more programms vist my blog\n");
printf("letsdoprogramminginc.blogspot.com");
}
//reverse the content of array using pointer at fixed loacation
#include<stdio.h>
#include<conio.h>
int main()
{
int *ptr,n,i,j,t,a[100];
printf("Enter n");
scanf("%d",&n);
ptr=a;
printf("\nEnter %d number \n",n);
for(i=0;i<n;i++)
{
scanf("%d",ptr+i);
}
i=0;
j=n-1;
while(i<j)
{
t=*(ptr+1);
*(ptr+i)=*(ptr+j);
*(ptr+j)=t;
i++;
j++;
}
printf("the resultant array is \n");
for(i=0;i<n;i++)
{
printf("%4d",*(ptr+1));
}
}
how to pass dimensional array in to fuction in c
how to pass dimensional array in to fuction in c
#include<stdio.h>
#define n 5
void fstore1d(int a[],int a_size);
void fretrieve1d(int a[],int a_size);
void feditid(int a[],int a_size);
int main()
{
int a[n];
printf("intput data into the matrix:\n");
fstore1d(a,n);
fetrieve1d(a,n);
fedit1d(a,n);
fretrieve1d(a,n);
return 0;
}
void fstore1d(int a[],int n)
{
int i;
for(i=o;i<n;++i)
scanf("%d",&a[i]);
}
void fretrieve1d(int a[],int n)
{
int i;
for(i=0;i<n;++i)
printf("%6d",a[i]);
printf("\n");
}
void fedit1d(int a[],int n)
{
int i,q;
for(i=0;i<n;++i)
printf("prev data :%d\nEnter 1 to edit o to skip",a[i]);
if(q==1)
{
printf("Enter new value");
scanf("%d",&a[i]);
}
}
#include<stdio.h>
#define n 5
void fstore1d(int a[],int a_size);
void fretrieve1d(int a[],int a_size);
void feditid(int a[],int a_size);
int main()
{
int a[n];
printf("intput data into the matrix:\n");
fstore1d(a,n);
fetrieve1d(a,n);
fedit1d(a,n);
fretrieve1d(a,n);
return 0;
}
void fstore1d(int a[],int n)
{
int i;
for(i=o;i<n;++i)
scanf("%d",&a[i]);
}
void fretrieve1d(int a[],int n)
{
int i;
for(i=0;i<n;++i)
printf("%6d",a[i]);
printf("\n");
}
void fedit1d(int a[],int n)
{
int i,q;
for(i=0;i<n;++i)
printf("prev data :%d\nEnter 1 to edit o to skip",a[i]);
if(q==1)
{
printf("Enter new value");
scanf("%d",&a[i]);
}
}
find the value of cosx
find the value of cosx
#include<stdio.h>
int main()
{
int n,x1;
float acc,term,den,x,cosx=0,cosval;
printf("Enter the value of x (in degrees)\n");
scanf("%f",&x);
x1=x;
//converting degrees to radians
x=x*(3.142/180.0);
cosval=cos(x);
printf("Enter the accuay for the result \n");
scanf("%f",&acc);
term=1;
cosx=term;
n=1;
do
{
den=2*n*(2*n-1);
term=-term*x*x/den;
cosx=cosx+term;
n=n+1;
}
while(acc<=fabs(cosval-cosx));
printf("sum of cosine series=%f\n",cosx);
printf("using library fuction cos(%d)=%d\n",x1,cos(x));
return 0;
}
#include<stdio.h>
int main()
{
int n,x1;
float acc,term,den,x,cosx=0,cosval;
printf("Enter the value of x (in degrees)\n");
scanf("%f",&x);
x1=x;
//converting degrees to radians
x=x*(3.142/180.0);
cosval=cos(x);
printf("Enter the accuay for the result \n");
scanf("%f",&acc);
term=1;
cosx=term;
n=1;
do
{
den=2*n*(2*n-1);
term=-term*x*x/den;
cosx=cosx+term;
n=n+1;
}
while(acc<=fabs(cosval-cosx));
printf("sum of cosine series=%f\n",cosx);
printf("using library fuction cos(%d)=%d\n",x1,cos(x));
return 0;
}
c program to find the roots of a quadratic equation
c program to find the roots of a quadratic equation
c program to find the roots of a quadratic equation using if else
here is c program witch is calculate the root of Quadric equation
find the roots of a Quadratic Equation#include<stdio.h>
int main()
{
float a,b,c,d,realp,imgp,r1,r2;
printf("Enter the 3 number \n");
scanf("%f %f %f",&a,&b,&c);
if(a==0|| b==0|| c==0)
{
printf("Error the input only non zero numbers\n");
}
else
{
d=b*b-4*a*c;
if(d==0)
{
printf("Rootre are Equal \n");
r1=r2=-b/(2*a);
printf("root 1=%f,root2=%f",r1,r2);
}
else if(d>0)
{
printf("roots are real & distinct\n ");
r1=(-b+sqrt(fabs(d)))/(2*a);
r2=(-b-sqrt(fabs(d)))/(2*a);
printf("root1=%f,root2=%f",r1,r2);
}
else
{
printf("Root are a imaginary \n");
realp=-b/(2*a);
imgp=sqrt(fabs(d))/(2*a);
printf("root1=%f+i%f,%f-i%f",realp,imgp,realp,imgp);
}
}
}
count the digit in a number
c programm count the digit in a number
#include<stdio.h>
int main()
{
int num ,conut=0;
printf("this code codedd by ameer hamza for more programming ckeak out my blog\n");
printf("http//letsdoprogramminginc.blodspot.com\n");
printf("Enter the number ");
scanf("%d",&num);
while(num)
{
num=num/10;
conut++;
}
printf("total, digtis is %d ",conut);
return 0;
}
#include<stdio.h>
int main()
{
int num ,conut=0;
printf("this code codedd by ameer hamza for more programming ckeak out my blog\n");
printf("http//letsdoprogramminginc.blodspot.com\n");
printf("Enter the number ");
scanf("%d",&num);
while(num)
{
num=num/10;
conut++;
}
printf("total, digtis is %d ",conut);
return 0;
}
c program Frequency of Array Elements
c program Frequency of Array Elements
in this c program we find the Frequency of Array Elements this programm tell us Frequency of Array Elementsconnting the Frequencies of Element of array
#include<stdio.h>
#define s 6
int main()
{
int a[s],freq[s];
int i,j,k,n=s;
for(i=0;i<s;i++)
{
printf("\n Enter a[%d] Element ",i);
scanf("%d",&a[i]);
freq[i]=1;
}
printf("Orignal array\n");
for(i=0;i<s;i++)
printf("%d",a[i]);
//main logic stats here
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
{
if(a[i]==a[j])
{
for(k=j;k<n;k++)
a[k]=a[k+1];
freq[i]++;
n--;
}
}
printf("\n array with freq\n");
printf("\n Elemetnt freq");
for(i=0;i<n;i++)
printf("%d %d\n",a[i],freq[i]);
}
c program convert temperature fahrenheit celsius degree
programm for ATM transtions
//c programm for ATM transtions
#include <stdio.h>
#include <stdlib.h>
int totalthousand=1000;
int totalfivefundred=100;
int totalonehundred=1000;
int main()
{
printf("code make by ameer hamza\nfor more help programming chake out my blog");
printf("letsdoprogramminginc.blodspot.com\n");
unsigned long withdrawamount;
unsigned longmomeny;
int totalmoney,
thousand=0,fivehundred=0,onehundred=0;
printf("enter the amount in multiply 0f 100:\n");
scanf("%lu",&withdrawamount);
if(withdrawamount%100!=0)
{
printf("invalid amount");
return 0;
}
totalmoney=totalthousand*1000+totalfivefundred*500+totalonehundred*100;
if(withdrawamount>totalmoney)
{
printf("sorrey,insuffcint money");
return 0;
}
thousand=withdrawamount/1000;
if(thousand>totalthousand)
thousand=totalthousand;
withdrawamount=withdrawamount-thousand*1000;
if(withdrawamount>0)
{
fivehundred=withdrawamount/500;
if(fivehundred>totalfivefundred)
fivehundred=totalfivefundred;
withdrawamount=withdrawamount-fivehundred*500;
}
if(withdrawamount>0 )
onehundred=withdrawamount/100;
printf("total 1000 note:%d\n",thousand);
printf("total 500 note:%d\n",fivehundred);
printf("total 100 note:%d\n",onehundred);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int totalthousand=1000;
int totalfivefundred=100;
int totalonehundred=1000;
int main()
{
printf("code make by ameer hamza\nfor more help programming chake out my blog");
printf("letsdoprogramminginc.blodspot.com\n");
unsigned long withdrawamount;
unsigned longmomeny;
int totalmoney,
thousand=0,fivehundred=0,onehundred=0;
printf("enter the amount in multiply 0f 100:\n");
scanf("%lu",&withdrawamount);
if(withdrawamount%100!=0)
{
printf("invalid amount");
return 0;
}
totalmoney=totalthousand*1000+totalfivefundred*500+totalonehundred*100;
if(withdrawamount>totalmoney)
{
printf("sorrey,insuffcint money");
return 0;
}
thousand=withdrawamount/1000;
if(thousand>totalthousand)
thousand=totalthousand;
withdrawamount=withdrawamount-thousand*1000;
if(withdrawamount>0)
{
fivehundred=withdrawamount/500;
if(fivehundred>totalfivefundred)
fivehundred=totalfivefundred;
withdrawamount=withdrawamount-fivehundred*500;
}
if(withdrawamount>0 )
onehundred=withdrawamount/100;
printf("total 1000 note:%d\n",thousand);
printf("total 500 note:%d\n",fivehundred);
printf("total 100 note:%d\n",onehundred);
return 0;
}
Saturday, 8 February 2014
difference between ++*p, *p++ and *++p
difference between ++*p, *p++ and *++p
Predict the output of following C programs.
The output of above programs and all such programs can be easily guessed by remembering following simple rules about postfix ++, prefix ++ and * (dereference) operators
1) Precedence of prefix ++ and * is same. Associativity of both is right to left.
2) Precedence of postfix ++ is higher than both * and prefix ++. Associativity of postfix ++ is left to right.
(Refer: Precedence Table)
The expression ++*p has two operators of same precedence, so compiler looks for assoiativity. Associativity of operators is right to left. Therefore the expression is treated as ++(*p). Therefore the output of first program is “arr[0] = 11, arr[1] = 20, *p = 11“.
The expression *p++ is treated as *(p++) as the precedence of postfix ++ is higher than *. Therefore the output of second program is “arr[0] = 10, arr[1] = 20, *p = 20“.
The expression *++p has two operators of same precedence, so compiler looks for assoiativity. Associativity of operators is right to left. Therefore the expression is treated as *(++p). Therefore the output of second program is “arr[0] = 10, arr[1] = 20, *p = 20“.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
// PROGRAM 1 #include <stdio.h> int main( void ) { int arr[] = {10, 20}; int *p = arr; ++*p; printf ( "arr[0] = %d, arr[1] = %d, *p = %d" , arr[0], arr[1], *p); return 0; } |
// PROGRAM 2 #include <stdio.h> int main( void ) { int arr[] = {10, 20}; int *p = arr; *p++; printf ( "arr[0] = %d, arr[1] = %d, *p = %d" , arr[0], arr[1], *p); return 0; } |
// PROGRAM 3 #include <stdio.h> int main( void ) { int arr[] = {10, 20}; int *p = arr; *++p; printf ( "arr[0] = %d, arr[1] = %d, *p = %d" , arr[0], arr[1], *p); return 0; } |
1) Precedence of prefix ++ and * is same. Associativity of both is right to left.
2) Precedence of postfix ++ is higher than both * and prefix ++. Associativity of postfix ++ is left to right.
(Refer: Precedence Table)
The expression ++*p has two operators of same precedence, so compiler looks for assoiativity. Associativity of operators is right to left. Therefore the expression is treated as ++(*p). Therefore the output of first program is “arr[0] = 11, arr[1] = 20, *p = 11“.
The expression *p++ is treated as *(p++) as the precedence of postfix ++ is higher than *. Therefore the output of second program is “arr[0] = 10, arr[1] = 20, *p = 20“.
The expression *++p has two operators of same precedence, so compiler looks for assoiativity. Associativity of operators is right to left. Therefore the expression is treated as *(++p). Therefore the output of second program is “arr[0] = 10, arr[1] = 20, *p = 20“.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Subscribe to:
Posts (Atom)