Anda di halaman 1dari 16

CMP 101 Fundamentals of Computer and programming in C

Programming in C Textbook: E. Balagurusamy, Programming in ANSI C, Third Edition, Tata McGraw-Hill Publishing company Limited, 2004

Programming in C

Self study History of C Self study Importance of C Already covered Structure of C Programs Character Set, tokens, key words & identifiers.

Data Types Constants


Symbolic class

Variables Declarations
Variables
A variable as a constant A variable as volatile

Storage class

Data overflow & underflow

Character Set, tokens, key words & identifiers.


Character Set
Letters (Uppercase A-Z, Lowercase a-z) Digits 0-9 Special characters
Comma Period Semicolon Colon Question mark Apostrophe Quotation mark Exclamation mark Vertical bar Slash Backlash Tilde Under score Dollar , . ; : ? ! | / \ ~ _ $ Percent sign Caret Asterisks Minus sign Plus sign Less than Greater than Left parenthesis Right parenthesis Left brace Right brace Number sign Left bracket Right bracket % ^ * + < > ( ) { } # [ ] Equal WHITE SPACES Blank space Horizontal tab Carriage Return New line Form feed =

Tokens
Keywords Identifiers Constants Strings Special symbols
Year 2010 Operators

Numeric constants like -59.67 Character constants like Year 2010

+, -, *, /,

Variable names symbolizing memory addresses. Rules for creating identifiers


1. 2. 3. 4. 5. The first character must be an alphabet or underscore. Must consists of letters, digits, or underscore May have 256 characters but only the first 31 characters are important Should be a keyword Should contain white spaces

*. +. ,. -. (.

Words with fixed meaning. Must be written in lower case. Example: main, int, float

Tokens
key words or reserve words (Prepare a list of ANSI C Key words) Constants Numeric Character Single
Backslash \a bell sound \f backspace \n new line \t horizontal tab ..

Integer
Decimal, Octal and Hexadecimal

Real

String

Example
#include <stdio.h> void main { printf (Integer values \n\n); printf (%d %d %d \n, 32767, 32767+1, 3267+10); printf (\n); } Output

Integer Values 3276 -32768 -32759


Why negative?

Data Types
Primary Data Types integer

void

Character
Signed -128 to 127 Unsigned 0 to 255

signed
Int short int long int

unsigned
unsigned int unsigned short int unsigned long int Exercise: Find the size and range of each data type

floating Point
float Double Long double

Variable Declaration
Data Type Character (8 bits) Unsigned Character Signed Character Integer (16 bits) Keyword char unsigned char signed char int Declaration in C char a, b, c; unsigned char a, b, c; signed char a, b, c; int x, y, z;

Short Integer (8 bits) Long Integer (32 bits)


Real (32 bits) Real (64 bits) Real (89 bits)

short int long int


float double long double

short int x, y, z; long int x, y, z;


Float x,y,z; double x, y, z; Long double x, y, z;

Example
# include <stdio.h> float int short int long int double unsigned char /*--------------------*/
Data Segment

x, y; code; count; amount; deviation; n; c;

x: y; code; count; amount; deviation; n; c;

4 Bytes 4 Bytes 2 Byte 1 Bytes 4 Bytes 8 Bytes 2 Bytes 1 Byte

Main () {
}

User defined Data types


typedef
User can define type using typedef feature.

Example typedef int unit; typedef float marks; Unit symbolizes int and marks float. unit x, y, z; means x, y and z are integer type. float a, b, c; means a, b, and c are float type.

User defined data type


enum (enumerated type)
enum identifier ( value1, value2, , valuen)

Example
Definition enum day (Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday); Variable Declaration enum day week_st, week_end; These declaration day week_st and week_end can take values in (Monday to Sunday) and we can use them as week_st = Monday; week_end=Friday;

Storage Class
Location and visibility of variables.
# include <stdio.h> float x, y; void main () { int i; float b; -- - functionx (); } float functionx (); { int i; float s; }
Data segment x i i y b s Local to main The variables i and b are visible to main only Global x, y are visible to Main and functionx

Local functionx The variables i and s are visible to functionx only

Storage class and their meaning


auto : known within the function it is declared. static: a local variable that retains its velue even after the control is transferred to calling program. extern: global variable known to all functions. register: a local variable that is stored in a register.

Assigning values to variables


Assignment statement
Assignment statement is defined as
identifier = expression where expression give a value.

Example fee = 20.5; rent = 56; expense = fee + rent;

Programing example
# include <stdio.h>
void main() { float double unsigned k; int long int x = 1.2345678900; y= 9.87654321; k= 54321; P=q=1.0; printf ( m = %d \n, m); printf ( n = %ld \n, n); printf ( x = %.121f \n, x); printf ( x = %f \n, x); printf ( y = %.121f \n, y); printf ( y = %f \n, y); printf ( k = %u p = %f q = %.121f \n, k,p,q); } x, p; y, q;

m=54321; n=1234567890;

Defining Synbolic Constants


#define directives
#define PI 3.14 #define PASS_MARK 50 #define MAX = 100 #define STRENGTH 200

Anda mungkin juga menyukai