C Programming
Prepared By
K.Kavitha
History of C
• The C programming language was designed by
Dennis Ritchie at AT and T Bell Laboratories in 1972
• Evolution of C,
– ALGOL 60 (1960),
– CPL (Cambridge, 1963),
– BCPL (Martin Richard, 1967),
– B (Ken Thompson, 1970)
– C (Dennis Ritchie, 1972 )
Features of C
• Simple and General purpose language
• Structured programming language
• Machine Independent or Portable
• Mid-level programming language
• Rich Library
• Memory Management
• Fast Speed
• Pointers
• Recursion
• Extensible
Structure of C Program
Simple C Program
Example
/*Simple program*/
#include <stdio.h>
void main()
{
int a;
printf("Enter an integer ");
scanf("%d", &a);
printf("\nInteger that you have entered is %d", a);
getch();
}
Output:
Enter an integer
4
Integer that you have entered is 4
Program:
/*Addition of two number program*/
#include<stdio.h>
void main()
{
int a, b, c;
printf("Enter two numbers to add");
scanf("%d%d",&a,&b);
c = a + b;
printf(“\nSum of two numbers = %d",c);
getch();
}
Output:
Enter two numbers to add 4 2
Sum of two numbers = 6
/*Addition of two number program*/
#include<stdio.h>
int a,b;
int add(int,int);
void main()
{
int c;
printf("Enter two numbers to add");
scanf("%d%d",&a,&b);
c = add(a,b);
getch();
}
void add(int a,int b)
{
int c;
c = a + b;
printf(“\nSum of two numbers = %d",c);
C Tokens
• Keywords (eg: int, while),
• Identifiers (eg: sum,a1, total),
• Character Set (eg: Alphabets, Digits)
• Delimiters (eg: : ; , () {} [] # )
• Constants (eg: 10, 20),
Literal constants – Interger ,float,character,string
Qualified contants- const int a=10;
• Strings (eg: “total”, “hello”),
Constants
Constants refer to fixed values that the program may not alter
during its execution.
Variables :
• variable in simple terms is a storage place
which has some memory allocated to it.
• variable used to store data.
Syntax:
datatype varName1, varName2;
Eg:
int a,b,c;
int a=4,b=2;
Data Types in c
• Data types are declarations for memory
locations or variables that determine the
characteristics of the data.
Data Types in c
Data Type Memory (bytes) Range Format Specifier
char 1 -128 to 127 %c
int 2 -32,768 to 32,767 %d
float 4 3.4E-38 to3.4E+48 %f
double 8 1.7E-308 to1.7E+308 %lf
Enumeration Constants
• Enumeration (or enum) is a user defined data type in C.
• It is mainly used to assign names to integral constants, the
names make a program easy to read and maintain.
• data type is 2 byte. It work like the Integer.
Eg:
enum State {Working = 1, Failed = 0};
#include<stdio.h>
#include<conio.h>
enum week {sun, mon, tue, wed, thu, fri, sat};
void main()
{
enum week today;
today=tue;
printf("%d day",today+1);
getch();
}
Output
3 day