karthikmunna karthikmunna - 3 months ago 11
C Question

How the two programmes works in c

1.First Programme

void main()
{
int x=30;
printf("%d",printf("%d",x))
}


2.Second Programme

void main()
{
char *x="abcdef";
x++;
printf("%s",x);
}


I Need to know how this code is working
the output for the first code is 302
And for second code is bcdf
how a character pointer sorting the address of an string

LPs LPs
Answer

First of all First code must be corrected

#include <stdio.h>

int main(void)
{
   int x=30;
   printf("%d\n",printf("%d",x));
}

This code print 302 due to:

  • The nested call to printf prints the value of x that is 30
  • the outer printf prints the returned value of nested printf, that is 2. Aa the man says

    Return value

    Upon successful return, these functions return the number of characters printed (excluding the null byte used to end output to strings).

Second code that must be corrected too:

#include <stdio.h>

int main(void)
{
   char *x="abcdef";
   x++;
   printf("%s\n",x);
}

Is too simple to be explained and you should take a look at a good C book and study something about pointers.

Comments