Introduction to C programming By Avani M. Sakhapara Asst Professor, IT Dept, KJSCE
Classification of Software Computer Software System Software Application Software
Growth of Programming Languages
History ALGOL 60 too abstract and generic CPL (Combined Programming Language) at Cambridge University too big, many features, difficult to learn and implement BCPL (Basic Combined Programming Language) at Cambridge university by Martin Richards too specific, less powerful B language at AT & T s Bell Lab by Kem Thompson too specific C language = B language + BPCL + new features C language at AT & T s Bell Lab in USA by Dennis M. Ritchie in 1972
Features of C Robust language can be used to write any complex program Has rich set of built-in functions and operators well suited for writing both, system s/w and application s/w Dynamic Allocation of Memory
Structure of a C Program #include<stdio.h> /*multiline Comment*/ void main() { //single line comment printf( Hello all ); } #include->preprocessor directive stdio.h->standard input output header file which has printf function program execution always start from main() function printf() function is used to display some text on the output screen
Structure of a C Program #include<stdio.h> /*multiline Comment*/ void main() { //single line comment printf( Hello all ); }
Escape Characters These are non-printing characters and are represented by escape sequences consisting of a backslash (\) followed by a letter
List of Escape Characters
Exercise Write a C program to display the following 1. Display the below message Favourite colour: yellow 2. Display the table Roll no Class Div 123 SE A 124 FE B
Variables A variable is an entity used by the program to store values used in the computation Variable names are the names given to the memory location where the values are stored. The type of the variable depends on the type of the value it stores.
Rules for forming variable names It should begin with a letter or underscore( _ ) Followed by any combination of letters, underscores and digits 0-9 Eg: sum, item_price1, _sys valid names price$, #num invalid names The uppercase and lowercase letters are distinct in C. The variable names sum and Sum refers to different variables No commas or blank spaces allowed Length of variables depends on the compiler
Data types
Primary Data Types Data type keyword Size(in bytes) Range Purpose Format Specifier Character char 1-2 7 to 2 7-1 Store single = -128 to +127 character %c Integer int 2-2 15 to 2 15-1 = -32768 to +32767 Store numbers %d Float float 4-3.4e38 to +3.4e38 Store fractions Double double 8-1.7e308 to +1.7e308 Store smaller fractions %f %lf
Qualifiers Qualifiers can be used with basic datatypes short and long vary the size - short->used with int - long-> used with int and double signed and unsigned vary the range - used with char and int
Data type with qualifier signed short int /short int/int signed long int/long int/long unsigned short int /unsigned short/unsigned int unsigned long int/unsigned long Data types with Qualifiers Size(in bytes) Range Purpose Format Specifier 2-2 15 to 2 15-1 = -32768 to +32767 4-2 31 to 2 31-1 Store large numbers 2 0 to 2 16-1 =0 to 65535 Store numbers %d store positive numbers and zero 4 0 to 2 32-1 store large positive numbers and zero unsigned char 1 0 to 2 8 1 =0 to 255 store positive bytes long double 10 store small fractional values %ld %u %lu %c %Lf
Variable Declaration and Initialization All the variables must be declared at the beginning of the function before using it. Syntax: datatype var1, var2; Eg: int number, sum; float interest; Variable Initialization: var1=value1; var2=value2; Eg: number = 23; interest = 2.3f;
Variable Declaration and Initialization Variables can be initialized at the time of variable declaration using the following Syntax: datatype var1= value1; Eg: int number = 23;
Specifications of Different Constants
Example: Program to add two #include<stdio.h> void main() { int a, b=6, c; a=3; c = a + b; printf( sum = 9 ); printf( sum = %d, c); } numbers #include<stdio.h> void main() { int a, b=6, c; a = 3; printf( sum = %d, a + b); }
Using scanf() for input scanf() function is defined in <stdio.h> header file Syntax: scanf( format specifier,&varname); &varname address of(&) varname Eg: scanf ( %d,&a); Getting multiple values using scanf() - scanf ( %d %f, &a, &b);
Excercise Write a C program to accept two numbers from the user and swap them using i) third variable ii) Without using third variable Write a C program to accept the radius of the circle from the user and calculate the circumference and the area of the circle and display it.
Defining Constants Two ways for defining constants i) Using #define directive ii) Using const keyword 1) Using #define directive Syntax: # define identifier token-string The directive substitutes token-string for all subsequent occurrences of an identifier Called symbolic constants Usually written in uppercase to differentiate it from variables
Examples # include<stdio.h> #define SIZE 5 void main() { int a=3; //SIZE replaced by 5 a = SIZE * 2; printf( result= %d, a); SIZE= 10; //error } # include<stdio.h> void main() { const int size = 5; int a=3; //size replaced by value 5 a = size * 2; printf( result= %d, a); size= 10; //error }