Beruflich Dokumente
Kultur Dokumente
PROGRAMMING LANGUAGE
DEPARTMENT OF TCP
C LANGUAGE - BASICS
SOFTWARE
Application S/W Ex: MS PACKAGE etc System S/W Ex: Complier,OS etc
Fundamentals
C was developed by Dennis Ritchie. C is a modular programming language. C is a Middle level language which combines the efficiency of machine language and ease of programming of high level language.
DEPARTMENT OF TCP
C LANGUAGE - BASICS
Some of the strengths of C are that its general purpose its a small language ( i.e. limited keywords ) its portable ( common functions are machine-independent; they are provided in library files) it has a variety of data types e.g. integer, character, float etc.
DEPARTMENT OF TCP C LANGUAGE - BASICS 4
C CHARACTER SET
The only characters required by the C Programming Language are as follows: A - Z a -z 0 - 9
DEPARTMENT OF TCP
C LANGUAGE - BASICS
}
f1() { local variables to function 1 ; statements associated with function 1 ; } etc
DEPARTMENT OF TCP C LANGUAGE - BASICS 6
KEYWORDS Keywords are the words whose meaning has already be defined (reserved words)
auto break case char continue default do double else extern float for
goto if int long register return short sizeof static struct switch typedef union unsigned void while VARIABLES In C, a quantity which may vary during execution is called a variable. Variable names are names given to the locations in memory of a computer where different constants are stored.
DEPARTMENT OF TCP
C LANGUAGE - BASICS
underscores.
C LANGUAGE - BASICS
Constants
A constant is a quantity that doesnt change. 3 types of constants are string constants, numeric constants, character constants.
String constants is a sequence of alphanumeric characters enclosed in double quotation marks (eg: sri sairam)
Numeric constants are positive / negative numbers. There are four types of numeric constants (integer, floating point, hexa, octal) Character constants one character enclosed within single quotes denotes a character constant. (eg: A, a, DEPARTMENT OF TCP C LANGUAGE - BASICS 9 :)
DATA TYPES:
Primary Datatypes
int - integer: a whole number. (2 bytes) float - floating point value: ie a number with a fractional part. ( 4 bytes) double - a double-precision floating point value. (8 bytes) char - a single character. (1 byte) void - valueless special purpose type which we will examine closely in later sections (0 bytes)
Modifiers
short, long, signed , unsigned
DEPARTMENT OF TCP LANGUAGE - of BASICS 10 The modifiers define theCamount storage allocated to the
short int <= int <= long int float <= double <= long double Qualifiers The const keyword is used to create a read only variable. Once initialised, the value of the variable cannot be changed but can be used just like any other variable. const syntax main() { const float pi = 3.14; }
The const keyword is used as a qualifier to the following data types - int float char double struct. const int degrees = 360; const float pi const char quit
DEPARTMENT OF TCP
= 3.14; = 'q';
C LANGUAGE - BASICS 11
Enum
DEPARTMENT OF TCP
C LANGUAGE - BASICS
12
OPERATORS Depending on the function performed the operators are classifies as 1. Arithmetic ( + - * / %) 2. 3. 4. Increment Decrement( ++ -- ) Relational ( < Logical ( && > || <= !) >= == !=)
5. Bitwise ( ~
6.
>>
<< & |
: (ternary) )
^ )
Conditional ( ?
7. Assignment 8. sizeof
C LANGUAGE - BASICS 13
DEPARTMENT OF TCP
/* O/P 2 1
2 2 */
*/
i=1;j=1;
puts("\n\tDemo 2"); printf("\t%d \n",i=j++); /* O/P printf("\t%d \n",i=++j); /* O/P
DEPARTMENT OF TCP C LANGUAGE - BASICS
1 3
*/ */
14
DEPARTMENT OF TCP
C LANGUAGE - BASICS
15
Bitwise operators (bit manipulation) operates on ints, chars Shift operator (<< , >>) shifts each bit in the operand to left / right
main()
{ unsigned int Value=4; unsigned int Shift=2; Value = Value << Shift; Value <<= Shift; printf("%d\n", Value);
DEPARTMENT OF TCP
C LANGUAGE - BASICS
C LANGUAGE - BASICS
17
OR ( | ) will copy a bit if it exists in either / both operand. main() { unsigned int a = 60; /* 60 = 0011 1100 */ unsigned int b = 13; unsigned int c = 0; c = a | b; /* 61 = 0011 1101 */ } /* 13 = 0000 1101 */
DEPARTMENT OF TCP
C LANGUAGE - BASICS
18
XOR ( ^ ) copies the bit if it is set (bit=1) in one operand (but not both).
main()
{ unsigned int a = 60; */ */ /* 60 = 0011 1100
unsigned int c = 0;
DEPARTMENT OF TCP
c = a ^ b;
C LANGUAGE - BASICS
/* 49 = 0011
19
Conditional Operator
Expressions1 ? Expression2 : expressin3 Eg:
int x,y;
scanf(%d,&x);
y = ( x > 5 ? 3 : 4);
int i; scanf(%d,&i); (i==1 ? printf(Hello) : printf(Bye));
DEPARTMENT OF TCP C LANGUAGE - BASICS 20
Condition Clause
C evaluates the condition clause as a true or false statement although strictly C doesnt have a data representation for this type. Alternatively C evaluates a Boolean condition clause as an integer value i.e.
0
any
DEPARTMENT OF TCP
sizeof operator
The following code shows sizeof returning the length of a data type.
/* How big is an int? expect an answer of 4. */ main() {
DEPARTMENT OF TCP
}
22
Display
single character signed integer exponential format signed decimal use %f or %e as required
%o %p
int pointer
%s
%u %x (%X)
array of char
int int
sequence of characters
unsigned decimal unsigned hex value C LANGUAGE - BASICS 23
DEPARTMENT OF TCP
Each specifier can be preceded by a modifier which determines how the value will be printed. The most general modifier is of the form: flag width.precision
The flag can be any of:
flag + meaning left justify always display sign
space display space if there is no sign 0 # pad with leading zeros use alternate form of specifier
The width specifies the number of characters used in total to display the value and precision indicates the number of characters used after the decimal point. Eg: %10.3f , %+5d
DEPARTMENT OF TCP C LANGUAGE - BASICS 24
#modifier
%#o
%#x %#f or%#e %#g
Control Codes
\b \f \n \r \t backspace formfeed new line carriage return horizontal tab
\'
\0
single quote
null
DEPARTMENT OF TCP
C LANGUAGE - BASICS
25
CONTROL STATEMENTS 1. Sequence control statements 2. Selection or Decision Control statements (if , switch) 3. Repitition or Loop control statements (while, do while, for) Eg programs Using break and continue statements within loops
goto statement
DEPARTMENT OF TCP
C LANGUAGE - BASICS
26
UNFORMATTED CONSOLE I/O FUNCTIONS getchar() getch() getche() putchar() gets() puts()
ARRAYS
Array is a collection of identical data objects which are stored in consecutive memory locations in a common heading or a variable name. Each storage location in an array is called an array element.
Declaring an Array Since array is also an identifier, it should be declared. Syntax: Array-type Array-name [size1][size2]..[size n]; Example : int marks[3]; int matrixA[2][2];
DEPARTMENT OF TCP C LANGUAGE - BASICS 28
Initializing arrays Arrays can be initialized in many ways. int a[]={10,20,30,40}; int a [4]; a[0]=10; a[1]=20;a[2]=30;a[3]=40; Int a[4]={1,2,3};
A String constant is a one dimensional array of characters terminated by a null (\0)
static char name[]={S,A,I,R,A,M,\0};
SAIRAM
DEPARTMENT OF TCP
C LANGUAGE - BASICS
29
2 dimensional array
Initializing 2 dimensional array static int stud[4][2]={123,234,345,234,23,23,456,4565}; Or { {123,234},{345,234},{23,23},{456,4565}}; Multidimensional arrays
DEPARTMENT OF TCP
C LANGUAGE - BASICS
30
FUNCTIONS
Function is a self-contained block of program that performs a coherent task of some kind. Gn. Format of the function definition function type functionname (arg1, arg2,,argn) datatype arg1, arg2;
Internal or local variables are known as the variables which are declared inside a function. Memory space is automatically allocated as the function is entered and released when exit.
Default initial value is unpredictable. Scope is local to the block defined. Life within the block
DEPARTMENT OF TCP
C LANGUAGE - BASICS
32
Main()
{
auto int I=1; {
Output?
{ auto int I=3; printf(%d\n,I); } printf(%d\n,I); }
printf(%d\n,I); }
DEPARTMENT OF TCP C LANGUAGE - BASICS 33
Register storage class Storage CPU registers Default value garbage Scope Local Life within the block Main()
{ register int I;
For (I=1; I <=10; I++) printf(%d,I); }
DEPARTMENT OF TCP C LANGUAGE - BASICS 34
{
Increment(); Increment(); Increment(); }
DEPARTMENT OF TCP C LANGUAGE - BASICS 35
}
Output?
DEPARTMENT OF TCP
C LANGUAGE - BASICS
36
Scope Global
Life As long as the program execution come to an end. Eg: Int I; Main() { Printf(I=,I);
DEPARTMENT OF TCP C LANGUAGE - BASICS 37
Increment();
Increment(); Decrement();
Decrement();
} Increment() { I = I+1; printf (on incrementing I =%d\n,I);} Decrement() { I = I 1; printf (on decrementing I =%d\n,I);} Output?
DEPARTMENT OF TCP
C LANGUAGE - BASICS
38
typedef is used to define new data type names to make a program more readable to the programmer. For example:
main()
{ }
{
}
DEPARTMENT OF TCP
C LANGUAGE - BASICS
39
STRUCTURE A Structure gathers together, different atoms of information that comprise a given entity. Need? Structures can be declared in various forms... struct x {int a; int b; int c;}; /* declaration */ struct {int a; int b; int c;} z;struct x z; All the examples above are structure declarations, The first gives x as a 'structure tag' - this is optional. The first and second declare the members of the structure. Second and third give z this is the variable that assumes the structure type.
DEPARTMENT OF TCP C LANGUAGE - BASICS 40
Eg: Struct student { char name[20]; int age; float height}; Static struct student class[2]={ {sai,18,5.4}, {ram,19,5.5}}; Accessing structure members Structure within structure
DEPARTMENT OF TCP
C LANGUAGE - BASICS
41
UNION
Union stores values of different types in a single location. A union may contain one of many different types of values. The union only holds a value for one data type.
Eg:
union value { Int c; Double d; }; union value x; INT C DOUBLE D
x.c=10;
x.d=-34545.45;
DEPARTMENT OF TCP C LANGUAGE - BASICS 42
ENUMERATION
Enum userdefined name{ Member 1; member2; .}; /* enum datatype declaration */ Enum days {}; Enum days d1, d2, d3;
DEPARTMENT OF TCP
C LANGUAGE - BASICS
43
POINTER
A pointer is a variable suitable for keeping memory addresses of other variables, the values you assign to a pointer are memory addresses of other variables (or other pointers).
C pointers are characterized by their value and datatype. The value is the address of the memory location the pointer points to, the type determines how the pointer will be incremented/decremented in pointer int *pi = &i; int * is the notation for a pointer to an int. & is the DEPARTMENT OF TCP C LANGUAGE - BASICS 44 operator which returns the address of its argument
The Preprocessor The C preprocessor permits you to define simple macros that are evaluated and expanded prior to compilation. Commands begin with a #. Abbreviated list: #define : defines a macro #undef : removes a macro definition #include : insert text from file
DEPARTMENT OF TCP
C LANGUAGE - BASICS
45
#if : conditional based on value of expression #ifdef : conditional based on whether macro defined #ifndef : conditional based on whether macro is not defined #else : alternative #elif : conditional alternative defined() : preprocessor function: 1 if name defined, else 0
DEPARTMENT OF TCP C LANGUAGE - BASICS 46
Q&A
1.What is the difference between malloc and calloc? Answer:
Calloc initializes the allocated segment to nulls while malloc does not do so. 2. Datatypes can be classified as Primary and Secondary. What are secondary data types ? Give two examples for both. Answer: Secondary data types are based on primary data types .Primary Data types: int, short int, long int, float, double,char. Secondary Data types: structures, arrays, union.
DEPARTMENT OF TCP C LANGUAGE - BASICS 47