Presentation is loading. Please wait.

Presentation is loading. Please wait.

Silberschatz and Galvin  1999 1.1 C Programming Language Kingdom of Saudi Arabia Ministry of Higher Education Al-Majma’ah University College of Education.

Similar presentations


Presentation on theme: "Silberschatz and Galvin  1999 1.1 C Programming Language Kingdom of Saudi Arabia Ministry of Higher Education Al-Majma’ah University College of Education."— Presentation transcript:

1 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 المملكة العربية السعودية وزارة التعليم العالي جامعة المجمعة كلية التربية بالزلفي قسم الحاسب الآلي Teacher: Chafika LAABIDI OUNI WANNASSI c.ouni@mu.edu.sa

2 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.

3 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.

4 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.

5 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.

6 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

7 Silberschatz and Galvin  1999 1.7 Example #include Int x; X=12; int main(x) { /*this is my first program in C */ printf("Hello, World! \n",x); return x; }

8 Silberschatz and Galvin  1999 1.8 Example (cont) The first line of the program #include 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.

9 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.

10 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

11 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;

12 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).

13 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

14 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. Autoelselongswitch Breakenumregistertypedef Caseexternreturnunion Charfloatshortunsigned Constforsignedvoid Continuegotosizeofvolatile Defaultifstaticwhile Dointstruct_Packed Double

15 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.

16 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.

17 Silberschatz and Galvin  1999 1.17 C Variables (cont) TypeDescription Char Typically a single octet(one byte). This is an integer type. Int The most natural size of integer for the machine. FloatA single-precision floating point value. DoubleA double-precision floating point value. VoidRepresents the absence of type.

18 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;

19 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';

20 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

21 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

22 Silberschatz and Galvin  1999 1.22 Input and Output Functions Input functions, called scanf Output functions, called printf

23 Silberschatz and Galvin  1999 1.23 Input and Output Functions (cont) Example: #include 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);

24 Silberschatz and Galvin  1999 1.24


Download ppt "Silberschatz and Galvin  1999 1.1 C Programming Language Kingdom of Saudi Arabia Ministry of Higher Education Al-Majma’ah University College of Education."

Similar presentations


Ads by Google