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

  • SEARCH
  • Home
  • 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 8101891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:05:57+00:00 2026-06-05T23:05:57+00:00

Why is this printing out 0 back in main but 6 when it is

  • 0

Why is this printing out 0 back in main but 6 when it is inside of the strcmp function?

  7 int main()
  8 {
  9 char* str = "test string";
 10 char* str2 = "test strong";
 11 //printf("string length = %d\n",strlen(str));
 12 
 13 int num = strcmp(str,str2);
 14 
 15 printf("num = %d\n",num);
 16 }




 29 int strcmp(char* str, char* str2)
 30 {
 31   if(*str == '\0' && *str2 == '\0')
 32     return 0;
 33   if(*str2 - *str == 0)
 34   {
 35     strcmp(str+1,str2+1);
 36   }
 37   else
 38   {
 39     int num = *str2 - *str;
 40     cout << "num = " <<num<<endl;
 41     return num;
 42     }
 43 }

The output is:

num = 6
num = 0

Why is it printing 0 when obviously the value that it should be returning is 6?

  • 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-05T23:05:58+00:00Added an answer on June 5, 2026 at 11:05 pm

    Did you forget to add a return statement?

     33 if(*str2 - *str == 0)
     34   {
     35     return strcmp(str+1,str2+1);
     36   }
    

    Otherwise, the code will just skip past the rest of your if statement and reach the end of your function, returning nothing (or 0 in your case, but that’s being lucky).

    Your code will only work if the first characters of both strings are different from each other. Or if both strings are empty.

    Your compiler should warn you about this; returning void from non void function. If not, you should compile with -Wall 🙂

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

Sidebar

Related Questions

I successfully inserted some data into my sqlite database(I confirmed this by printing out
I am having some issues printing out the subarray homemain=>image This is my JSON
With this javascript I'm printing list of records: <script type=text/javascript> $(document).ready(function(){ function hide(){ $('#friend_list').fadeIn(100);
ok back at another issues in as3 printing //Function to print entire screen function
Consider this Python code for printing a list of comma separated values for element
In this below program i am printing the contents of the div when i
What are my options for printing in Silverlight 3? Assume I have this awesome
I have gotten this to work nicely, but when speed hits a certain speed
This does not work. I'm trying to learn how to use std::copy but I
Been practicing with those system calls, but I stucked into this code: #include <stdio.h>

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.