Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Objective-C Basics (Retired) Fundamentals of C Variables

Differences between char & int?

Are there any more distinguishable differences between int and char in C?

Ā 

It sounds like int can hold a whole number, and char can hold a number or a letter. Are there any other differences between the two? When would use use char over int, or int over char?

Petar Brkovic
Petar Brkovic
8,651 Points

Char is short for character, and should be used for strings. Int is used for whole numbers.

Never use char for number.

I'm not sure that this logic of mine is 100% true, so please if someone knows better, correct me.

1 Answer

Jayden Spring
Jayden Spring
8,625 Points

Big difference - char is really the most basic type accessible to a programmer. I would suggest having a read of The C Programming Language from Kernighan & Ritchie to understand in detail.

Petar Brkovic said that never to use char for a number - which is correct to an extent. A char is an integer, just one which you will never see as that integer is machine code (ie 0's or 1's). Char's are individual characters, for example you can store a line of text in an array of type char with an allocated size that matches the size of your string. Or if you wanted to change the 4th letter of a word from upper to lower you could turn the Unicode String into a w_char or char and then access the letter at position [3] in an array and transform it. Have a look at this page here for more on Chars