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; }
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; }
> 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.
Seems like you are also a beginner likewise me but this is the place where we can clear our concepts. :-) :-)
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.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; }
The other way I have found it is using array where you can store "fRod" string.
Please share if you have additional knowledge as well. Hope it helps.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; }
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.
@rohitbayern
In this simple example, given how it is used, there is no practical difference between:
andCode:char * name = "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.)Code:char name[5] = "fRod";
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:
Both would create a 5 char array for the null terminated string.Code:char name[] = "fRod";
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.