C Fundamentals & Pointers Overview of C Structure Of “C” Programs Flow of C Program TOKENS Keywords
History of C C is developed by Dennis Ritchie at AT&T Bell Laboratories, USA C is a structured programming language Developed to overcome the problems of previous language such as BCPL,B, Pascal etc., Initially developed to use in UNIX OS Comments in C provides easy readability C is a powerful language Friday, September 07, 2018Friday, September 07, 2018Friday, September 07, 2018
Basic Structure Of “C” Programs #include<stdio.h> #include<conio.h> void main() { -- other statements } Friday, September 07, 2018Friday, September 07, 2018Friday, September 07, 2018
Header files The files that are specified in the include section is called as header file These are precompiled files that has some functions defined in them We can call those functions in our program by supplying parameters Header file is given an extension .h C Source file is given an extension .c Friday, September 07, 2018Friday, September 07, 2018Friday, September 07, 2018
Main function This is the entry point of a program When a file is executed, the start point is the main function From main function the flow goes as per the programmers choice. There may or may not be other functions written by user in a program Main function is compulsory for any c program Friday, September 07, 2018Friday, September 07, 2018Friday, September 07, 2018
Writing the first program #include<stdio.h> int main() { printf(“Hello”); return 0; } This program prints Hello on the screen when we execute it Friday, September 07, 2018Friday, September 07, 2018Friday, September 07, 2018
Running a C Program Type a program Save it Compile the program – This will generate an exe file (executable) Run the program (Actually the exe created out of compilation will run and not the .c file) In different compiler we have different option for compiling and running. We give only the concepts. Friday, September 07, 2018Friday, September 07, 2018Friday, September 07, 2018
Comments in C Single line comment Multi line comment // (double slash) Termination of comment is by pressing enter key Multi line comment /*…. …….*/ This can span over to multiple lines Friday, September 07, 2018Friday, September 07, 2018Friday, September 07, 2018
“C” language TOKENS The smallest individual units in a C program are known as tokens A token is source-program text that the compiler does not break down into component elements C has 6 different types of tokens viz. Keywords [e.g. float, int, while] Identifiers [e.g. main, amount] Constants [e.g. -25.6, 100] Strings [e.g. “SMIT”, “year”] Special Symbols [e.g. {, }, [, ] ] Operators [e.g. +, -, *] C - programs are written using these tokens and the general syntax. Keywords Constants Operators Strings Identifiers Special Symbols C tokens
Keywords in ANSI “C” switch typedef union unsigned void volatile while long register return short signed sizeof static struct int double else enum etern float for goto if auto break case char const continue default do
Constants Constants in C are the fixed values that do not change during the execution of a program. CONSTANTS Numeric constants Character constants Integer Constants Real Constants Single Character Constants String Constants
Data types in C Used for declaring variables or functions of different types Type of a variable determines how much space it occupied in storage Datatypes is classified into Basic Types or Primitive data types int, float, double, char Enumerated Types Define and assign certain discrete int value Type Void – No value Derived Types or Aggregate data types Pointer Types Array types Structure types Union types Function types Friday, September 07, 2018Friday, September 07, 2018Friday, September 07, 2018
Data Types- different attributes Size Representation Minimum range Maximum range char, signed char 8 bits ASCII -128 127 unsigned char bool 8 bits 255 short, signed short 16 bits 2's complement -32768 32767 unsigned short Binary 65535 int, signed int unsigned int long, signed long 32 bits -2,147,483,648 2,147,483,647 unsigned long 4,294,967,295 float IEEE 32-bit 1.175495e-38 3.4028235e+38 double long double
Variables Variables are data that will keep on changing Declaration <<Data type>> <<variable name>>; int a; Definition <<varname>>=<<value>>; a=10; Usage <<varname>> a=a+1; //increments the value of a by 1 We can also declare and define a variable in single shot like this. int a=10; Friday, September 07, 2018Friday, September 07, 2018Friday, September 07, 2018
Variable names- Rules Should not be a reserved word like int etc. Should start with a letter or an underscore(_) Can contain letters, numbers or underscore No other special characters are allowed including space Variable names are case sensitive A and a are different Friday, September 07, 2018Friday, September 07, 2018Friday, September 07, 2018
Input and Output Input Output scanf(“%d”,&a); Gets an integer value from the user and stores it under the name “a” Output printf(“%d”,a); Prints the value present in variable a on the screen Format specifiers %d is the format specifier. This informs to the compiler that the incoming value is an integer value. Other data types can be specified as follows: %c – character %f – float %lf – double %s – character array (string) Printf and scanf are defined under the header file stdio.h Friday, September 07, 2018Friday, September 07, 2018Friday, September 07, 2018
For loops The syntax of for loop is for(initialisation;condition checking;increment) { set of statements } Eg: Program to print Hello 10 times for(I=0;I<10;I++) printf(“Hello”); Friday, September 07, 2018Friday, September 07, 2018Friday, September 07, 2018
While loop The syntax for while loop while(condn) { statements; } Eg: while(a != 0) Output: 10987654321 printf(“%d”,a); a--; Friday, September 07, 2018Friday, September 07, 2018Friday, September 07, 2018
Do While loop The syntax of do while loop do { set of statements }while(condn); Eg: i=10; Output: do 10987654321 printf(“%d”,i); i--; }while(i!=0) While – Entry controlled loop Do While – Exit controlled loop Friday, September 07, 2018Friday, September 07, 2018Friday, September 07, 2018
Conditional statements if (condition) { stmt 1; //Executes if Condition is true } else stmt 2; //Executes if condition is false Friday, September 07, 2018Friday, September 07, 2018Friday, September 07, 2018
Conditional statement switch(var) { case 1: //if var=1 this case executes stmt; break; case 2: //if var=2 this case executes default: //if var is something else this will execute } Friday, September 07, 2018Friday, September 07, 2018Friday, September 07, 2018
Operators Arithmetic (+,-,*,/,%) Relational (<,>,<=,>=,==,!=) Logical (&&,||,!) Bitwise (&,|) Assignment (=) Compound assignment(+=,*=,-=,/=,%=,&=,|=) Shift (right shift >>, left shift <<) Friday, September 07, 2018Friday, September 07, 2018Friday, September 07, 2018
String functions strlen(str) – To find length of string str strrev(str) – Reverses the string str as rts strcat(str1,str2) – Appends str2 to str1 and returns str1 strcpy(st1,st2) – copies the content of st2 to st1 strcmp(s1,s2) – Compares the two string s1 and s2 strcmpi(s1,s2) – Case insensitive comparison of strings Header file to be included is string.h Friday, September 07, 2018Friday, September 07, 2018Friday, September 07, 2018
Numeric functions pow(n,x) – evaluates n^x ceil(1.3) – Returns 2 floor(1.3) – Returns 1 abs(num) – Returns absolute value log(x) - Logarithmic value sin(x) cos(x) tan(x) Header file to be included math.h Friday, September 07, 2018Friday, September 07, 2018Friday, September 07, 2018
Procedures Procedure is a function whose return type is void Functions will have return types int, char, double, float or even structs and arrays Return type is the data type of the value that is returned to the calling point after the called function execution completes Friday, September 07, 2018Friday, September 07, 2018Friday, September 07, 2018
Functions and Parameters Syntax of function Declaration section <<Returntype>> funname(parameter list); Definition section <<Returntype>> funname(parameter list) { body of the function } Function Call Funname(parameter); Friday, September 07, 2018Friday, September 07, 2018Friday, September 07, 2018
Example function #include<stdio.h> void fun(int a); //declaration int main() { fun(10); //Call } void fun(int x) //definition printf(“%d”,x); Friday, September 07, 2018Friday, September 07, 2018Friday, September 07, 2018
Actual and Formal parameters Actual parameters are those that are used during a function call Formal parameters are those that are used in function definition and function declaration Friday, September 07, 2018Friday, September 07, 2018Friday, September 07, 2018
Pointers / Locator / indicator Pointer is a variable that stores address of another variable Pointer Variable Addresses are integers. Hence pointer stores integer data Size of pointer = size of int Pointer that stores address of integer variable is called as integer pointer and is declared as int *ip; Address Value Friday, September 07, 2018Friday, September 07, 2018Friday, September 07, 2018
Advantages Usage of Pointer Reduces the Code Improves the Performance Return multiple values from function Access any memory location Usage of Pointer Dynamic Memory Allocation malloc(), calloc() Arrays, Functions and Structures
Operators used in Pointers Address Dereferencing & * (Value of) (Address of)
The value ‘3’ is saved in the memory location ‘x100C’ Int i=3; Value of ‘i’ Address of ‘i’ ‘&i’ ‘*i’ variable i 3 (Value of i) X1000 x1004 x1008 x100c x1010 x1014 Address of i The value ‘3’ is saved in the memory location ‘x100C’
Syntax for pointers (pointer type declaration) type *identifier ; Example Char *cp ; Int *ip ; Double *dp ;
Pointer Assignment Int i = 1 , *ip ; //pointer declaration ip = &i ; //pointer assignment *ip = 3 ; //pointer assignment
Examples int a; a=10; //a stores 10 int *ip; ip = &a; //ip stores address of a (say 1000) ip : fetches 1000 *ip : fetches 10 * Is called as dereferencing operator Friday, September 07, 2018Friday, September 07, 2018Friday, September 07, 2018
Pointer Arithmetic Lets take this example program #include<stdio.h> Void main() { Int a [5]={1,2,3,4,5} , b , *pt ; pt = &a[0]; pt = pt + 4 ; b=a[0] ; b+=4 ; } b = 1 b=1+4 b= 5 a[0] a[1] a[2] a[3] a[4] pt X1000 x1004 x1008 x100c x1010 a[0] a[1] a[2] a[3] a[4] b X1000 x1004 x1008 x100c x1010
Predict the output of this code Void main() { int num=10; Int *pnum=NULL; pnum = # *pnum += 20; printf("\nNumber = %d", num); printf("\nPointer Number = %d", *pnum); }
Number = 10 Pointer Number = 30
Eg., int a[10] = {1,2,3,4,5,6,7,8,9,12} ,*p, *q , i; p = &a[2]; q = &a[5]; i = *q - *p; Printf(“The value of i is %d” i ); i = *p - *q; a[2] = a[5] = 0;
The value of i is 3 The value of i is -3 The value of i is 0
Work to your Brain int a[10] = { 2,3,4,5,6,7,8,9,1,0 }, *p, *q; p = &a[2]; q = p + 3; p = q – 1; p+ + ; Printf(“The value of p and q are : %d , %d” *p,*q);
The value of p and q are : 7 , 7
Work to your Brain int main() { int x[2]={1,2},y[2]={3,4}; int small,big; small=&x[0]; big=&y[0]; min_max(&small,&big); printf(“small%d big%d",*small,*big); return 0; } min_max(int *a,int *b) { a++; b++; return (*a,*b); }
Small 2 big 4