You are on page 1of 10

Programming 1

Introduction

Introduction
Why do we need it?
Where can we use it?
How can we write the code?
How the code is organized?
In this subject we will program in C language.

C language
Why do we learn C language as a structural
programming language?
C based languages
C++
C#
Java
As a compiler you can use CodeBlocks, or any other
C compiler (http://www.codeblocks.org/)

How can we write program in C language?


1.
2.
3.
4.

Declaration of the main functon


Declaration of varialbes and constants.
The main logic of the program
Showing the results

. Declaring some external functions (optional)


void main()
{
Declaration of the variables;
Main logic of the program;
Here we can use predeclared functions.
Final output or result.
}

Defining variables in C
There are multiple types of variables
integer (Real numbers)
float (Decimal point numbers)
char (Characters, single or string)

Our first C program


#include <stdio.h>
void main()
{
printf(Welcome to UIST!\n);
/* This will be printed on the screen */
}

Program for adding two


numbers
void main()
{
int a, b, c;
a = 5;
b = 10;
c = a + b; // c=15
}

Example
void main()
{
int a;
float p;
p=1.0/2.0; /* p=0.5 */
a=5/2; /* a=2 */
p=(1/2)+(1/8); /* p=0.0 */
p=3.5/2.8; /* p=1.25 */
a=p; /* a=1 */
a=a+1; /* a=2 */
}

Operators in C
Math operators
Addition +
Subtraction
Multiplication *
Division /
Power ^
Logical operators
Equal ==
Not equal !=
Greater > (Greater equal >=)
Smaller < (Smaller or equal <=)

Example
Write a program that will calculate this mathematical
expression x = 3/2 + (5 46*5/12)
#include <stdio.h>
void main()
{
float x;
x = 3/2 + (5-46*5/12);
printf(The value of x is %f\n, x);
}

You might also like