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.
Variables Declarations
Variables
A variable as a constant A variable as volatile
Storage class
Tokens
Keywords Identifiers Constants Strings Special symbols
Year 2010 Operators
+, -, *, /,
*. +. ,. -. (.
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
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;
Example
# include <stdio.h> float int short int long int double unsigned char /*--------------------*/
Data Segment
Main () {
}
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.
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
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;