BASIC C PROGRAMMING LANGUAGE TUTORIAL infobizzs.com
WHAT IS C LANGUAGE:C is mother language of all programming language. It is system programming language.
It is procedure-oriented programming language. It is also called mid level programming language.
infobizzs.com
HISTORY OF C LANGUAGE:C programming language was developed in 1972 by Dennis Ritchie at bell laboratories of AT&T(American Telephone & Telegraph), located in U.S.A. Dennis Ritchie is known as founder of c language. It was developed to be used in UNIX Operating system. It inherits many features of previous languages such as B and BPCL.
infobizzs.com
Language
year
Developed By
ALGOL
1960
International Group
BPCL
1967
Martin Richards
B
1970
Ken Thompson
Traditional C
1972
Dennis Ritchie
K&RC
1978
Kernighan & Dennis Ritchie
ANSI C
1989
ANSI Committee
ANSI/ISO C
1990
ISO Committee
C99
1999
Standardization Committee
infobizzs.com
FEATURES OF C LANGUAGE:There are many features of c language are given below. 1) Simple 2) Machine Independent or Portable 3) Mid-level programming language 4) structured programming language 5) Rich Library 6) Memory Management 7) Fast Speed 8) Pointers 9) Recursion 10) Extensible
infobizzs.com
FIRST PROGRAM OF C LANGUAGE:#include <stdio.h> #include <conio.h>
void main(){ printf("Hello C Language");
getch(); }
infobizzs.com
PREPROCESSOR DIRECTIVES #include <stdio.h> #include <conio.h> #include <stdlib.h> #include <string.h> #include<math.h> • The #include directives “paste” the contents of the files like stdio.h and conio.h into your source code, at the very place where the directives appear.
infobizzs.com
• These files contain information about some library functions used in the program: • stdio stands for “standard I/O”, • conio stands for “console I/O” • stdlib stands for “standard library”, • string.h includes useful string manipulation functions. • Want to see the files? Look in /TC/include
infobizzs.com
GLOBAL DECLARATION #define MAX_COLS 20 #define MAX_INPUT 1000
• The #define directives perform “global replacements”:
• every instance of MAX_COLS is replaced with 20, and every instance of MAX_INPUT is replaced with 1000
infobizzs.com
• commonly used stdio.h functions:
• printf() – Output function • Syntax: printf(“….”) ; • scanf() – Input function • Syntax: scanf(“format specifier”, &var,&var2…);
infobizzs.com
• commonly used conio.h functions:
• clrscr() • Used to clear the screen • getch() • Used to get a character from ouput screen to come into the edit screen.
infobizzs.com
• Comments • Text surrounded by /* and */ is ignored by computer • Used to describe program • int main() • Program’s execution starts from the main function • Parenthesis used to indicate a function • int means that main "returns" an integer value • Braces ({ and }) indicate a block • The bodies of all functions must be contained in braces
infobizzs.com
CHARACTER SET • 256 characters are allowed in C.
• • • • • •
A – Z : 65 to 90 a – z : 97 to 122 0 – 9 : 48 to 57 Special symbols[ #, &, `…] Control characters[\n, \t . ..] Graphic characters
26 26 10 32 34 128
Total
256
infobizzs.com
DATA TYPES oC
support several different types of data, which may be represented differently within the computers memory.
o Types 1] Primary • Integer • Float • Double • Character
2] Derived • Arrays • Pointers • Structure
infobizzs.com
3] User Defined
typedef enum
PRIMARY DATA TYPES Data Types 1] char signed char unsigned char
Byte 1 1 1
Format Specifier %c %c %c
2] short short signed int short unsigned int
2 2 2
%d %d %u
3] int signed int unsigned int
2 2 2
%d %d %u
infobizzs.com
Data Types
Byte
Format
Specifier 4] long long signed int long unsigned int
4 4 4
%l %ld %lu
5] float signed float unsigned float
4 4 4
%f %f %uf
6] double
8
%lf
7] Long Double
10
%Lf
infobizzs.com
TYPE CASTING • It is used to convert on data at a time. • We can have two types of type casting. • Implicit • Explicit
o
Implicit : This converts narrow type to wider type so that use can avoid the information to the system.
• Explicit : Explicit type casting is done by the programmer manually. When the programmer wants a result of an expression to be in particular type, then we use explicit type casting. This type casting is done by casting operators ‘( )’ and data type.
infobizzs.com
TYPE CASTING EXAMPLE #include <stdio.h> void main ( ) { int base, height, area; base = 5; height =3; area = (base * height)/2;
printf ( “Area = %d \n”, area); } Output : Area = 7
But Incorrect …………………….
infobizzs.com
#include <stdio.h>
void main ( ) { int base, height, area; base = 5; height = 3; area = ((float) (base * height)/2); //explicite type casting printf ( “Area = %d \n”, area); } Output : Area = 7.5 …………………….Correct
infobizzs.com
USER DEFINED DATA TYPE [A] Type Definition • Allows user to define an identifier that would represent an existing data type • Syntax: typedef type identifier • Eg: typedef int units; units batch1, batch2;
infobizzs.com
[B] Enumerated data type â&#x20AC;˘ Syntax: enum identifier { value1, value2... valuen} â&#x20AC;˘ Eg: enum day{ Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday}
infobizzs.com