Unit 1: Introduction to C Language Saurabh Khatri Lecturer Department of Computer Technology VIT, Pune
Introduction to C Language The C programming language was designed by Dennis Ritchie at Bell Laboratories in the early 1970s Traditionally used for systems programming. In fact C Language was part of Unix OS development. Standardized in 1989 by ANSI (American National Standards Institute) known as ANSI C.
Introduction to C Language A C development environment includes System libraries and headers: a set of standard libraries and their header files. Compiler: converts source to object code for a specific platform Linker: resolves external references and produces the executable module
C as Middle Level Language Allows Inline assembly code. Allows to access system registers. Allows to access memory directly. C has the efficiency as close to the assembly language.
Basic Structure of C program System Libraries and Preprocessor Directives void main() { variables statements.. } Functions { }
My First C Program #include <stdio.h> #include <stdlib.h> void main() { printf( Hello World\n ); }
Preprocessor Directives Preprocessor directives are the statements that is not part of C code. However these statements are processed by a pre processor. These lines are always preceded by a hash sign (#) Eg: #include, #define
#define #define identifier replacement When the preprocessor encounters this directive, it replaces any occurrence of identifier in the rest of the code by replacement. #define TABLE_SIZE 100 int table1[table_size]; int table2[table_size]; After the preprocessor has replaced TABLE_SIZE, the code becomes equivalent to: int table1[100]; int table2[100];
#include When the preprocessor finds an #include directive it replaces it by the entire content of the specified file. #include <file> Ex: #include<stdio.h> contains the functions printf, scanf, etc
System libraries and Header Files A header file is a file containing C declarations and prototypes for standard functions. There are some predefined operations like: 1. String handling concatenate, length, etc 2. Mathematical computations Sine, Cosine, etc 3. Input/output : printf, scanf, etc Location of Header File : Windows: Linux: c:/tc/include /usr/local/include or /usr/include/i386-linux-gnu or /usr/include
Main Function The main function is generally the first programmerwritten function. It runs when a program starts It is invoked directly from the system-specific initialization. Syntax : void main()
Syntax and Semantics Syntax: The form of writing the statements in a programming language. These are the rules to be followed to write correct code in a Programming language. Semantics is the meaning of those statements and thus the after effect. For example, the semantics for the syntax for (int i=0; i<10; i++) x += i; ----------in C and FOR i IN 0..9 LOOP x := x+i; END LOOP; ------in Ada
Variables and Constants Reincarnation from Mathematics. A variable is a data name that can be used to store a data value. int p = 10; Constants: Its value cannot be changed by the program during its execution. Literal constant : Hello, 7, 1.345 ----- Literals are used to initialize the variables. Named constant : const int p = 3;
Data Types Data types refers to an extensive system for declaring variables of different types. Declaring a data type of a variable speaks of the range and type of the values can be stored in the variable. Example: int n ; float x; char c; Boolean false;
Data Types Type Storage size Value range char 1 byte -128 to 127 or 0 to 255 Int 2 or 4 bytes -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647 float 4 byte 1.2E-38 to 3.4E+38
Variable declaration int n ; Tells the compiler that n will be used to store an integer value. n is a memory location. n corresponds to some memory address. int n = 10; operator // Variable initialization using assignment All what previous statement does + Stores 10 to that memory location.
Comment line statements Single line ------ // Multiple line ---- /* this is multiple comment line */
Operators : Arithmetic + _ * / % ^
Operators : Logical AND NOT OR
Operators : Relational ==!= > > >= <=
Operators : Bitwise
Increment and Decrement Operator Post Increment a++ Pre Increment ++a Post Decrement a-- Pre Decrement --a
Expressions A Mathematical equation Ex: a = 5*3/(3+4) ; b = (c-d)/(4*d) ;
a =5; b = 7; c = 10; d = 15; result = c+d/a; result = (c+d)/a; result = c+d/b;
A = 0011 1100 B = 0000 1101 ----------------- A&B 0000 1100 A B 0011 1101 ~A 1100 0011
Points to Note What is the difference! A and ~A What is the difference A&B and A&&B A B and A B
Precedence
Versions of Printf Statement The printf() function is used to print the character, string, float, integer, octal and hexadecimal values onto the output screen. To display the value of an integer variable, we use printf statement with the %d format specifier. Similarly %c for character, %f for float variable,%s for string variable, %lf for double, %x for hexadecimal variable. To generate a newline, we use \n in C printf statement.
#include <stdio.h> int main() { char ch = 'A'; float flt = 10.234; int no = 150; double dbl = 20.123456; printf("character is %c \n", ch); printf("float value is %f \n", flt); printf("integer value is %d\n", no); printf("double value is %lf \n", dbl); printf("octal value is %o \n", no); printf("hexadecimal value is %x \n", no); }
Output Character is A Float value is 10.234000 Integer value is 150 Double value is 20.123456 Octal value is 226 Hexadecimal value is 96
# include <stdio.h> int main() { int a=40,b=20, add,sub,mul,div,mod; add = a+b; sub = a-b; mul = a*b; div = a/b; mod = a%b; printf("addition of a, b is : %d\n", add); printf("subtraction of a, b is : %d\n", sub); printf("multiplication of a, b is : %d\n", mul); printf("division of a, b is : %d\n", div); printf("modulus of a, b is : %d\n", mod); }
Output Addition of a, b is : 60 Subtraction of a, b is : 20 Multiplication of a, b is : 800 Division of a, b is : 2 Modulus of a, b is : 0
#include <stdio.h> int main() { int m=40,n=20; if (m == n) { printf("m and n are equal"); } else { printf("m and n are not equal"); } }
Output m and n are not equal
#include <stdio.h> int main() { int m=40,n=20; if (m>n && m!=0) { printf("m is greater than n and not equal to 0"); } }
Output m is greater than n and not equal to 0
#include <stdio.h> int main() { int m=40,n=20,and_opr,or_opr,xor_opr ; AND_opr = (m&n); OR_opr = (m n); XOR_opr = (m^n); printf("and_opr value = %d\n",and_opr ); printf("xor_opr value = %d\n",xor_opr ); printf("or_opr value = %d\n",or_opr ); }
Output AND_opr value = 0 XOR_opr value = 60 OR_opr value = 60
Homework a =5, b = 10, x = 5; (a/b)*b + a%b a x = (x << 1) 1; y = x++ + --x; a /= b * 5; 0 11 8 0
Input Statement scanf() function scanf(" format string ", arguments ); %c" - Reads a single character "%d" - Reads a signed integer (int) numbers ( white space is used to separate multiple numbers ) %f for float values