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 7587205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:36:53+00:00 2026-05-30T19:36:53+00:00

A few years back i was working with Turbo C compiler and the following

  • 0

A few years back i was working with Turbo C compiler and the following code worked fine on it.

#include<stdio.h>
void main()
{
     int a=2,b=3;
     swap(a,b);
     printf("%d\n%d\n",a,b);
}

swap(int *x,int *y)
{
     int t;
     //x=malloc(2);
     //y=malloc(2);
     t=*x;
     *x=*y;
     *y=t;
     printf("%d\n%d\n",x,y);
}

Now i am working on cygwin and if i run this code i get an error Segmentation fault(core dumped)
If i uncomment the malloc statements i get the output

536937064
536937080
2
3

Are first two lines of output some garbage values? What exactly is happening here and how can i get the correct output?

  • 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-05-30T19:36:54+00:00Added an answer on May 30, 2026 at 7:36 pm

    Here is the corrected version of your program, which will execute correctly.

    There are a number of things going wrong in the sample you posted:

    Incorrect Argument type being passed:

    swap(a,b); 
    

    should be :

    swap(&a,&b); 
    

    Your function expects pointer to the integers to be modified, You are not doing so.


    Incorrect Format specifiers for printf:

    printf("%d\n%d\n",x,y); 
    

    should be:

    printf("%d\n%d\n",*x,*y); 
    

    printf is not type safe and you need to ensure you use proper format specifiers while using it. Using incorrect format specifiers results in Undefined Behavior.


    The next two are good practices if not errors and you should follow them.

    Incorrect return type of main():

    As per the Standard a program should return int,

    void main()
    

    should be

    int main()
    

    Also, add return a value return 0;at the end of main.


    Function Declaration:

    You should declare the function swap() correctly before main:

    void swap(int *x,int *y); 
    

    Providing a function declaration before the place where you use it in code, gives the compiler a opportunity to match the parameters and report incorrect types being passed.


    Further using malloc as you have would not acheive what you are trying to achieve, You do not need to use malloc here at all and One should always avoid using it as much as possible.

    Also, You should pick up a good book and learn the basics.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following question was asked in one of my interview (few years back) What
I've been working almost exclusively on back-end tasks for the past few years, and
I'm working with a WCF service I wrote a few years back, which uses
A few years back I worked in a Sybase/Delphi environment, using the BDE to
A few years back, I have adopted the following pattern for all except the
a few years back in college I wrote TONS of simple little java programs...
I remember reading something about it a few years back but I don't remember
I remember seeing a poster a few years back that had a nice break
I learnt HTML/CSS a good few years back, then PHP a little later. I've
I wrote a nice ARM assembler routine a few years back and it has

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.