24
Silberschatz and Galvin 1999 1.1 C Programming Language Kingdom of Saudi Arabia Ministry of Higher Education Al-Majma’ah University College of Education - Zulfi Computer Department ة ودي ع س ل ا ة ي ب ر لع ا كة ل م م ل ا ي ل عا ل ما ي ل ع ت ل ا ا"رة# ور عة م ج م ل ا عة ام ج ي# ف ل# ز ل ا ب ة ي ب ر لت ا ة ي ل ك ي ل6 الآ" ب س حا ل ما س قTeacher: Chafika LAABIDI OUNI WANNASSI [email protected]

Silberschatz and Galvin 1999 1.1 C Programming Language Kingdom of Saudi Arabia Ministry of Higher Education Al-Majmaah University College of Education

Embed Size (px)

DESCRIPTION

Silberschatz and Galvin  Features Easy to learn Structured language It produces efficient programs. It can handle low-level activities. It can be compiled on a variety of computer platforms.

Citation preview

Page 1: Silberschatz and Galvin  1999 1.1 C Programming Language Kingdom of Saudi Arabia Ministry of Higher Education Al-Majmaah University College of Education

Silberschatz and Galvin 1999 1.1

C Programming Language

Kingdom of Saudi ArabiaMinistry of Higher Education

Al-Majma’ah UniversityCollege of Education - Zulfi

Computer Department

السعودية العربية المملكةالعالي التعليم وزارة

المجمعة جامعةبالزلفي التربية كلية

اآللي الحاسب قسم

Teacher: Chafika LAABIDI OUNI [email protected]

Page 2: Silberschatz and Galvin  1999 1.1 C Programming Language Kingdom of Saudi Arabia Ministry of Higher Education Al-Majmaah University College of Education

Silberschatz and Galvin 1999 1.2

C Programming Language

• C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell Labs. C was originally first implemented on the DEC PDP-11 computer in 1972.

• The UNIX operating system, the C compiler, and essentially all UNIX applications programs have been written in C. The C has now become a widely used professional language for various reasons.

Page 3: Silberschatz and Galvin  1999 1.1 C Programming Language Kingdom of Saudi Arabia Ministry of Higher Education Al-Majmaah University College of Education

Silberschatz and Galvin 1999 1.3

Features

• Easy to learn

• Structured language

• It produces efficient programs.

• It can handle low-level activities.

• It can be compiled on a variety of computer platforms.

Page 4: Silberschatz and Galvin  1999 1.1 C Programming Language Kingdom of Saudi Arabia Ministry of Higher Education Al-Majmaah University College of Education

Silberschatz and Galvin 1999 1.4

Facts about C

• C was invented to write an operating system called UNIX.

• C is a successor of B language which was introduced around 1970

• The language was formalized in 1988 by the American National Standard Institute (ANSI).

• The UNIX OS was totally written in C.

Page 5: Silberschatz and Galvin  1999 1.1 C Programming Language Kingdom of Saudi Arabia Ministry of Higher Education Al-Majmaah University College of Education

Silberschatz and Galvin 1999 1.5

Facts about C (cont)

• Today C is the most widely used and popular System Programming Language.

• Most of the state-of-the-art softwares have been implemented using C.

• Today's most popular Linux OS and RBDMS MySQL have been written in C.

Page 6: Silberschatz and Galvin  1999 1.1 C Programming Language Kingdom of Saudi Arabia Ministry of Higher Education Al-Majmaah University College of Education

Silberschatz and Galvin 1999 1.6

Why to use C?

• C was adopted as a system development language because it produces code that runs nearly as fast as code written in assembly language. Some examples of the use of C might be:

Operating Systems

Databases

Functions

Variables

Statements & Expressions

Comments

Page 7: Silberschatz and Galvin  1999 1.1 C Programming Language Kingdom of Saudi Arabia Ministry of Higher Education Al-Majmaah University College of Education

Silberschatz and Galvin 1999 1.7

Example

#include <stdio.h>

Int x;

X=12;

int main(x)

{

/*this is my first program in C */

printf("Hello, World! \n",x);

return x;

}

Page 8: Silberschatz and Galvin  1999 1.1 C Programming Language Kingdom of Saudi Arabia Ministry of Higher Education Al-Majmaah University College of Education

Silberschatz and Galvin 1999 1.8

Example (cont)

• The first line of the program #include <stdio.h> is a preprocessor command, which tells a C compiler to include stdio.h file before going to actual compilation.

• The next line int main() is the main function where program execution begins.

Page 9: Silberschatz and Galvin  1999 1.1 C Programming Language Kingdom of Saudi Arabia Ministry of Higher Education Al-Majmaah University College of Education

Silberschatz and Galvin 1999 1.9

Example (cont)

• The next line /*...*/ will be ignored by the compiler and it has been put to add additional comments in the program. So such lines are called comments in the program.

• The next line printf(...) is another function available in C which causes the message "Hello, World!" to be displayed on the screen.

• The next line return 0; terminates main()function and returns the value 0.

Page 10: Silberschatz and Galvin  1999 1.1 C Programming Language Kingdom of Saudi Arabia Ministry of Higher Education Al-Majmaah University College of Education

Silberschatz and Galvin 1999 1.10

Data Types

• int - integer: a whole number,

• float - floating point value: ie a number with a fractional part,

• double - a double-precision floating point value.

• char - a single character.

• void - valueless special purpose type which we will examine closely in later sections

Page 11: Silberschatz and Galvin  1999 1.1 C Programming Language Kingdom of Saudi Arabia Ministry of Higher Education Al-Majmaah University College of Education

Silberschatz and Galvin 1999 1.11

Data Types (cont)

