chevron_left chevron_right
Login Register invert_colors photo_library


Stay updated and chat with others! - Join the Discord!
Thread Rating:
  • 0 Vote(s) - 0 Average
Thread Closed 


Introduction to C Programming [Part 2: your first program] filter_list
Author
Message
Introduction to C Programming [Part 2: your first program] #1
Some members have made me think maybe writing this tutorial was a bad choice, but I will write it anyways, for the few that actually give a rats ass about learning.


So, you now have the tools, and are ready to begin your first program. Lets get started. Open up a blank file named "hello.c"
Code:
nano hello.c

Alright. A few rules I need to state before I jump into code.

1. EVERY C program MUST have an entry point. We call that "main".
2. EVERY C program MUST be compiled from object or source files (these are the .c files)

So, step 1. Main.

Main is a function. For those of you who went above the high school level in math will already know what a function is. v(p)=dp/dt type of thing.
Basically, a function takes zero or more parameters, does a task, and returns zero or one output. It sounds simple to me, but it may not have been explained correctly. So, let's do a quick example:
f(x, y)=x*y
Simple. Here is the equivalent integer function in C:
Code:
int f(int x, int y)
{
     return x * y;
}

There we go. There are a few things in that I need to explain before we can go any farther. Firstly, data types.
Data types are what we will use to store our data, manipulate it, or really do anything with it. There are a few basic types:
int - integer in the range +/-2^32
float - floating point (decimal) number (32-bit)
double - double precision float (64-bit)
char - integer in the range +/-2^8 (-128 to +127)
short - integer in the range +/-2^16
void - nothing (used for functions)

Memorize those, they are VERY IMPORTANT.

Next, here are some rules.
EVERY STATEMENT MUST END IN A SEMICOLON
MAIN MUST RETURN A VALUE (int)
EVERY FUNCTION MUST BE CONTAINED WITHIN CURLY BRACES ({ })

