Advertising (This ad goes away for registered users. You can Login or Register)

[Tutorial] Introduction to programming using C (IV)

Discuss about your favorite (gaming...or not) devices here. The most popular ones will end up getting their own categories
Programming discussions for your favorite Device
Forum rules
Forum rule Nº 15 is strictly enforced in this subforum.
User avatar
m0skit0
Guru
Posts: 3817
Joined: Mon Sep 27, 2010 6:01 pm

[Tutorial] Introduction to programming using C (IV)

Post by m0skit0 » Sun May 15, 2011 7:42 pm

Back to index
<< Prev Next >>

Basic data types

The goal for every program is processing some data and giving a result with it. For processing this data, high-level languages like C define types of data. Is the data a number? If so, which type of number? Can it be negative? Can it be decimal? Or if it's not a number, is it a sequence of characters? This typification helps limiting bugs on the code, which was one of the many problems programmers had with assembly.

One important thing to note is that in C everything is a number. Even a character is a number (its ASCII code is stored). This is because for computers everything are numbers too. Keep this awake in your mind forever when dealing with computers.

On a programmer's point-of-view, C distinguishes basically between 2 types of data: numbers and characters (and strings of characters). Let's begin with the numbers.

Integers

Integers are non-decimal numbers. Basic type for this type of data is int, which defines a "32-bit signed integer". "32-bits signed" means one bit is used a sign flag (0 = positive, 1 = negative) and the rest of the 31 bits are used for the value, which means this data type can hold a value from −2,147,483,648 to 2,147,483,647. Any number outside this range cannot be represented with the int data type (you're not likely to be handling such big numbers, so don't worry).

Several modifiers can be applied on the int data type. unsigned is one of the most common. unsigned int indicates that the data cannot be negative, thus boosting the positive range from 0 to 4,294,967,295. Even if we don't need such a range, it's always wise to define an unsigned int when needed.

Let's see an example:

Code: Select all

// printf
#include <stdio.h>