• Example:

int a;

Declares that you want to create an int variable called a. To assign a value to our integer variable we would use the following C statement: a=10;

Page 12: Silberschatz and Galvin  1999 1.1 C Programming Language Kingdom of Saudi Arabia Ministry of Higher Education Al-Majmaah University College of Education

Silberschatz and Galvin 1999 1.12

Identifiers

• A C identifier is a name used to identify a variable, function, or any other user-defined item.

• An identifier starts with a letter A to Z or a to z or an underscore _ followed by zero or more letters, underscores, and digits (0 to 9).

Page 13: Silberschatz and Galvin  1999 1.1 C Programming Language Kingdom of Saudi Arabia Ministry of Higher Education Al-Majmaah University College of Education

Silberschatz and Galvin 1999 1.13

Identifiers (cont)

• C does not allow punctuation characters such as @, $, and % within identifiers.

• C is a case sensitive programming language. Thus, Manpower and manpower are two different identifiers in C.

• Examples:

mohd zara abc move_name a_123

myname50 _temp j a23b9 retVal

Page 14: Silberschatz and Galvin  1999 1.1 C Programming Language Kingdom of Saudi Arabia Ministry of Higher Education Al-Majmaah University College of Education

Silberschatz and Galvin 1999 1.14

Keywords

• The following list shows the reserved words in C. These reserved words may not be used as constant or variable or any other identifier names.

Auto else long switchBreak enum register typedefCase extern return unionChar float short unsignedConst for signed voidContinue goto sizeof volatileDefault if static whileDo int struct _PackedDouble

Page 15: Silberschatz and Galvin  1999 1.1 C Programming Language Kingdom of Saudi Arabia Ministry of Higher Education Al-Majmaah University College of Education

Silberschatz and Galvin 1999 1.15

Whitespace in C

• Whitespace is the term used in C to describe blanks, tabs, newline characters and comments.

• Whitespace separates one part of a statement from another and enables the compiler to identify where one element in a statement, such as int, ends and the next element begins.

Page 16: Silberschatz and Galvin  1999 1.1 C Programming Language Kingdom of Saudi Arabia Ministry of Higher Education Al-Majmaah University College of Education

Silberschatz and Galvin 1999 1.16

C Variables

• A variable is nothing but a name given to a storage area that our programs can manipulate.

• Each variable in C has a specific type, which determines the size and layout of the variable's memory;

• The name of a variable can be composed of letters, digits, and the underscore character.

Page 17: Silberschatz and Galvin  1999 1.1 C Programming Language Kingdom of Saudi Arabia Ministry of Higher Education Al-Majmaah University College of Education

Silberschatz and Galvin 1999 1.17

C Variables (cont)

Type Description

Char Typically a single octet(one byte). This is an integer type.

Int The most natural size of integer for the machine.

Float A single-precision floating point value.Double A double-precision floating point value.Void Represents the absence of type.

Page 18: Silberschatz and Galvin  1999 1.1 C Programming Language Kingdom of Saudi Arabia Ministry of Higher Education Al-Majmaah University College of Education

Silberschatz and Galvin 1999 1.18

Variable Definition in C:

• A variable definition means to tell the compiler where and how much to create the storage for the variable. A variable definition specifies a data type and contains a list of one or more variables of that type as follows:

type variable_list;

• Examples:

int i, j, k;

char c, ch;

float f, salary;

double d;

Page 19: Silberschatz and Galvin  1999 1.1 C Programming Language Kingdom of Saudi Arabia Ministry of Higher Education Al-Majmaah University College of Education

Silberschatz and Galvin 1999 1.19

Variable Definition in C (cont)

• Variables can be initialized (assigned an initial value) in their declaration.

type variable_name = value;

Examples:

int d = 3, f = 5;

char x = 'x';

Page 20: Silberschatz and Galvin  1999 1.1 C Programming Language Kingdom of Saudi Arabia Ministry of Higher Education Al-Majmaah University College of Education

Silberschatz and Galvin 1999 1.20

Variable Definition in C (cont)

The % Format Specifiers, The % specifiers that you can use in ANSI C are:

•%c char single character

•%d (%i) int signed integer

•%e (%E) float or double exponential format

•%f float or double signed decimal

•%g (%G) float or double use %f or %e as required

Page 21: Silberschatz and Galvin  1999 1.1 C Programming Language Kingdom of Saudi Arabia Ministry of Higher Education Al-Majmaah University College of Education

Silberschatz and Galvin 1999 1.21

Variable Definition in C (cont)

• %o int unsigned octal value

• %p pointer address stored in pointer

• %s array of char sequence of characters

• %u int unsigned decimal

• %x (%X) int unsigned hex value

Page 22: Silberschatz and Galvin  1999 1.1 C Programming Language Kingdom of Saudi Arabia Ministry of Higher Education Al-Majmaah University College of Education

Silberschatz and Galvin 1999 1.22

Input and Output Functions

• Input functions, called scanf

• Output functions, called printf

Page 23: Silberschatz and Galvin  1999 1.1 C Programming Language Kingdom of Saudi Arabia Ministry of Higher Education Al-Majmaah University College of Education

Silberschatz and Galvin 1999 1.23

Input and Output Functions (cont)Example:#include <stdio.h>

main()

int num1, num2;

float average;

scanf("%d%d",& num1,& num2);

average = ( num1+num2 ) / 2 ;

printf("Here is the answer... ");

printf("\n");

printf("%f",average);

Page 24: Silberschatz and Galvin  1999 1.1 C Programming Language Kingdom of Saudi Arabia Ministry of Higher Education Al-Majmaah University College of Education

Silberschatz and Galvin 1999 1.24