Data Types in C - cradules/cs50 GitHub Wiki
In C the data type must be declared for every variable we create.. This is not a general rule for other programming languages as, java, python, etc but is something specific to C, being an older programming language.
-
int The int data types is used for variables that will store integers Integers always take up 4 bytes of memory (32 bits system).This means the range of values they can store is necessarily limited to 32 bits worth of information.
### Integer Range
-2square31_____________________________0__________________________________________2square32-1
-
unsigned int unsigned is a qualifier that can be applied to certain types (including int), witch effectively doubles the positive range of variables of that type, at the cost of disallowing negatives values.
### Integer Range
0____________________________________2square31__________________________________2square32-1
-
chars The char data type is used for variables that will store single characters Characters always take up 1 byte of memory (8 bits). This means the range of values they can store is necessarily limited to 8 bits worth of information. Thanks to ASCII, we've developed a mapping of characters like A, B, C, etc .. to numeric values in the positive side of this range:
### Character Range
<|-128________________________________________0__________________________________________127|>
-
float The float data type is used for variables that will store floating-point values, also know as real numbers. Floating points values always take up 4 byes of memory (32bits) Is a little complicated to describe the range of a float, but sufficient is to say with 32 bits of precision, some of
witch might be used for an integer part, we are limited in how precise we can be. -
double The double data type is used for variables that will store floating-points values, also know as real numbers The difference is that doubles are _double precision _. They always take up 8 bytes of memory(64bits) With additional 32 bits of precision relative to a float, doubles allow us to be specify much more precise real numbers.
-
void Void is a type but not a data type Functions can have void return type, witch just means they don't return a value. The parameter list of functions can also be void. It simply means the function take no parameters. For now, think of void more as placeholder for "nothing". It's more complex then that, but this should suffice for the better part of the course
-
bool The bool data type is used for variables that will store a Boolean value. More precisely, they are capable only one of two values: true and false. Be sure to #include <cs50.h> (libcs50) atop your programs if you wish to use the bool type.
-
string The string datatype is used for variables that will store a series of characters, witch programmers typically calls strings. Strings include things such as words, sentence, paragraphs, and the like.
Be sure to #include <cs50.h> (libcs50) atop your programs if you wish to use the string type. -
Creating a variable To bring a variable into existence, you need simply specify the data type of the variable and give it a name
- int number;
- char letter;
If you wish to create multiple variables of same type, you specify the name once, and then list as many variables of that type as you want
- int height, width;
- float sqrt2, sqrt3, pi;
In general is good practice to only declare variables when you need them.
-
Using a variable After a variable has been declared, is no longer necessary to specify that variable's type(In fact doing so has some unintended consequences!)
- int number; //declaration
- number = 17; //assignment
- char letter; //declaration
- letter = 'H'; //assignment If you are simultaneously declaring and setting the value of a variable, (sometimes called initializing), you can consolidate this to on step
- int number = 17; //initialization
- char letter = 'H'; //initialization