0% found this document useful (0 votes)
6 views37 pages

1.Basics of C Programming

The document provides an overview of the C programming language, detailing its history, features, and structure. It includes examples of simple C programs, explanations of C tokens, identifiers, constants, variables, data types, and the compilation process. Additionally, it covers derived data types such as arrays and enumerations, along with their usage in C programming.

Uploaded by

Celin Narayanan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views37 pages

1.Basics of C Programming

The document provides an overview of the C programming language, detailing its history, features, and structure. It includes examples of simple C programs, explanations of C tokens, identifiers, constants, variables, data types, and the compilation process. Additionally, it covers derived data types such as arrays and enumerations, along with their usage in C programming.

Uploaded by

Celin Narayanan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 37

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”),
Rules for identifier
• The first letter of an identifier should be either a letter or an underscore.
• A valid identifier can have letters (both uppercase and lowercase letters), digits and
underscores.
• There is no rule on length of an identifier. However, the first 31 characters of
identifiers are significant by the compiler.
• Must not contain white space
• Can not use Keyword as a identifier
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;
Compiling and Executing the program
• The compilation is a process of converting the source code
into object code.
• It is done with the help of the compiler.
• The compiler checks the source code for the syntactical or
structural errors, and if the source code is error-free, then it
generates the object code.
• Preprocessoexpanding the source code
• Compiler– >convert expanding code to assembly code
• Assembler 
The assembly code is converted into object code by using an assembler.
The name of the object file generated by the assembler is the same as the source
file. The extension of the object file in DOS is '.obj,' and in UNIX, the extension is
'o'. If the name of the source file is 'hello.c', then the name of the object file would
be 'hello.obj'.
• Linker -combine the object code of library files with the object code of our
program
Mainly, all the programs written in C use library functions.
These library functions are pre-compiled, and the object code of these library files
is stored with '.lib' (or '.a') extension.
Data Types in c
• Data types are declarations for memory locations or variables
that determine the characteristics of the data.
• Built in datatype (primary datatype or primitive )
• Userdefined data type

Eg:
• Name: String
• ID: Integer
• Salary: Float or Double
• Phone No: String
Data Types in c
Data Type Example of Data Type

Integer, Floating-point,
Primary Data Type
double, string.

Derived Data Type Union, structure, array, etc.

Enumerated Data Type Enums

Void Data Type Empty Value

Bool Type True or False


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


Int datatype
#include <stdio.h>
void main()
{
int i = 5;
printf("The integer value is: %d \n", i);
}
Char datatype
#include <stdio.h>
void main()
{
char c = 'b';
printf("The character value is: %c \n", c);
}
Float datatype
#include <stdio.h>
void main()
{
float f = 7.2357;
printf("The float value is: %f \n", f);
}
Double datatype
#include <stdio.h>
void main()
{
double d = 71.2357455;
printf("The double value is: %lf \n", d);
}
Modifiers
• long
• short
• signed
• Unsigned
Derived data type
• group many elements of similar data types
• Array
• Pointers
• Structure
• Union
Array
#include<stdio.h>
int main(){
int i=0;
int marks[5];//declaration of array
clrscr();
marks[0]=50;//initialization of array
marks[1]=60;
marks[2]=75;
marks[3]=40;
marks[4]=85; //traversal of array
for(i=0;i<5;i++){
printf("%d \n",marks[i]); }
return 0;
}
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

You might also like