1 Introduction to C programming

10
Programming 1 Introduction

description

This is a lecture note from a class i attended by-Lecture 1-Dr. Sunil K. Jha Programing 1

Transcript of 1 Introduction to C programming

Programming 1Introduction

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. Declaration of the main functon2. Declaration of varialbes and constants.3. The main logic of the program4. 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 CThere 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

}

Examplevoid 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 CMath operators• Addition +• Subtraction –• Multiplication *• Division /• Power ^

Logical operators• Equal ==• Not equal !=• Greater > (Greater equal >=)• Smaller < (Smaller or equal <=)

ExampleWrite 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);

}