Você está na página 1de 18

1

Brief Notes on C Structure of C program Include files #include <stdio.h> //for input/output functions Define Constant #define const_name value main program main() { /* each declarations and statements are separated by semi colon */ // declarations variables arrays records function declarations etc // statements } // function definitions

Comments are enclosed within /* comments on multiple lines */ // single line comment

Compiler Directives #include directive is used to include the header file for input/output, the standard mathematics library math.h etc. These files are enclosed within < >

#define directive helps in defining constant symbol

3

Datatypes Standard: int, float, char, double User defined datatypes: structures, pointers, arrays, enumerated datatype etc.

Declaration int x, y, z; float p, q[3][4]; char name[20];

array

Expression x = x + 2 x += 2 i = i +1 i++ or ++i x = x +(++i); the value of x is added with the value of i after incrementing it by 1.

x = x + (i++); the value of x is added with the value of i and then i is incremented by 1. Similarly, x = x+(--i); x = x+(i--); Example: /* include header file stdio.h that contains I/O functions. */ #include <stdio.h> // constant definition #define i 6 main() { // integer declaration int x, y; // Assignment statements x=7; y= i + x; // output statement printf("%d\n", y); }

Conditional Expression

exp ? exp1 : exp2 /* An expression exp is evaluated and if the value is nonzero (or true represented by 1) then expression exp1 is the final value otherwise exp2 is the final value of entire expression. */

Basic Statements

Assignment statement x = expression Compound statement { s1; s2; . }

/* Collection of statements, each separated by semi colon and enclosed in brackets */


Conditional statements i. if (cond) statement ii. if (cond) s1 else s2; /* Here cond is a boolean condition which can have value 1 (for true) or 0 (for false). */

For statement for (i = m1; i <= m2; i+=m3) {Body};block of statements /* Here m1 : initial value; m2:maximum value of i m3 : increment (positive or negative) */

While statement while (cond) { Body };

Do-while statement do {Body } while cond;

Logical Operators && || ! AND OR NOT

8

Bitwise operations & | ^ << >> ~ bitwise AND bitwise inclusive OR bitwise exclusive OR left shift right shift One's complement

Relational Operators == < <= > >= != equality less than less than equal to greater than greater than equal to Not equality

9

Switch statement switch (exp) { case v1 : s1 ; break; case v2 : s2 ; break;

case vn : sn ; break; optional default : s }

/* If the value of exp is vj then sj is executed and switch statement is exited using break statement. Execution always starts from 1 to last. */

10

Input/Output statement /* reads single character and stores in character variable x */ x = getchar(); /* prints single character stored in x */ putchar(x); /* the following functions are in standard file named stdio.h */ scanf(control, v1, v2, ..); printf(control, e1,e2,...);

Control in input/output control = "sequence of format descriptor"

11

Format descriptor %d %o %c %s %f \n

Meaning a decimal integer a octal integer a single character a character string a decimal number (float or double) skip to new line

Examples: i. printf("%4d%7.2f\n%c\n",x, y, z) ii. scanf("%4d%8.2f \n",&x, &y) Here & represents memory addresses

Structure in C : Used for record declaration struct person { char name[25]; float salary; char address[35]; } p_rec;

12

Here p_rec is user defined structure datatype Declaration using above define structure is as follows: struct person x, y; OR p_rec x, y; Can define structure within structure Fields of structure variable are accessed as: x . salary, y . name etc.

13

Subprograms type fun_name(parameter along with type) { // local declaration; body; } type : is the type of value returned by the function and can be basic type or user defined. return: statement is used in the body of a function.

14

Example: #include <stdio.h> main() { int i, x; // function declaration int power (x, n); for (i =0; i < 10; ++i) { x = power(2, i); printf("%d%d\n", i, x); } } // function definition int power(int x, n) { int i, p; p = 1; for (i =1; i <=n; ++i) p = p*x; return (p); }

15

Parameter Passing

Default parameter passing is by value Call by reference can be achieved by passing addresses as an actual parameters corresponding to pointer variables in formal parameter list.

Pointer variable int *p, *q, i, t ; float *x, *y; p & q are pointer variables pointing to locations holding integer values. x & y are pointer variables pointing to locations holding real values. i and t are simple integer variables.

16

Possible Assignments // initializing pointer variable. p = NULL /* address of t is stored in pointer variable p. */ t = 2; p = &t; /* contents of t are added with that pointed out by p and assigned to i */ i = t + (*p); /* illegal as i is a simple variable and p is a pointer variable */ i=p /* value pointed out by x is assigned to simple variable z */ *x=34.5; z = *x; /* releasing the memory location pointed out by p */ free(p);

17

Illustration of pointer variables: p q 20 30

p *p = *q q

30

30

dangling p p=q q 30 20

18

Example: Explaining call by reference void swap (int *p,*q) { int t; t = *p; *p = *q; *q = t; } Void type means that the function is not returning any value. So return statement is not used when return type is void. Corresponding call statement x = 4; y = 5; // addresses are passed swap(&x, &y);

Você também pode gostar