• 02-22-2013
jsalz
Hi everyone, first post and very, very new to C. I'm taking a class to learn programming and while doing homework I ran into a problem that I can't figure out. I'm trying to get the output to show "mph" which I added below in red, and I've tried adding a string but cannot get it to work. The code is below, If anyone could point me in the right direction on how to get "mph" added I'd greatly appreciate it.

int a;
float b,c,d,e,total;

a = 100;
b = 3;
c = 19;
d = 3;
e = 4;
total = e+d+a/c*b;

printf("The total miles driven per day: %d\n", a);
printf("The cost per gallon of gasoline: \$%0.2f\n", b);
printf("The average miles per gallon: %.0f\n", c);
printf("The parking fees per day: \$%0.2f\n", d);
printf("The tolls per day: \$%0.2f\n", e);
printf("Your daily cost of driving to work is: \$%0.2f\n", total);
system("PAUSE");
}

/*
The total miles driven per day: 100
The cost per gallon of gasoline: \$3.00
The average miles per gallon: 19 mph
The parking fees per day: \$3.00
The tolls per day: \$4.00
Your daily cost of driving to work is: \$22.79
Press any key to continue . . ./
• 02-22-2013
Welcome to the forum, jsalz. ;)

Always go advanced when you have code to post, highlight your code, and click on the # icon in the top bar of the editor - that will put [code] tags around your program, and keep it looking like code, instead of forum html text.

Makes it MUCH easier to study.

Code:

`char *mymph={"mph"};`
and then print it using %s, after printing the mph digits.

Although you could just print the mph digits and then print out mph, right inside the printf formatting string.

More than one way to do almost anything.
• 02-22-2013
jsalz
We learned how to add a name with char name [10] but never how to just add normal char's! If you don't mind me asking, why is it *mymph=? What does the asterisk do?
Sorry... like I said, super new at programming C.
• 02-22-2013
MutantJohn
I think it's because it dereferences the pointer, right? Like, mymph would be the pointer to a char structure and *mymph would be what mymph points at, the char structure itself. So what you're doing is setting the value of the char structure. I think. I'm super new to C as well but I need to know if I know this stuff so if I'm wrong then please, feel free to stop the spread of misinformation.
• 02-22-2013
smokeyangel
I'd just put it in the printf rather than having a variable:

Code:

`printf("The average miles per gallon: %.0f mph\n", c);`
I'm afraid you're wrong, MutantJohn. The * here is indicated that the variable being defined is a pointer.
Once you're got a pointer, you can use * to derefence it in the way you've said.

Code:

```  char *mymph = "mph";  // mymph is type char*     char test = *mymph;    /// Deference the pointer and get back the char 'm'```
jsalz -- if you're happier with arrays you can also do:
Code:

```  char mymph[] = "mph";    // or   char mymph[4] = "mph";```
What you can't do is:
Code:

`  char mymph = "mph";`
These are all valid ways of defining a string (sequence of chars) in C. They're all slightly different in important ways -- but I wouldn't worry about that for now (unless you really want to know?).

Usually when you see a char* in code it's referring to a string. Pointer types hold the addresses at which a variable is stored -- in the case of a string, it holds the address of the first character. The string continues until a NULL terminator is found (value 0). However, a char* can also be the address of a single character, or a pointer to a sequence of characters in memory that aren't a string because they don't end with a NULL terminator.

If you're just starting out then it's probably best to think of char* as "string type*. Be aware that it's a pointer and could point to anything, not necessarily a valid string. But don't worry about it too much -- you won't get very far in C without understanding pointers, so it probably won''t be long before you learn about them.
• 02-22-2013
jsalz
This forum is awesome!! Thank you so much everyone! I may have another question soon haha
• 02-25-2013
hk_mp5kpdw
Just a minor nit but this:
Code:

`char *mymph = "mph";`
Should really be this:
Code:

`const char *mymph = "mph";`
It's safer to do this as the compiler can then make sure you aren't trying to write to the memory pointed to by the pointer somewhere in your code. Without the const you could accidentally try writting to this memory - and the compiler wouldn't know to complain about it - which could quite possibly present itself as a bug.