170 likes | 450 Views
Fundamentals of Computer and programming in C. Programming in C Rohit Khokher. Programming in C. History of C Importance of C Structure of C Programs Character Set, tokens, key words & identifiers. Data Types Constants Symbolic class Variables Declarations Variables
E N D
Fundamentals of Computer and programming in C Programming in C RohitKhokher
Programming in C • History of C • Importance of C • 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 Self study Self study Already covered
Character Set, tokens, key words & identifiers. • Character Set • Letters (Uppercase A-Z, Lowercase a-z) • Digits 0-9 • Special characters
Tokens Operators Keywords Identifiers Constants Strings Special symbols +, -, *, /, … Numeric constants like -59.67 Character constants like “Year 2010” “Year 2010” • Variable names symbolizing memory addresses. • Rules for creating identifiers • 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 String Integer Real Backslash ‘\a’ bell sound ‘\f’ backspace ‘\n’ new line ‘\t’ horizontal tab ….. Decimal, Octal and Hexadecimal
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 void Primary Data Types integer Character Signed -128 to 127 Unsigned 0 to 255 floating Point signed unsigned Int short int long int unsigned int unsigned short int unsigned long int float Double Long double Exercise: Find the size and range of each data type
Example Data Segment # include <stdio.h> float x, y; int code; short int count; long int amount; double deviation; unsigned n; char c; /*--------------------*/ Main () { } x: 4 Bytes y; 4 Bytes code; 2 Byte count; 1 Bytes amount; 4 Bytes deviation; 8 Bytes n; 2 Bytes c; 1 Byte
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 enumday(Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday); Variable Declaration enumday 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 Global x, y are visible to Main and functionx x y i b i s Local to main The variables i and b are visible to main only 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 x, p; double y, q; unsigned k; int m=54321; long int n=1234567890; 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); }
Defining Synbolic Constants • #define directives • #define PI 3.14 • #define PASS_MARK 50 • #define MAX = 100 • #define STRENGTH 200