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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T00:53:46+00:00 2026-05-11T00:53:46+00:00

I’m a beginner programmer and I’m learning my first language, C. I’m learning mostly

  • 0

I’m a beginner programmer and I’m learning my first language, C.

I’m learning mostly from Deitel and Deitel’s C How to Program book, but also using example tasks and things from the university, however I am stuck on one.

I have a very basic understanding of pointers – adding & in front of a variable makes it print an address and * uses a pointer to use the value stored at that address or such.

The piece of code I’ve written is for calculating the greatest (largest?) common denominator of two numbers and doesn’t actually need or involve pointers at all. It uses two functions and the logic is all correct because it prints out the correct answer to the screen if I do it from the second function, rather than returning it to the main. This is where the problem lies.

When the second function returns the answer value, for some reason it returns what I can only assume is a pointer. I have no idea why it does this. I would be able to work with this and convert it to look up the value – however it seems to be a pointer local the second function and is written over. Nothing on the web that I could find or in my book gave me any idea how to solve the problem.

Here is my code and output. I know I could just have it print in the second function but I would prefer to know how and why it doesn’t return the value like I would like it to.

Code

#include <stdio.h> int greatestCD (int num1, int num2);  int main(void) {     int a=0, b=0;     int result;     printf("Please enter two numbers to calculate the greatest common denominator from\n");     scanf("%d%d", &a, &b);     result = greatestCD (a,b);     printf("Using the correct in main way:\nThe greatest common denominator of %d and %d is %d\n",a,b, result); }  int greatestCD (int num1 ,int num2) {       if (num2==0){         printf("Using the cheaty in gcd function way:\nThe greatest common denominator is %d\n",num1);         return num1;     } else {         greatestCD(num2,(num1%num2));     }     } 

Output (using 12 and 15 – the answer should be 3)

C:\Users\Sam\Documents\C programs>gcd Please enter two numbers to calculate the greatest common denominator from 12 15 Using the cheaty in gcd function way: The greatest common denominator is 3 Using the correct in main way: The greatest common denominator of 12 and 15 is 2293524 

Such a simple solution from frankodwyer. It’s tiny things like that I either can’t spot or don’t know about. So what was being returned wasn’t a pointer and was just junk?

  • 1 1 Answer
  • 2 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. 2026-05-11T00:53:47+00:00Added an answer on May 11, 2026 at 12:53 am

    You are missing a ‘return’ statement in the last line of the function greatestCD()

    greatestCD(num2,(num1%num2)); 

    Make it

    return greatestCD(num2,(num1%num2)); 

    (you may still have further debugging to do, but this is why it’s returning junk…you’re not telling it what else to return)

    edit: I’d also suggest that when compiling in future you turn on all compiler warnings…if you’re using gcc then try adding the flag -Wall to your compile command. This should warn you when you’re doing things that may lead to bugs like this. (Not every such warning is an error necessarily, hence ‘warning’, but it usually indicates a possible problem.)

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

Sidebar

Ask A Question

Stats

  • Questions 215k
  • Answers 215k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This article does what you need it to do. http://blog.xebia.com/2009/03/09/jpa-implementation-patterns-data-access-objects/ May 12, 2026 at 11:02 pm
  • Editorial Team
    Editorial Team added an answer You need do this on every page that accesses the… May 12, 2026 at 11:02 pm
  • Editorial Team
    Editorial Team added an answer This is a fairly common question on SO and the… May 12, 2026 at 11:02 pm

Related Questions

In order to apply a triggered animation to all ToolTip s in my app,
I want use html5's new tag to play a wav file (currently only supported
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a French site that I want to parse, but am running into

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.