Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8795233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:21:35+00:00 2026-06-13T23:21:35+00:00

#include<stdio.h> int main() { int *p=0; char *ch=0; p++; ch++; printf (%d and %d\n,p,ch);

  • 0
#include<stdio.h>
int main()
{
    int *p=0;
    char *ch=0;
    p++;
    ch++;
    printf ("%d and %d\n",p,ch);
    return 0;
}

Output:

4 and 1
  1. I know the char pointer increments as +1 in the address that it is pointing too.

  2. I know the pointer to an int increments as +4 in the address in gcc that it is pointing too.

  3. I know Derefrencing a pointer should be done by the use of * with the pointer.

Queries:

  1. Why is this not giving any garbage value for p and ch as both are pointers and has not assigned any address;

  2. Why is this giving me the address difference that the respective pointer has obtained while incrementing, or is this a undefined behavior.

3.Why is the output 4 and 1?

Pl. Explain.

I have compiled this code on gcc-4.3.4.
Its a C code.
I am sorry if this comes out to be a copy of some question as I was not able to find any such question on stackoverflow.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T23:21:35+00:00Added an answer on June 13, 2026 at 11:21 pm

    First, your code is printing pointers as integers. While this is probably what you’re trying to do, it is not defined behavior, as it is entirely unportable on platforms where the size of a pointer (in bytes) is not the same as the size of int. if you want to print pointer values, use %p instead.

    To answer your questions. You are assigning values to both pointers: 0, which is synonymous with NULL.

    Second. The reason you’re getting 4 1 is due to the size of an int vs the size of a char on your platform. The char is going to be 1. On your platform, anint is 4 bytes wide. When incrementing a pointer the compiler will automatically move the address it references by the byte-count of the underlying type it represents.

    #include<stdio.h>
    int main()
    {
        int *p=0;    // int is 4 bytes on your platform
        char *ch=0;  // char is 1 byte
        p++;         // increments the address in p by 4
        ch++;        // increments the address in ch by 1
        printf ("%d  and %d\n",p,ch);
        return 0;
    }
    

    EDIT: you’re going to get the similar results, but with a supported print statement, do this instead:

    #include<stdio.h>
    int main()
    {
        int *p=0;
        char *ch=0;
        p++;
        ch++;
        printf ("%p and %p\n",p,ch);
        return 0;
    }
    

    Output (on my Mac) is:

    0x4 and 0x1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include <stdio.h> #include <stdlib.h> int main() { char *str=Helloworld; printf(%d,printf(%s,str)); return 0; } output
#include <stdio.h> void wrapperPrint(char* s) { printf(s); return; } int main() { wrapperPrint(Hello world\n);
#include <stdio.h> int main() { char TurHare; while(1) { scanf(%c,TurHare); printf(\nCharacter :%c, TurHare); }
#include <stdio.h> #include <string.h> char* changeString(char *inputString); int main() { printf(Changed string is %s\n,
Code : #include stdio.h #include string.h int main() { char *p = abc; printf(p
Consider this little program: #include <stdio.h> int main() { char c = 0xFF; printf(%d\n,
The following program yields 12480 as the output. #include<stdio.h> int main() { char c=48;
#include<stdio.h> int main() { char a[5]=hello; puts(a); //prints hello } Why does the code
#include<stdio.h> int main() { char *s[] = { knowledge,is,power}; char **p; p = s;
#include <stdio.h> int main() { char read = ' '; while ((read = getchar())

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.