// Entry point
int main()
{
	// We declare an integer and store there the value -35
	int number1 = -35;
	
	// We declare a positive integer and store there the value 15
	unsigned int number2 = 15;
	
	// We use printf to show the numbers
	// Note the use of %d for int and %u for unsigned int
	printf("The numbers are %d and %u\n", number1, number2);
	
	// Back to shell
	return 0;
}
First thing to note is the use of comments. Everything after a double slash (//) is ignored by the compiler so always comment your code!

I think most confusing line might be

Code: Select all

printf("The numbers are %d and %u\n", number1, number2);
Here the %d and %u are substitutes for the values of number1 and number2 respectively. All % indicators are substituted by the value of the expressions put as arguments for printf after the format string. The letter(s) after the % indicate how should printf show those values. In this case we want the human representation of the numbers (as introduced). We could also put printf("The numbers are %08X and %08X\n", number1, number2) and get them as the computer sees them.

For more information about the use of printf, please refer to the documentation (man 3 printf).

Decimal numbers

Decimal numbers are referred to as floating-point numbers on computer science and float in C. This is a 32-bit single-precision floating point number, denoted as float in C. As with the other data types, having a fixed bits length means it can only store a range of numbers. In this case, not all decimal numbers can be stored exactly. For example, 0.1 cannot be stored exactly on a C float data type, so it's approximated.

Let's see an example:

Code: Select all

#include <stdio.h>

int main()
{
	// We declare a decimal number and assign him the value 3.5
	float number1 = 3.5;

	// Note the use of %f for float
	printf("Your floating point number is %f\n", number1);
	
	return 0;
}
Single character

One single character is represented by the char data type. It's one byte long, which means it can store values from -128 up to 127. Negative values are rareley used with char, so it's usual to see it declared as unsigned char. The value stored on this data type is the ASCII code of the character.

Let's see an example:

Code: Select all

#include <stdio.h>

int main()
{
	// We declare a character and assign him the value 'a'
	// Characters are denoted with a single quote (')
	char character1 = 'a';

	// Note the use of %c for character
	// We also output the character as an integer to check the value stored
	printf("Your character point number is '%c' and its ASCII code is %d\n", character1, character1);
	
	return 0;
}
Character strings

A character string is a list of characters. It's represented as char* or char[] in C (which are actually the same thing with some differences). C uses what's called null-terminated character strings, which means the end of the character string is marked with a NULL character, the character with ASCII code 0 (do not confuse with character '0'). For example, the string "Hello" would be stored on memory as 72 101 108 108 111 0, thus being 6 bytes despite the fact that the string is only 5 characters long. This should be always in your mind while working with strings.

Let's see an example:

Code: Select all

#include <stdio.h>

int main()
{
	// We declare a character string and assign him the value "Hello world!\n"
	// Character strings are delimited with a double quote (")
	char* string = "Hello world!\n";

	// Note the use of %s for character string
	printf("%s", string);
	
	return 0;
}
Because character strings are represented as pointers in C, their handling is a little tricky compared to other data types. We'll look into this later on.

See you on the next part, until then carpe diem!

<< Prev Next >>
Advertising
Last edited by m0skit0 on Fri Apr 05, 2013 8:56 pm, edited 1 time in total.
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"

irfanhb7
Posts: 466
Joined: Wed Jan 26, 2011 2:46 pm
Location: In your nightmares

Re: [Tutorial] Introduction to programming using C (IV)

Post by irfanhb7 » Mon May 16, 2011 2:00 am

Hey learning C is easy if you know turbo C++.
Advertising
Languages I know : C , C++ & Java
Image
Boy : There is something wrong with my phone.
Girl : What ?
Boy: It don't have your Phone Number.
Image

ASKidwai
Posts: 937
Joined: Mon Jan 10, 2011 7:42 am
Location: 'Ere and There
Contact:

Re: [Tutorial] Introduction to programming using C (IV)

Post by ASKidwai » Mon May 16, 2011 3:53 am

irfanhb7 wrote:Hey learning C is easy if you know turbo C++.
Don't mean to jump on you but don't you mean C++?
Turbo C++ is an (outdated) compier
Image
Image
Image
Image

irfanhb7
Posts: 466
Joined: Wed Jan 26, 2011 2:46 pm
Location: In your nightmares

Re: [Tutorial] Introduction to programming using C (IV)

Post by irfanhb7 » Mon May 16, 2011 7:21 am

I mean that turbo C++ programming is somehow related to C.
Correct me if I am wrong.
Languages I know : C , C++ & Java
Image
Boy : There is something wrong with my phone.
Girl : What ?
Boy: It don't have your Phone Number.
Image

ASKidwai
Posts: 937
Joined: Mon Jan 10, 2011 7:42 am
Location: 'Ere and There
Contact:

Re: [Tutorial] Introduction to programming using C (IV)

Post by ASKidwai » Mon May 16, 2011 7:24 am

irfanhb7 wrote:I mean that turbo C++ programming is somehow related to C.
Correct me if I am wrong.
Since when is an outdated C++ compiler related to learning C?
Image
Image
Image
Image

irfanhb7
Posts: 466
Joined: Wed Jan 26, 2011 2:46 pm
Location: In your nightmares

Re: [Tutorial] Introduction to programming using C (IV)

Post by irfanhb7 » Mon May 16, 2011 7:33 am

If you have some experince of C++ programming then you should know that coding in turbo C++ is diffrent from dev C++ or codeblocks or any other compiler that you use.
Again correct me if I am wrong.
Languages I know : C , C++ & Java
Image
Boy : There is something wrong with my phone.
Girl : What ?
Boy: It don't have your Phone Number.
Image

waratte
Posts: 1320
Joined: Wed Oct 20, 2010 12:03 am

Re: [Tutorial] Introduction to programming using C (IV)

Post by waratte » Mon May 16, 2011 10:49 am

irfanhb7 wrote:If you have some experince of C++ programming then you should know that coding in turbo C++ is diffrent from dev C++ or codeblocks or any other compiler that you use.
Again correct me if I am wrong.
Dev C++ and codeblocks are and always will be IDE's. They are not compilers.

User avatar
m0skit0
Guru
Posts: 3817
Joined: Mon Sep 27, 2010 6:01 pm

Re: [Tutorial] Introduction to programming using C (IV)

Post by m0skit0 » Mon May 16, 2011 11:06 am

Turbo C++ is an old obsolete C++ compiler. And C++ is not C.

Also C++ is a standarized language, so if you coded in standard C++ using Turbo C++ you will code fine on any other compiler/IDE.
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"

ASKidwai
Posts: 937
Joined: Mon Jan 10, 2011 7:42 am
Location: 'Ere and There
Contact:

Re: [Tutorial] Introduction to programming using C (IV)

Post by ASKidwai » Mon May 16, 2011 11:28 am

m0skit0 wrote:Turbo C++ is an old obsolete C++ compiler. And C++ is not C.

Also C++ is a standarized language, so if you coded in standard C++ using Turbo C++ you will code fine on any other compiler/IDE.
So I was right?
Image
Image
Image
Image

irfanhb7
Posts: 466
Joined: Wed Jan 26, 2011 2:46 pm
Location: In your nightmares

Re: [Tutorial] Introduction to programming using C (IV)

Post by irfanhb7 » Mon May 16, 2011 11:32 am

ASKidwai wrote:
m0skit0 wrote:Turbo C++ is an old obsolete C++ compiler. And C++ is not C.

Also C++ is a standarized language, so if you coded in standard C++ using Turbo C++ you will code fine on any other compiler/IDE.
So I was right?
No.
What I was basically trying to say was -
"if you coded in standard C++ using Turbo C++ you will code fine on any other compiler/IDE" .
Languages I know : C , C++ & Java
Image
Boy : There is something wrong with my phone.
Girl : What ?
Boy: It don't have your Phone Number.
Image

Post Reply

Return to “Programming and Security”