Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 35

Programming in c

By Vishal Vanaki

A brief history of c

Programming languages are used to specify, design, and build software systems.. Dennis Ritchie(1969-1973). Standardized by ANSI in 1982. To solve the performance problems of B a new language was created: C declaration of data types definition of data structures

C as a programming language

C has been standardized (ANSI C) and spawned new languages (C++, Stroustrup, 1986) that improve C

The basic characteristics of C are: small in size


loose typing (lots of freedom, error prone) structured (extensive use of functions) C is higher level than assembler

it provides higher level language constructs (functions, data structures)

Basics of C programming

Keywords and Identifier


All keywords have fixed meanings Basic building blocks for program statements. 32 keywords(in C88) & 32+5 keywords(in C99) Identifiers refer to the names of variables, functions and arrays.

Basic Structure
preprocessor statements global declarations; main( ) { declarations; statements; } User defined function

The c compilation model


-The Preprocessor accepts source code as input and removes comments, extends the code according to the preprocessor directives included in the source code (lines starting with #) -The Compiler takes the output of the preprocessor and produces assembly code -The Assembler takes the assembly code and produces machine code (or object code) -The Linker takes the object code, joins it with other pieces of object code and libraries and produces code that can be executed

Constants
(a)

Integer Constants
-sequence of digits. 123

(b) Floating-point constants


-Real numbers 12.1234

(c) Character Constants


-Alphabates a,b

(d) Backslash Character Constants:


- escape sequences \t,\n

(e) String constants -Sequence of characters SJBIT

Variables

Name that may be used to store a data value

Rules:

The variables must always begin with a letter ANSI standard recognizes a length of 31 characters Uppercase and lowercase are significant. The variable name should not be a keyword. White space is not allowed.

Declaration of Variables
data_type variable_name; Fundamental types: (primary) Data type Size int 2 float 4 char 1

Managing Input/Output Operation

Managing Input/Output Operation


Types of I/O functions -Formatted -Unformatted
Type Formatted Unformatted Input function scanf() getchar() gets() Output function printf() putchar() puts()

Format Specifiers

d c f x o

decimal integer character floating point hexadecimal octal

scanf(%d %f %e, &a , &b, &c); printf (%d %f %e, a , b, c);

Operators in C

Operators

Relational Operators(==,!=,<,>,<=,>=) Logical Operators( &&, ||, !) Assignment Operators(+,-,*,/,%) Bitwise Operators(&, |,^,~) Increment and Decrement Special Operators Conditional Operator

Increment/Decrement Operators
Increment Operator Prefix: First increment then use it Example: a=10; b=++a; Postfix: first use it then increment Example: a=10; b=a++;

Special operators
sizeof() operator int a,b; b=sizeof(a);

Pecedence

All operators work from left to right Except following: Prefix increment/decrement. Unary plus/minus. Logical not/bitwise complement. typecast (change type). *(Dereference.) &(address) sizeof

Control Statements

Control Statements

If If else Nested if else Elseif ladder Switch Break continue;

If Statement
if ( condition ) { statements; } Example: if(a==1) { a=10; }

If else statement
If (condition) { statement1; } Else { staement2; }

Nested if else statement


If (condition1) { statement1; if (condition2) { statement2; } } Else { staement3; }

Elseif ladder
If (condition1) { statement1; } elseif(condition2) { staement2; } else { statement3; }

Switch statement
Switch(expression) { case value1: statement 1; break;

case value2:
statement 2; break; default:

statement 3; break;
}

Loops in C

While loop Do-while loop For loop

While Loop
while(condition) { statements; }

Also called as entry controlled loops

Do-while Loop
do { statements;

}while(condition);

Also called as exit controlled Loops

For Loop
for( initialization ; condition ; Increment/decrement) { statements; } Example: for( ; ; ) { }

Arrays

Arrays

A group of related data items that share a common name. One-dimensional array: data_type array_name[size]; int a[4]; a[0]
a[1] a[2] a[3]

Initialization of 1D array
int a[3]={1,2,3}; Reading of 1D array:

for(i=0;i<3;i++) { scanf(%d,&a[i]); }

Two-dimensional Array
data_type array_name [rows][columns] int a[2][2] a[0][0], a[0][1], a[1][0], a[1][1]

Generally used for matrix

Initialization of 2D array
int a[2][2]={1,2,3,4} Reading of 2D array:
For(i=0;i<2;i++) { for(j=0;j<2;j++) { scanf(%d,&a[i][j]); } }

Thank you - Vishal Vanaki.

You might also like