Introduction to C programming languageceng198.cankaya.edu.tr/uploads/files/ceng198...

27
Introduction to C programming language Computer Programming

Transcript of Introduction to C programming languageceng198.cankaya.edu.tr/uploads/files/ceng198...

Introduction to C programming language

Computer Programming

Course Information Instructor: Abdülkadir Görür

Office L210 Phone 2331340 Email: [email protected]

Instructor: Murat Saran

Office L209 Phone 2331333 Email: [email protected]

Instructor: Sibel Tarıyan Ozyer

Office L204 Phone 2331347 Email: [email protected]

Course is about: Introduction to C programming language

Topics

• The structure of a C program

• Variables

– Variable declaration

– Initialization

– Assignment

• Basic Input/Output operations

– printf

– scanf

The Structure of a C Program

#include<stdio.h> Declare which instructions will be used

void main() program always starts from main

{

/* variable declaration */

int i; Variables should be declared at the

beginning of the program

/* program statements */

printf(" A very simple C program \n");

}

Comments

Comments are used to explain the instructions

Comments are not executed

Any expression after // is considered as comment

Any expression between /* and */ is considered as

comment

Comments

Example

void main()

{

// this line is a comment

int t = 10;

/* this is another comment */

printf(“%d\n”, t);

}

Main Features of a C Program

A C program should have a main() part. The program

starts with this part.

The instructions of a C program are written inside the

body of main() which is shown with { and }

Each instruction ends with a ; character

The Structure of a C Program

#include<stdio.h> Declare which instructions will be used

void main() program always starts from main

{

/* variable declaration */

int i; Variables should be declared at the

beginning of the program

/* program statements */

printf(" A very simple C program \n");

}

Variables

The input or output data should be stored in memory

Memory locations for storing values temporarily are called

variables.

Variables are accessed by their names

The type of the data stored in a variable should be given

Declaring Variables

All variables should be declared before use

For declaration

Variable _Type Variable_Name;

Variable types: int (or long), float (or double),

char Ex. int i;

float account;

Declaring Variables

Variable name should start with a letter

Variable name cannot be a key word (like if, else, switch,

for, while, … )

No blank character is allowed in a name

_ (underscore) can be used in the variable names Ex.

New_box

A combination of letters and digits can be used as variable

names. e.g. Num2

Upper and lower case characters are different

Ex. Name and name are different

Initialization

Variables can be given initial values at declaration

Ex. float average=0;

char letter=‘A’;

Assignment

• “Assignment” gives values to variables

• Assignment is done by = operator

• At the left side of = symbol, we must have a variable name Ex. int speed_limit;

speed_limit = 50; Values are converted if they are not of the type of the variable

Ex. float delay; delay = 12; /* is converted to 12.0 */

int max;

max = 16.2; /* truncated to 16 */

Assignment

In character variables, the ASCII code of the character is

stored.

Ex.

int a;

char c;

c=‘A’;

a=c; /* at a we will have 65 (the ASCII code of ‘A’ ) */

Assignment

Multiple assignments are allowed in C

Ex.

int a,b,c;

a=b=c=5;

5 is stored in all variables

Basic Input / Output

Input instructions are used to get a value from the user and

store it in a variable

Output instructions are used to write a value to some output

device such as Monitor

Output Command: printf

printf( format string, variables );

Format string defines the variable types

%d decimal (int)

%f float

%c char

\n new line

Format string is written using “” marks

e.g. “%d\n” print an integer and then go to a new line

Examples

int a = 15;

float k=4.5;

char m=‘L’ ;

printf(“%d %f %c \n”,a,k,m);

printf(“First number is = %d\n”,a);

printf(“%5.2f”,k);

/* uses 5 digits totally including ‘.’ having 2 of them after ‘.’ */

Input Command: scanf

scanf( format string, &var1, &var2,..);

scanf has the same format as printf

Put an & character before the name of the variables

Example

int a;

float b;

char k;

scanf(“%d%f%c”,&a,&b,&k);

Example Read three integer, float and character values and print them #include<stdio.h>

void main() { /* variable declaration */ int i;

float R; char t_type; scanf("%d%f%c",&i,&R,&t_type); printf(" The values entered are %d and %f and %c \n“,i,R,t_type);

}

Escape Sequences

Escape sequences are used to define output format.

For example, to start a new line, or to print special

characters we use Escape sequences

Escape Sequences

Example

Write a C program to read 2 characters, an integer and a

floating point number . Then print them

#include <stdio.h>

void main() {

char c1,c2;

int num; float fnum;

scanf(“%c%c”,&c1, &c2);

scanf(“%d%f”,&num,&fnum);

printf(“The characters are : %c and %c \n”, c1,c2); printf(“The numbers are : %d and %f\n”, num, fnum); }

Summary

A C program contains a main() part which includes the

instructions

Variables are used to store values

Assignment operator (=) is used to give value to a variable

scanf and printf are used to read values and to write values

Questions?