So, let's break down that function:
(also, anything following a // is ignored by the compiler, known as a comment)

Code:
int f(int x, int y) //define a function named f that takes x and y and returns another integer
{ //remember, code must be inside braces
     return x * y; //return the value x * y. end it in a semicolon, it is a statement
} //tell the compiler our function is done



Easy enough. I may not have gone in depth enough on functions. they are defined with the following format:

Code:
return-type name(parameters)
{
     statements;
     return-statement;
}

The return-statement must be present unless the return-type is void.

sample return statements:
Code:
return 1;
return a;
return 3.14;
return;

and now, we can introduce you to your first function (ones already written).

Code:
int printf(const char *fmt, ...);

You can basically ignore that for now, it will make sense in part 3 when we start making more functions in our programs.

But in order to use printf, we must tell our compiler that we want to. This is done with include statements. I will go over this a little later, but essentially, that last code block is in a file called stdio.h somewhere on your system, and including it just pastes that entire file in our code when we compile it.

Code:
#include <file>
and
#include "file"

What is the difference?
Well, <file> is used for files we didn't write (ones on our system somewhere) and "file" is for files in the same directory

Code:
#include <stdio.h> //so we have printf

int main() //memorize this line
{
     printf("Hello, world!\n");
     return 0; //this should be in main usually. Will go into more details later. Memorize for now
}

and compile it! (^X that, say yes to saving)
Code:
gcc hello.c

this will create a file named a.out
Code:
./a.out

You should see hello world on the command line!



Stay tuned for part 3 where we start writing some cool functions.


RE: Introduction to C Programming [Part 2: your first program] #2
Serious question, as every statement needs to be followed by a semicolon and functions must be within braces, why is the following not syntatically correct?\

Code:
#include <stdio.h>;

{;
    int main(int argc, char* argv){;

          printf("Hello World!);
          return 0;
    };
};

[+] 2 users Like DAKIMAKURAFAN2008's post

RE: Introduction to C Programming [Part 2: your first program] #3
(01-14-2015, 08:22 AM)DAKIMAKURAFAN2008 Wrote: Serious question, as every statement needs to be followed by a semicolon and functions must be within braces, why is the following not syntatically correct?\

Code:
#include <stdio.h>;

{;
    int main(int argc, char* argv){;

          printf("Hello World!);
          return 0;
    };
};

Codes have parts, you can't call everything a statement. Here's what phyrrus9 labeled.

(01-13-2015, 07:20 PM)phyrrus9 Wrote:
Code:
return-type name(parameters)
{
     statements;
     return-statement;
}

I find this thread very useful, especially since I don't know how to code yet. Can't wait to read the part 3.
[Image: dHJ4Beo.gif]
Hidden Lesson: Reactions are always instinctive whereas responses are always well thought of.


RE: Introduction to C Programming [Part 2: your first program] #4
(01-14-2015, 08:22 AM)DAKIMAKURAFAN2008 Wrote: Serious question, as every statement needs to be followed by a semicolon and functions must be within braces, why is the following not syntatically correct?\

Code:
#include <stdio.h>;

{;
    int main(int argc, char* argv){;

          printf("Hello World!);
          return 0;
    };
};

A semicolon by itself is a statement. It just happens to compile to nothing.

example:
Code:
for (;;);

is the EXACT SAME AS
Code:
while (1) {}


RE: Introduction to C Programming [Part 2: your first program] #5
Don't forget the 'bool' type.


RE: Introduction to C Programming [Part 2: your first program] #6
(01-16-2015, 01:00 AM)0xDEAD10CC Wrote: Don't forget the 'bool' type.

bool isn't a real type. in the same sense that the for loop does not exist.
If you wish to discuss either of those statements, take it somewhere else.


RE: Introduction to C Programming [Part 2: your first program] #7
(01-13-2015, 07:20 PM)phyrrus9 Wrote: There we go. There are a few things in that I need to explain before we can go any farther. Firstly, data types.
Data types are what we will use to store our data, manipulate it, or really do anything with it. There are a few basic types:
int - integer in the range +/-2^32
float - floating point (decimal) number (32-bit)
double - double precision float (64-bit)
char - integer in the range +/-2^8 (-128 to +127)
short - integer in the range +/-2^16
void - nothing (used for functions)

I find it interesting to list short but not long, it may also be worth explaining that there are optional keywords that may be used here such as unsigned and signed, short and long (e.g. short int, long double), and even long long. It may also be worth qualifying that voids need not only be used for functions but may also be used to declare pointers which are not type-specific.

If this list is meant to be exhaustive then enum and struct also deserve mention as data types.

(01-13-2015, 07:20 PM)phyrrus9 Wrote: EVERY STATEMENT MUST END IN A SEMICOLON

Not quite true: every Expression Statement must end in a semicolon. Labelled statements will end in a Colon punctuator, and most statements altering control-flow will not require a terminating semicolon unless they are meant as a compound statement with a nested null statement.


(01-13-2015, 07:20 PM)phyrrus9 Wrote: include statements

As noted by yourself, statements often require some form of punctuator whether it be a semicolon, colon, or curly braces. Includes have none of the aforementioned punctuators and that is quite easily explained by the fact that they are indeed preprocessor Directives, not statements.

I think your tutorial is aiming to do too much too quickly. You glaze over data types when they probably deserve some more attention as we should probably see the proper use and declaration of pointers long before we see a function that requires one:

(01-13-2015, 07:20 PM)phyrrus9 Wrote:
Code:
int printf(const char *fmt, ...);


RE: Introduction to C Programming [Part 2: your first program] #8
(01-16-2015, 02:19 AM)phyrrus9 Wrote: bool isn't a real type. in the same sense that the for loop does not exist.
If you wish to discuss either of those statements, take it somewhere else.

Tell this to the C99 standard (that you obviously haven't read) before you pretend to know what you're talking about Smile

bool Is an alias for _Bool, and is large enough to hold 1 and 0.


RE: Introduction to C Programming [Part 2: your first program] #9
(01-20-2015, 02:18 AM)0xDEAD10CC Wrote: Tell this to the C99 standard (that you obviously haven't read) before you pretend to know what you're talking about Smile

bool Is an alias for _Bool, and is large enough to hold 1 and 0.

_Bool is a one byte storage unit, the same as a char. It is large enough to hold a value between 0 and 255 (or -128 to +127).

C99 6.3.1.2:
Quote:When any scalar value is converted to _Bool, the result is 0 if the value compares equal
to 0; otherwise, the result is 1.

That implies that it is a single bit field, or can only hold the value 0 or 1, but in reality, the compiler is actually messing with any scalar. the following will output 1:
Code:
#include <stdio.h>
#include <stdbool.h>

int main(void)
{
     _Bool a = 5;
     printf("%d\n", a);
     return 0;
}

Which would imply that it was single bit. But in that case, then setting it to the value 4 should force it to output zero, but it comes out to be 1 again.

Code:
#include <stdio.h>
#include <stdbool.h>

int main(void)
{
     _Bool a = 5;
     char *b = (char *)&a;
     *b = 5;
     printf("%d\n", a);
     return 0;
}

now it comes out to be 5. Since the largest number a 1 bit field can hold is 1 (2^1-1), this disproves that it is only large enough for 0 and 1. Testing this once more, with negative numbers.
Setting the value to -5 results in 251 being printed, so we know that -5 is the value
11111011
which, in unsigned binary is the value 251. And just for a test, let's throw in 256 just to see if it is 0 (if the field is larger than one byte, it will be 256, else 0).
Funny thing:
Quote:warning: overflow in implicit constant conversion
Well, lets run it just to see..hey, look at that, it is in fact 0.

So, we can conclude that _Bool is EXACTLY the same data structure as unsigned char.

Any more snotty comments you wish to add or can you go back to playing on the playground wrapped in bubble wrap and talking to walls?

I'm closing this thread now. It seems that programming threads tend to spark semantic trolls, and I am tired of cleaning up the mess.
(This post was last modified: 01-20-2015, 08:39 PM by phyrrus9.)







Users browsing this thread: 1 Guest(s)