1. MCA DEPARTMENT | C Basics
C
1
History & Evolution:
B
BCPL
C - with Unix 1962- Dennis Ritchie
At: AT &T BELL Labs,US.
Designed for systems programming:
• Operating systems
• Utility programs
• Compilers
• Filters
Characteristics:
Currently, the most commonly-
used language for embedded
systems
“High-level assembly”
Very portable: compilers exist for
virtually every processor
Easy-to-understand compilation
Produces efficient code
Fairly concise
Structure of C Program:
(1) Preprocessor Commands
(2) Functions
(3) Variables
(4) Statements & Expressions
(5) Comments
Example:
#include <stdio.h>-------------(1)
int main()---------------------(2)
{
/* my first program in C */--(5)
int a;-------------------------(3)
printf("Hello, World! n");----(4)
return 0;
}
Compilation & Execution:
(1) Press F2- to Save
(2) Press F9 – Compilation
(3) Press Ctrl+F9 - Execution
Tokens in C:
1) Keyword
2) Identifier
3) Constant
4) Comments
5) Symbol
1) Keywords
auto else long switch
break
enum register
typedef
case
extern
return union
char float short unsigned
const for signed void
continue goto sizeof volatile
default if static while
do int struct _Packed
double
2) Identifier
A name used to identify a variable,
function, or any other user-defined
item.
An identifier starts with a letter A
to Z or a to z or an underscore _
followed by zero or more letters,
underscores, and digits (0 to 9).
C does not allow punctuation
characters such as @, $, and %
within identifiers.
C is a case sensitive programming
language.
3) Constant
The constants refer to fixed values
that the program may not alter
during its execution. These fixed
values are also called literals.
1. Using #define preprocessor.
2. Using const keyword.
1. The #define Preprocessor
#define identifier value
// example for preprocessor
#include <stdio.h>
#define LENGTH 10
#define WIDTH 5
#define NEWLINE 'n'
int main()
{
int area;
area = LENGTH * WIDTH;
printf("value of area : %d", area);
printf("%c", NEWLINE);
return 0;
}
Output:
value of area : 50
2.The const Keyword
Use const prefix to declare constants
with a specific type as follows:
const type variable = value;
// Example for Const keyword
#include <stdio.h>
int main()
{
const int LENGTH = 10;
const int WIDTH = 5;
const char NEWLINE = 'n';
int area;
area = LENGTH * WIDTH;
printf("value of area : %d", area);
printf("%c", NEWLINE);
return 0;
}
Output:
value of area : 50
4) Comments
Single line comment - //
Multiline Comment - /* --*/
5) Symbol
Punctuators - : , , ;.
Escape sequences
Escape
sequence
Meaning
character
' ' character
" " character
? ? character
a Alert orbell
b Backspace
f Form feed
n Newline
r Carriage return
t Horizontal tab
v Vertical tab
ooo Octal number ofone to three digits
xhh . . . Hexadecimal number of oneor more digits
Input output function:
Built-in functions to read given input and
write data on screen, printer or in any file.
scanf()and printf()functions
// Demo of printf,scanf
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
printf("Enter a value");
scanf("%d",&i);
printf( "nYou entered: %d",i);
getch();
}
getchar() & putchar() functions
// Demo of getchar, putchar
#include <stdio.h>
#include <conio.h>
void main( )
{
int c;
printf("Enter a character");
C PROGRAMMING - BASICS
2. MCA DEPARTMENT | C Basics
C
2
c=getchar();
putchar(c);
getch();
}
gets() & puts() functions
// Demo of gets(), puts()
#include<stdio.h>
#include<conio.h>
void main()
{
char str[100];
printf("Enter a string");
gets( str );
puts( str );
getch();
}
Somebasicsyntaxrule forC program
C is a case sensitive language so all C
instructions must be written in lower case
letter.
All C statement must be end with a
semicolon.
Whitespace is used in C to describe
blanks and tabs.
Whitespace is required between
keywords and identifiers
Data Types
A data type is
o A set of values AND
o A set of operations on those values
A data type is used to
o Identify the type of a variable when
the variable is declared
o Identify the type of the return value of
a function
o Identify the type of a parameter
expected by a function
The type of a variable determines how
much space it occupies in storage and how
the bit pattern stored is interpreted.
All data types are transformed into a
uniform representation
They are stored in a computer and
transformed back to their original
form when retrieved.
This universal representation is called a
bit.
Binary Digit
• pattern or a sequence of 0s and
1s.
Objective of Data Type:
• Is to process data
• Data is classified into specific
types
Numerical
Alphabetical
Audio
Video
Two Classifications of Data Types
• Built-in data types
(1) Fundamental data types
void – used to denote the type with no
values
int – used to denote an integer type
char – used to denote a character type
float, double – used to denote a floating
point type
int *, float *, char * – used to denote a
pointer type, which is a
memory address type
(2) Derived data types
Array – a finite sequence (or table) of
variables of the same data type
String – an array of character variables
Structure – a collection of related
variables of the same and/or different data
types. The structure is called a record and
the variables in the record are called
members or fields
• Programmer-defined data types
– Structure
– Union
– Enumeration
(!) Fundamental data types
int - data type
int is used to define integer numbers.
{
int Count;
Count = 5;
}
float - data type
float is used to define floating point
numbers.
{
float Miles;
Miles = 5.6;
}
double - data type
double is used to define BIG floating point
numbers. It reserves twice the storage for
the number. On PCs this is likely to be 8
bytes.
{
double Atoms;
Atoms = 2500000;
}
char - data type
char defines characters.
{
char Letter;
Letter = 'x';
}
3. MCA DEPARTMENT | C Basics
C
3
Modifiers
The data types explained above have the
following modifiers.
short
long
signed
unsigned
The modifiers define the amount of storage
allocated to the variable. The amount of
storage allocated is not cast in stone. ANSI
has the following rules:
short int <= int <= long int
float <= double <= long double
S.No C Data types
storage
Size
Range
1 char 1 –127 to 127
2 int 2 –32,767 to 32,767
3 float 4
1E–37 to 1E+37 withsix
digits of precision
4 double 8
1E–37 to 1E+37 withten
digits of precision
5 long double 10
1E–37 to 1E+37 withten
digits of precision
6 long int 4
–2,147,483,647 to
2,147,483,647
7 short int 2 –32,767 to 32,767
8
unsigned
short int
2 0 to 65,535
9
signed
short int
2 –32,767 to 32,767
10
long long
int
8
–(2power(63) –1)to
2(power)63 –1
11
signed long
int
4
–2,147,483,647 to
2,147,483,647
12
unsigned
long int
4 0 to 4,294,967,295
13
unsigned
long long
int
8 2(power)64 –1
Example to check size of memory taken
by various datatypes.
#include<stdio.h>
int main()
{
printf("sizeof(char) == %dn",
sizeof(char));
printf("sizeof(short) == %dn",
sizeof(short));
printf("sizeof(int) == %dn",
sizeof(int));
printf("sizeof(long) == %dn",
sizeof(long));
printf("sizeof(float) == %dn",
sizeof(float));
printf("sizeof(double) == %dn",
sizeof(double));
printf("sizeof(long double) ==
%dn", sizeof(long double));
printf("sizeof(long long) ==
%dn", sizeof(long long));
return 0;
}
Declarations
The C language is a statically typed
language.
Ex:
#include <stdio.h>
int main() {
double a = 123.456;
double b = 50.2;
double c = 100.0;
double d[]= {a, b, c};
printf("a=%.3f,b=%.3f,c=%.3f,
d=[%.3f, %.3f, %.3f]n",
a, b, c, d[0], d[1], d[2]);
return 0;
}
Output:
a=123.456, b=50.200, c=100.000,
d=[123.456, 50.200, 100.000]