Getting started with C [2]

Before we start you will need platform for C/C++, you can use Code::Blocks is a free IDE(Integrated development environment) for C/C++, or Visual Studio, I think they have a free version too. Next, I will give a few examples of how C look like, and I will try to describe how he works.

Number 1

#include "stdafx.h"

int main()
{
	printf("This is just a test");
	getchar();
        return 0;
}

We include stdafx.h is the library that helps the compiler to understand the code.

#include "stdafx.h"

int main()
{

}

This is the main function, I think this is obvious. Next the printf function will show whatever we want on the console. In this case “This is just a test” is what you will see when you run this application. The getchar(); function will allow the console to stay open until I press ENTER. And finally return 0;means that we finished the program without any error.

This is what you will see:

Number 2

#include "stdafx.h"

int main()
{
	int a,b;

	a = 10;
	b = 20;

	printf("%d + %d = %d",a , b , a + b); 
	printf("    ");
	printf("%d * %d = %d",a , b , a * b);
	printf("    ");
	printf("%d - %d = %d",a , b , a - b);
	printf("    ");
	printf("%d : %d = %d",a , b , a / b); 

	getchar();
    return 0;	
}

In the main function I make 2 variables a and b, next, I make a = 10 and b = 20. As you know from the previous example printf() function will show what ever we want on the console. Now, lets see what this line of code do: printf(“%d + %d = %d”,a , b , a + b); . If you read the previous tutorial you know that %d indicate the position where a decimal integer should be. We have 3 of %d there, so we need 3 variable, the first one is a, the second one is b and the third one is a + b or a * b etc…
You can use Constant Characters to show every operation on a new line or whatever you want.
The final result should be:

You see that 10 : 20 = 0 that is because our variables are int. The int type does not support 0.5, if you make a and b float, you will have your answer, and of course change %d to %f.

Number 3

#include "stdafx.h"
#include <stdio.h>

int main()
{
	float a = 0,b = 0,c = 0,r = 0;
	
	printf("a:");
	scanf("%f",&a);

	printf("b:");
	scanf("%f",&b);

	printf("c:");
	scanf("%f",&c);

	r = a + b - c;

	printf("r = %f", r);

	getchar();
    return 0;	
}

In this example a new function appears scanf();. Well, let’s take a look and see what we have here:scanf(“%f”,&a);. The scanf() function in this case has 2 parameters, the first one is the type of data in this case float, and the second one is an address. More specific is the address where the a variable it will be stored. & means address.

Number 4

#include "stdafx.h"
#include <stdio.h>

void main()
{
	char c;
	int i;
	float f;
	double d;
	
	c = 'C';
	i = 1195;
	f = 123.4567;
	d = 11212.33E3;
	
	printf("character = %c", c);
	printf("     ");
	printf("int = %d", i);
	printf("     ");
	printf("float = %f", f );
	printf("     ");
	printf("double = %e", d);

	getchar();
}

This is a very simple example, shows you how to use every type of fundamental data, so you can experiment with C as you want.

Comments (0)

Leave a Reply

Your email address will not be published. Required fields are marked *


*

This site uses Akismet to reduce spam. Learn how your comment data is processed.

WordPress SEO