Tag Archive for C programming

Introduction to Programming with C – Data Types and Variables

Variables are the places which stores the numbers, strings in the program. These belong to various data types, depends on what type of data we store. Basic data types in C:

char - 1 Byte 
int - Usually 4 Byte
float - Usually 4 Byte
double - Usually 8 Byte

We get another versions of data types by putting modifiers in front of the data type names. These modifers are:

short
long
unsigned

You can easily learn which data type takes how many bytes in memory with this code:

#include < stdio.h >
int main(){

    printf("%d byte", sizeof(int)); // put which data type you want to learn instead of int, such long int, short int, char, float...
    return 0;
}

Because one byte consist of 8 bit and every bit takes only 1 or 0 value, one byte can take 2^8 different values. Hence, int data type , which takes 4 byte , takes 2^(8*4)-1 different positive integer values.

Now let’s look at which data type use for what.

char: Stores only one character. Stored data is not actually a character, it is a number matches to the characters in the ascii table. Namely, it stores integers with the maximum size of 1 byte. It is enough to know that data type stores characters, for now.

int: Stores integers.

float: Stores floating point number with single precision.

double: Stores floating point number with double precision

Some modifers to put in front of these data types;

unsigned: It specifies no negative values. Therefore, 1 bit for sign join to number area in the memory. For example, char takes values between -128,127 and unsigned char takes values between 0,255.

long: It induces compiler to take more bytes than usual.

short: It induces compiler to take less bytes than usual.

After short explanation of data types, let’s come to variables. Variables are shortly the areas storing numbers inside which allow us to use (by writing or reading) them. Instead of calling these variables like the second 4byte in the ram ve call them by a name, for example “a”. This is not really difficult. Here is a code segment explained until these point.

#include 

int main(){
    int a=3,b,c;
    long int d;
    unsigned long int e;
    float x,y,z;
    char k,l,m;
    unsigned char n;

    b=6;

    return 0;
}

Variables are defined like.

DATA_TYPE VARIABLE_NAME

We can assign vales while defining such variables(look at the variable a), or you can just define one and then use it later in your program(look at the variable b).  We can read or write the data inside these variables easily. Here is a code segment which explains that.

#include 

int main(){
    int a,b=3;
    float c;
    char d;
    scanf("%d %f %c", &a, &c, &d);
    printf("a: %d, b: %d, c: %f, d: %c", a,b,c,d);  
    return 0;
}

Another unknown thing; scanf. we can print something to the screen via printf function. scanf does the opposite takes data from the keyboard. For this program we expect 3 data from user to enter. It stores these data inside the a, c and d. “%d, %f, %c” are the format implying symbols to the compiler. It is used for both reading(scanf) and printing(printf) operations.

%d for integers.

%f for floating points.

%c for characters.

Introduction to Programming with C – First Program

The basic overview of C program is like that;

#include < stdio.h >

int main(){

    printf("Hello world!");
    /* prints hello world to the screen */
    return 0;
}

Let examine these lines one by one:

#include < stdio.h >

With “#include” command we say to compiler which header files our program needs and include them by putting the name of header file into < and > symbols. Standart input and output functions of C stand in that header file(stdio.h).

int main()

Actually main is a function and our program looks for a function called main then runs it. The “int” word implies that this function returns an integer data type. Data types and functions will be in my next posts.{ and } symbols express that the lines between them belong to that function.

printf("Hello world!\n");

printf is a function which prints a string to the screen. Sending a string between quotation marks is enough to call this function. “\n” is the new line character. “;” means this is the end of the line and this is compulsory. Unless we put semicolon end of the line, compiler sends a syntax error and does not compile our program.

/* prints hello world to the screen */

/* */ symbols prevent compiler from reading lines between them. So why we need these lines, if compiler does not read? These are for adding comments to our code. In other words, if anybody or we read the program again, understand better how any line works with these comments. If our comment is only one line length, we can use // symbol.

/*
This
is
a
multiple
comment
block
*/
// This is one line comment
return 0;

I’ve mentioned that main is a function which returns an integer. This line refers the end of the function and process. We use this to halt program.

This is it. We wrote the first C program. 😉

Introduction to Programming with C

Let’s give a brief introduction to C programming.

We do code like writing essays, and these codes include lots of english words. Anyone can easily become familiar with these expressions. However, translating these to computer-language is a challenging process.

Kod-Compiler-Linker

Our code segments get compiled (translated into Assembly or machine language) by a program(compiler). These states (.exe files) stands until we run them. When we double-click these exe files linker translates these codes with related libraries into computer language.

We can reach a point; we need a compiler. I use Dev C++ in Windows. Usually linux is installed with gcc compiler bu if not you can easily install it by writing these line in your terminal:

$ sudo apt-get install gcc

For linux distrubitions I advice you Geany editor, it is really practical. Of course you can write with Vim on terminal.

C programming files have “.c” extension. If you compile these files, you get exe files in the same directory..

In the next posts we will write these codes in such programs, and run after compilation.

I think this is enough for now.

For windows users: If you can not see anything when you run your program, your program halted quickly. You can add these lines at the end of your code.

system("PAUSE");
OR
getch();