Vous êtes sur la page 1sur 21

C Basics

History of C
1969 By Dannis Ratchie On a DEC PDP-11 that used the UNIX Operating System Result of development process with an older language called BCPL 1970 A new language ''B'' a second attempt. c. 1971 A totally new language ''C'' a successor to ''B''. 1973 UNIX OS almost totally written in ''C''. 1990 ANSI C becomes available

Eastern University

C Basics (Cont.)

Characteristics of C
Small size. Extensive use of function calls. Structured language.
Pointer implementation - extensive use of pointers for memory, array, structures and functions.
Eastern University 2

C Basics (Cont.)

C- a widely used professional language


It It It It has high-level constructs. can handle low-level activities. produces efficient programs. can be compiled on a variety of computers.

Eastern University

C Library

All C programs include calls to various functions for performing input/output (I/O) operations, high-level mathematical operations, or character handling. Cs standard library contains these functions. All C compilers come with a standard library of functions that perform most commonly needed tasks.

Eastern University

C Linker

Linker combines the original program object code with the object code found in the standard library for the declared header file. This process is called the Linking. C compiler have their own linker.

Eastern University

The C Compilation Model


Source Code Preprocessor

Object Code for header file (From Library)

Compiler

Object Code Linker

Executable Code

Eastern University

C Program Structure (Cont.)


A C Program must have a main() function. A function has the form:


type function_name (parameters) { local variables C Statements }

Eastern University

C Program Structure (Cont.)

Sample Program

#include <stdio.h> main() { printf ("I Like C"); return 0; }

Eastern University

Programming in C
Constants, Variables & Data Types

Eastern University

C Tokens

Smallest individual units are known as tokens C has six types of tokens
Tokens

Keywords

float while

Constants

-15.5 100

Strings

ABC year

Operators

+ * /

Identifiers

ch amount

Special Symbols

[] ()

Eastern University

10

Keywords & Identifiers

All C words are classified as

Keyword Identifier
Keyword: All keywords have fixed meanings and these meanings can not be changed Keywords serve as basic building blocks for program statements Example: float, while, switch
Eastern University 11

Keywords & Identifiers (Cont.)

Identifiers: Identifiers refer to the names of variables, functions and arrays Identifiers are user-defined names and consists of a sequence of letters and digits Example: amount, salary

Eastern University

12

Identifiers (Cont.)

Rules for Identifiers:


First letter must be an alphabet (or underscore) Must consists of only letters, digits or underscore Only first 31 characters are significant Cannot use a keyword Must not contain white space

Eastern University

13

Constants

Constants refer to fixed values that do not change during program execution
Constants

Numeric Constants

Character Constants

Integer Constants

142 -453

Real Constants

.0084 -4.5

Single Character Constants

String Constants Hello

Eastern University

14

Basic Data Types

Five Basic Data Types:


character integer floating-point double floating-point

Eastern University

15

Modifiers

Basic data types have various modifiers preceding them which alters the meaning of the basic data types. The list of modifiers is

signed unsigned long short

Eastern University

16

Data Types
Type char / signed char unsigned char int / signed int unsigned int long int float double Size (Bits) 8 8 16 16 32 32 64 Range

-127 to 127 0 to 255 -32,767 to 32,767 0 to 65,535 -2,147,483,647 to 2,147,483,647 1E-37 to 1E+37 with 6 digits of precision 1E-37 to 1E+37 with 10 digits of precision

Eastern University

17

Variables

A variable is a named location in memory that is used to hold a value that can be modified by the program

A variable is a data name that may be used to store a data value


A variable may take different values at different times during execution

Eastern University

18

Variables (Cont.)

Variable declaration: type variable_list; It tells the compiler what the variable name is It specifies what type of data the variable will hold Example: int i,j; unsigned int s; double b;
Eastern University 19

Variable Initializations

The general form of initialization is: type variable_name=constant;

Example:
int y=10; char ch='A'; double b=143.56;

Eastern University

20

Local Variables
Variables that are declared inside a function are called local variables Example:

# include <stdio.h> # include <conio.h> int main() { int x; x=10; printf("%d", x); getch(); return 0; }
Eastern University 21

Vous aimerez peut-être aussi