Thread: What's wrong with this code ?

  1. #1
    Registered User
    Join Date
    Mar 2015
    Posts
    1

    What's wrong with this code ?

    Code:
    #include <stdio.h>
    
    int main()
    { char * name = "fRod";
      int age = 23;
      printf ("you're called %d. you're %f years old.\n",name,age);
      return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > What's wrong with this code ?
    Compile it and find out.

    Run it and find out.

    Tell us what you think is wrong, then we'll tell you whether you're on the right track.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2015
    Posts
    6
    Quote Originally Posted by fRod View Post
    Code:
    #include <stdio.h>
    
    int main()
    { char * name = "fRod";
      int age = 23;
      printf ("you're called %d. you're %f years old.\n",name,age);
      return 0;
    }
    Seems like you are also a beginner likewise me but this is the place where we can clear our concepts. :-) :-)

    Code:
    #include <stdio.h>
     
    int main()
    { char * name = "fRod";
    
      int age = 23;
      printf ("you're called %s. you're %d years old.\n",name,age);
    
      return 0;
    }
    In the first one, with the pointer to character. You are printing %d instead of %s. That is why it is pointing to the memory address of the variable. And, regarding age you declared it with "int" datatype and in output you are using %f (which is for float). For integer, c uses %d.


    The other way I have found it is using array where you can store "fRod" string.

    Code:
    #include <stdio.h>
     
    int main()
    { 
      char name[5] = "fRod";
    
      int age = 23;
    
      printf ("you're called %s. you're %d years old.\n",name,age);
    
      return 0;
    
    }
    Please share if you have additional knowledge as well. Hope it helps.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Or you could just wait for some well meaning noob to blurt out the answer, depriving you of the chance to try something for yourself and gain some real knowledge and experience.

    @rohitbayern, please read the rules about giving a complete answer before the OP has had a chance to show some progress of their own.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,110
    @rohitbayern

    In this simple example, given how it is used, there is no practical difference between:
    Code:
    char * name = "fRod";
    and
    Code:
    char name[5] = "fRod";
    There is a big difference when used in a different context. In the first, name points to the constant string. (Only one copy of the string exists, and cannot be altered.)

    In the second, a null terminated char array is created, initialized with the constant string. You then have two copies of the string. One is a constant string, and the other a char array where the contents could be changed.

    In addition, the second could also be written as:
    Code:
    char name[] = "fRod";
    Both would create a 5 char array for the null terminated string.

    EDIT: You also need to turn on your warnings and errors, and/or turn them up to see all. You would then have seen the problems you were asking about. This should have been taught by the instructor, or in the book, depending on where and how you are learning to program C.

    Also, if no command line arguments are needed, main() should be defined as:

    Code:
    int main(void)
    {
        /* ...  */
        return 0;
    }
    Last edited by rstanley; 03-03-2015 at 10:22 AM.

  6. #6
    Registered User
    Join Date
    Feb 2015
    Posts
    6
    Quote Originally Posted by Salem View Post
    Or you could just wait for some well meaning noob to blurt out the answer, depriving you of the chance to try something for yourself and gain some real knowledge and experience.

    @rohitbayern, please read the rules about giving a complete answer before the OP has had a chance to show some progress of their own.
    Sorry about that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Where I'm wrong in this C code
    By rcbandit in forum C Programming
    Replies: 1
    Last Post: 04-06-2011, 11:58 AM
  2. What is wrong with my code?
    By dakarn in forum C Programming
    Replies: 6
    Last Post: 10-14-2008, 05:16 AM
  3. What's wrong with my code?
    By Codefish in forum C++ Programming
    Replies: 4
    Last Post: 07-27-2005, 05:19 PM
  4. what's wrong with the following code?
    By catalyst in forum C Programming
    Replies: 1
    Last Post: 11-07-2003, 04:30 AM
  5. What's wrong with this code?
    By ylzhang in forum C Programming
    Replies: 3
    Last Post: 06-20-2003, 07:09 AM

Tags for this Thread