Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Top Posters

Who's Online (3)

Powered by Vanilla. Made with Bootstrap.
C for newcomers, Part 3
  • chroniccommand
    Posts: 1,389
    Welcome to part 3 of my C for newcomers guide. In this portion, I will talk about integers and other types of datatypes in C and all that boring shit.. But please bear with me because this is crucial for you to learn.

    Data types
    In C, there are several data types such as int, float, double and more. Obviously, Int means Integer. Integers can hold numbers from -32768 to 32767. Lets look at this piece of code using an integer. This piece of code will store an integer value of 10 and print it out. Simple right? Take a look.

    #include <stdio.h>

    int main()
    {
    int ten = 10;
    printf(\"The value of variable ten is %i\n\",ten);
    return(0);
    }

    Now, see the line of this code stating that integer ten is equal to ten? When you declare an integer with "int", you are setting the value of ten to 10, which is an integer. A bit confusing? Nah not really.. It's pretty easy to understand. As you can see, we use the %i placeholder in the printf statement. %i is used to hold a place for an integer. So when we compile and run this program, we should get a line like this:

    The value of variable ten is 10


    So int is quite easy to understand. Once you declare that variable of ten, you may change it any other time in the program by adding another line saying that integer ten is now equal to 20(Even though ten is not equal to twenty and if you're atleest in kindergarten you should know that. Now on to the next data type I will talk about, the float double type. No, float's do not float anywhere nor do they even hover a bit off the ground. Floats hold numbers 3.4 e-38 to 3.4 e+38. The E, if you payed attention in 6th grade math, is saying it can be 3.4000000000000000000000000000000000000000 Yes that's 38 0's, alot of zeros. Float holds very large numbers, as you can see. Whether they are negative or positive. Float can also be used with numbers that have a decimal point in it. Lets take a look at this code snippet, using a float.

    #include <stdio.h>
    //Remember to comment code ;)
    int main()
    {
    float pi = 3.14159265;
    printf(\"Do you know what pi equals? It equals %f but its infinite!\n\",pi);
    return(0);
    }

    Yes, pi is infinite. The first couple numbers is 3.14159265. When you compile and run this program you should get a line like so:

    Do you know what pie equals? It equals 3.14159265 but its infinite!


    Yes, in case you haven't figured it out, %f is the placeholder for float. So floats are very useful, especially when dealing with large numbers or numbers with decimals. Now for the next data type, double. The double data type is the data type that can hold the biggest numbers. From 1.7 e-308 to 1.7 e+308. Yes, that is 1.7 with 308 zeros. No, I am not typing out 308 zeros. So double is useful for those giant numbers that can have 308 zeros in it. Here is a code snippet using double:

    #include <stdio.h>
    int main()
    {
    double dubz = 1.709195313634613562739 //Setting dubs to a giant number
    printf(\"The value of dubz equals %d\n\",dubz);
    return(0);
    }

    Dayummmm gallll that's one big number! Compile it and run it, you should get something like this:

    The value of dubz equals 1.709195313634613562739


    So double is pretty easy to understand and very useful. Now for the last data type I will be covering in this segment, the char datatype. yes, you guessed it, char stands for character. Char is used to declare things like strings. Remember in my last guide
    char name[20];

    That piece of code is creating a character called name with 20 bytes. We can also create strings like so:

    char lol = \"lolzers\";

    That would set the character of "lol" to "lolzers". Char can be anywhere from -128 to 127. Characters are stored in 8 bit's of the computers storage. Every value you set for any program is taking up temporary space in the computers internal space, or RAM(Random Access Memory). We can also define datatypes like so:

    int a, b, c, d;
    double v1, v2, v3;

    And then we can declare them later in the program, like so:

    a = 12
    b = 13
    ....

    And so on and so forth.
    #define
    In C, we can also use the pound key for something else, the Define keyword. The syntax of defining something would be something like this:

    # define total 50
    # define pi 3.14159

    Constants
    Constants are something in a program that will never change, unless the coder ever changes them. Lets take a look at a program that has no int declaration, but stores a number and prints it as a constant:

    #include <stdio.h>
    int main()
    {
    printf(\"The value of ten is %i\n\",10);
    return(0);
    }

    Now we see in the printf statement that %i will be the placeholder for 10, which is put into the program after. If you do decide to compile and run this, you would get this:

    The value of ten is 10


    Now since it's a constant and never changes, the value will be ten. Now, try revising the code and replacing this line:
    printf(\"The value of ten is %i\n\",10);

    With this:
    printf(\"The value of ten is %i\n\",20);

    Now when you compile and run it you should get this:

    The value of ten is 20


    Well that doesn't make any sense :? Oh well, we successfully completed the task of changing the constant of 10 to 20. GREAT SUCCESSSSS(Borat reference).

    I hope this tutorial taught you something about simple datatypes in the C programming language. In my next guide I'll go over using math with the datatypes and in C. Cya next time,
    --Chroniccommand
  • Xin
    Posts: 3,251
    Thanks for the great input ! keep it coming.
    Xin
  • Bursihido
    Posts: 406
    bro error in this code
    [code]
    #include <stdio.h>
    int main()
    {
    double dubz = 1.709195313634613562739 //Setting dubs to a giant number
    printf("The value of dubz equals %d\n",dubz);
    return(0);
    }

    OUTPUT
    [bursihido@Exploiter c]$ gcc double.c
    double.c: In function ‘main’:
    double.c:5:1: error: expected ‘,’ or ‘;’ before ‘printf’




    [hr]
    and can u please talk more Define detail :
  • Sh3llc0d3
    Posts: 1,910
    Add a ';' after the value of "dubz".

    #include &lt;stdio&#46;h&gt;
    int main()
    {
    double dubz = 1&#46;709195313634613562739; //Setting dubs to a giant number

    printf(\"The value of dubz equals %d\n\",dubz);
    return(0);
    }


    try that