computer programming i lesson 2: introduction to c

41
Computer Programming I Lesson 2: Introduction to C Dr. Amani YAHYAOUI Istanbul Zaim University

Upload: others

Post on 31-Jan-2022

3 views

Category:

Documents


0 download

TRANSCRIPT

Computer Programming I

Lesson 2: Introduction to C

Dr. Amani YAHYAOUI Istanbul Zaim University

2

INTRODUCTION

To communicate with machines, we have to learn their languages.

Introduction

01111000010101011100=HELLO!!!!!!!

Use programming language (c, c++, java…) 011110000101

Convert to machine code

C is a structured programming language.

It is considered a high-level language because it allows the programmer to concentrate on the problem

at hand and not worry about the machine that the program will be using.

C language consist of some characters set, numbers and some special symbols. The character set of C

consist of all the alphabets of English language a to z, A to Z, Numeric 0,1 to 9 and some special

Symbols {,},[,],?,+,-,*,/,%,!,;, and more.

Introduction

First Program: Hello World in C

#include <stdio.h>

int main()

{

printf("Hello world\n");

}

CODE

OrSource CODE

COMPILE

Exectuble Program

First Program: Hello World in C

#include <stdio.h>

int main()

{

printf(«Hello world\n");

}

- A header file

- A statement which tells the compiler to insert the contents of stdio at that particular place.

C is a lightweight language ( we just include libraries)

Almost all c programs use the “stdio” or standard input/output library. Many also use the “math” library.

To use a library, include the header file at the top of the file.

Introduction

First Program: Hello World in C

#include <stdio.h>

int main()

{

printf(«Hello world\n");

}

- Program mostly a collection of functions“main” function special: the entry point“int” qualifier indicates function does return an integer

First Program: Hello World in C

#include <stdio.h>

int main()

{

printf(«Hello world\n");

}

I/O performed by a library function

Basic Data Types

Int, long – Used for integer types.

Float, double – Used for floating-point numbers (reel) types.

Char – Used for character types.

Bool – Logical data types for true (true) or false (wrong).

Void – Data type with no value

Basic Data Types

Int data type : int is used to define integer numbers.

{

int X;

X=5;

}

ValueNameType

5Xint

Basic Data Types

Float data type : int is used to define floating point numbers.

{

Float mile;

mile=5.6;

}

Basic Data Types

Double data type : double is used to define BIG floating point numbers. It reserves twice the storage for the number.

{

Double atoms;

atoms=2500000;

}

Basic Data Types

Char data type : defines characters.

{

char letter;

letter=‘a’;

}

Memory size for the Basic Data Types

Type Memory Size

int 2 Bytes

long 4 Bytes

float 4 Bytes

double 8 Bytes

char 1 Bytes

Example 1: Print a line of text

/* my second program*/

#include< stdio.h>

/* main function starts program execution*/

int main (void)

{

printf(" C programming ");

return 0; /* show that the program ended successfully*/

} / * End of main function * /

Line 1:

/* my second program*/

Starting with / *

and ending with * / is a comment line.

FREQUENTLY ERRORS: Starting with / * and not ending with */

FREQUENTLY ERRORS: Starting with / * and ending with /*

Example 1: Print a line of text

Example 1: Print a line of text

Line 2:

#include< stdio.h>

This header contains the standard input / output library. The compiler obtains functions like printf from this library.

Example 1: Print a line of text

Line 4:

int main (void)

It is part of the C program.

Every C program is executed starting from the main function.

Example 1: Print a line of text

Line 5+ Line 8

{ The left curly bracket shows the beginning of each function body

} The right curly bracket shows the end of the function.

These brackets are called {} program block (BLOCK)

Example 1: Print a line of text

Line 6

Printf(" C programming ");

Enables writing a string of characters between double quotes.

Each line must end with a semicolon, this is called a statement terminator.

Example 1: Print a line of text

Line 7

Return 0;

show that the program ended successfully

23

Printf AND Scanf

printf() and scanf() functions are library functions in C programming language which are available in C library

by default.

These functions are defined in “stdio.h” which is a header file in C language.

We have to include “stdio.h” file to make use of these printf() and scanf() library functions in C language.

Printf and Scanf

Printf:

In C programming language, printf() function is used to print the “character, string, float, integer, octal

and hexadecimal values” in the output screen.

We use printf() function with %d format specifier to display the value of an integer variable.

Similarly %c is used to display character, %f for float variable, %s for string variable.

To generate a newline, we use “\n” in C printf() statement.

Note: C language is case sensitive. For example, printf() and scanf() are different from Printf() and

Scanf(). All characters in printf() and scanf() functions must be in lower case.

Printf and Scanf

Printf: example program for c printf() function:

#include <stdio.h>

int main()

{

char ch = 'A';

string word= "fresh";

float flt = 10.234;

int no= 150;

printf("Character is %c \n", ch);

printf("String is %s \n" , word);

printf("Float value is %f \n", flt);

printf("Integer value is %d\n" , no);

return 0;

}

Printf and Scanf

OUTPUT:

Character is A

String is fresh

Float value is 10.23

Integer value is 150

Scanf:

In C programming language, scanf() function is used to read character, string, numeric data from keyboard.

We use scanf() function with %d format specifier to display the value of an integer variable.

Similarly %c is used to display character, %f for float variable, %s for string variable.

Printf and Scanf

Printf and Scanf

ValueNameType

10aint

ValueNameType

aint

ValueNameType

10aint

After Execution

Before Execution

Scanf: Example program for c scanf() function:

#include <stdio.h>

int main()

{

char ch;

printf("Enter any character \n");

scanf("%c", &ch);

}

Printf and Scanf

OUTPUT:

Enter any character

a

Printf and Scanf

KEY POINTS TO REMEMBER IN C PRINTF() AND SCANF():

1. printf() is used to display the output and scanf() is used to read the inputs.

2. printf() and scanf() functions are declared in “stdio.h” header file in C library.

Our next program will use scanf function and display the sum of two integers read from the

keyboard by using the printf function.

Solution 1+solution 2

Printf and Scanf

Printf and Scanf

Printf and Scanf

Example 2

#include <stdio.h>

int main(void)

{ int num1; /* 1st number input from user */

int num2; /* Second number input from user */

int total; /* the variable to store the result of the addition */

printf("Write number1:\n"); /* request */

scanf("%d", &num1); /* read an integer */

printf("Write number2:\n"); /* request */

scanf("%d" , &num2); /* read an integer */

total= num1+num2; /* assign to the total variable */

printf("total: %d \n", total); /* print sum */

} / * end of main function * /

Some Rules

SOME RULES:

Variable names can consist of letters and numbers.

The first character must be a letter.

Do not start with underline _ for variable names.

Lower case and UPPER CASE are detected differently. X and x are considered to be two different

variables.

At least the first 31 characters are meaningful in any variable name.

Key words like if, else, for, int, ... can not be used.

ç, Ç, Ö, ö, Ş, ş, Ğ, ğ, ü, Ü, I, ı can not be used!

Some Rules

The list of keywords that can not be used as a name:

ARITHMETIC IN C

Operator Description Example

+ Adds two operands. A + B = 30

− Subtracts second operand from the first. A − B = -10

* Multiplies both operands. A * B = 200

/ Divides numerator by de-numerator. B / A = 2

% Modulus Operator and remainder of after an integer division.

B % A = 0

++ Increment operator increases the integer value by one.

A++ = 11

-- Decrement operator decreases the integer value by one.

A-- = 9

The following table shows all the arithmetic operators supported by the C language. Assume variable A holds

10 and variable B holds 20.

ARITHMETIC IN C

The following example explain all the arithmetic operators available in C .

#include <stdio.h>

main() {

int a = 21;

int b = 10;

int c ;

c = a + b;

printf("Line 1 - Value of c is %d\n", c );

c = a - b;

printf("Line 2 - Value of c is %d\n", c );

c = a * b;

printf("Line 3 - Value of c is %d\n", c );

c = a / b;

printf("Line 4 - Value of c is %d\n", c );

ARITHMETIC IN C

c = a % b;

printf("Line 5 - Value of c is %d\n", c );

c = a++;

printf("Line 6 - Value of c is %d\n", c );

c = a--;

printf("Line 7 - Value of c is %d\n", c );

}

When you compile and execute the above program, it produces

the following result :

ARITHMETIC IN C

Line 1 - Value of c is 31

Line 2 - Value of c is 11

Line 3 - Value of c is 210

Line 4 - Value of c is 2

Line 5 - Value of c is 1

Line 6 - Value of c is 21

Line 7 - Value of c is 22

End