O slideshow foi denunciado.
Seu SlideShare está sendo baixado. ×

Complete c programming presentation

Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
KALINGAINSTITUTEOFINDUSTRIALTECHNOLOGY
BHUBANESWAR ODISHA 751024
DEPARTMENT OF ELECTRONICS AND TELECOMMUNICATION
Industria...
Overview
 C is a general-purpose, high-levellanguage that was originally developed
by Dennis M. Ritchie to develop the UN...
compiling a C program
int main()
This is the main function from where execution of any C
program begins.
{ This indicates ...
Anúncio
Anúncio
Anúncio
Anúncio
Próximos SlideShares
C programming  session3
C programming session3
Carregando em…3
×

Confira estes a seguir

1 de 23 Anúncio

Complete c programming presentation

Baixar para ler offline

This material is developed in such beautiful manner to the beginners of C language can understand it accurately.
Every concept In material is explained in well disciplined.
Even it will be helpful to the professors for presenting lecture in class room

This material is developed in such beautiful manner to the beginners of C language can understand it accurately.
Every concept In material is explained in well disciplined.
Even it will be helpful to the professors for presenting lecture in class room

Anúncio
Anúncio

Mais Conteúdo rRelacionado

Diapositivos para si (18)

Semelhante a Complete c programming presentation (20)

Anúncio

Mais recentes (20)

Complete c programming presentation

  1. 1. KALINGAINSTITUTEOFINDUSTRIALTECHNOLOGY BHUBANESWAR ODISHA 751024 DEPARTMENT OF ELECTRONICS AND TELECOMMUNICATION Industrial Training Report On C Programming. Submitted To Submitted By Md Nadim Akber Roll No: 1404294 Branch: ETC
  2. 2. Overview  C is a general-purpose, high-levellanguage that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell Labs. C was originally firstimplemented on the DEC PDP-11 computer in 1972. In 1978 Brian Kernighan and Dennis Ritchie produced the firstpublicly available description of C, now known as the K&R standard. History of C Language Programming 1. C is a programming language which born at “AT & T’s Bell Laboratory” of USA in 1972. 2. C was written by Dennis Ritchie, that’s why he is also called as father of c programming language. 3. C language was created for a specific purpose i.e designing the UNIX operating system (which is currently base of many UNIX based OS). 4. From the beginning, C was intended to be useful to allow busy programmers to get things done because C is such a powerful, dominant and supple language 5. Its use quickly spread beyond Bell Labs in the late 70’s because of its long list of strong features. Basics In C – Programming Below are few commands and syntax used in C programming to write a simple C program. Let’s see all the sections of a simple C program line by line. C Basic commands Explanation #include <stdio.h> This is a pre-processor command that includes standard input output header file(stdio.h) from the C library before
  3. 3. compiling a C program int main() This is the main function from where execution of any C program begins. { This indicates the beginning of the main function. /*_some_comments_*/ whatever is given inside the command “/* */” in any C program, won’t be considered for compilation and execution. printf(“Hello_World!“); printf command prints the output onto the screen. getch(); This command waits for any character input from keyboard. return 0; This command terminates C program (main function) and returns 0. } This indicates the end of the main function Printf() and scanf() in C : The printf() and scanf() functions are used for input and output in C language. Both functions are inbuilt library functions, defined in stdio.h (header file). printf() function  The printf() function is used for output. It prints the given statement to the console.  The syntax of printf() function is given below: printf("format string",argument_list);  The format string can be %d (integer), %c (character), %s (string), %f (float) etc. scanf() function  The scanf() function is used for input. It reads the input data from the console.  The syntax of scanf() function is given below:  scanf("format string",argument_list);
  4. 4. Variables in C: A variable is a name of memory location. It is used to store data. Its value can be changed and it can be reused many times. It is a way to represent memory location through symbol so that it can be easily identified. The example of declaring variable is given below: int a; float b; char c; Here, a, b, c are variables and int,float,char are data types. We can also provide values while declaring the variables as given below: int a=10,b=20;//declaring 2 variable of integer type float f=20.8; char c='A'; Rules for defining variables o A variable can have alphabets, digits and underscore. o A variable name can start with alphabet and underscore only. It can't start with digit. o No white space is allowed within variable name. o A variable name must not be any reserved word or keyword e.g. int, float etc. Types of Variables in C There are many types of variables in c: 1. local variable 2. global variable 3. static variable 4. automatic variable 5. external variable Local Variable A variable that is declared inside the function or block is called local variable.
  5. 5. It must be declared at the start of the block. You must have to initialize the local variable before it is used. Global Variable A variable that is declared outside the function or block is called global variable. Any function can change the value of the global variable. Itis available to all the functions. It must be declared at the start of the block. Static Variable A variable that is declared with static keyword is called static variable. It retains its value between multiple function calls. If you call this function many times, local variable will print the same value for each function call e.g, 11,11,11 and so on. But static variable will print the incremented value in each function call e.g. 11, 12, 13 and so on. Automatic Variable All variables in C that is declared inside the block, are automatic variables by default. By we can explicitly declare automatic variable using auto keyword. External Variable We can share a variable in multiple C source files by using external variable. To declare a external variable, you need to use extern keyword.
  6. 6. Data Types in C: A data type specifies the type of data that a variable can store such as integer, floating, character etc. There are 4 types of data types in C language. Types Data Types Basic Data Type int, char, float, double Derived Data Type array, pointer, structure, union Enumeration Data Type enum Void Data Type void Basic Data Types The basic data types are integer-based and floating-point based. C language supports bothsigned and unsigned literals. The memory size of basic data types may change according to 32 or 64 bit operating system. Let's see the basic data types. Its size is given according to 32 bit architecture.
  7. 7. Data Types Memory Size Range char 1 byte −128 to 127 signed char 1 byte −128 to 127 unsigned char 1 byte 0 to 255 short 2 byte −32,768 to 32,767 signed short 2 byte −32,768 to 32,767 unsigned short 2 byte 0 to 65,535 int 2 byte −32,768 to 32,767 signed int 2 byte −32,768 to 32,767 unsigned int 2 byte 0 to 65,535 short int 2 byte −32,768 to 32,767 signed short int 2 byte −32,768 to 32,767 unsigned short int 2 byte 0 to 65,535 long int 4 byte -2,147,483,648 to 2,147,483,647 signed long int 4 byte -2,147,483,648 to 2,147,483,647 unsigned long int 4 byte 0 to 4,294,967,295 float 4 byte
  8. 8. double 8 byte long double 10 byte Keywords in C: A keyword is a reserved word. You cannot use it as a variable name, constant name etc. There are only 32 reserved words (keywords) in C language. C Operators: An operator is simply a symbol that is used to perform operations. There can be many types of operations like arithmetic, logical, bitwise etc. There are following types of operators to perform different types of operations in C language. o Arithmetic Operators o Relational Operators o Shift Operators o Logical Operators o Bitwise Operators o Ternary or Conditional Operators o Assignment Operator o Misc Operator C Control Statements Control statements is given below: C if else Statement The if statement in C language is used to perform operation on the basis of condition. By using if-else statement, you can perform operation either condition is true or false. There are many ways to use if statement in C language:
  9. 9. o If statement o If-else statement o If else-if ladder o Nested if If Statement The single if statement in C language is used to execute the code if condition is true. The syntax of if statement is given below: 1. if(expression){ 2. //code to be executed 3. } If-else Statement The if-else statement in C language is used to execute the code if condition is true or false. The syntax of if-else statement is given below: 1. if(expression){ 2. //code to be executed if condition is true 3. }else{ 4. //code to be executed if condition is false 5. } If else-if ladder Statement The if else-if statement is used to execute one code from multiple conditions. The syntax of if else-if statement is given below: 1. if(condition1){ 2. //code to be executed if condition1 is true 3. }else if(condition2){ 4. //code to be executed if condition2 is true 5. } 6. else if(condition3){ 7. //code to be executed if condition3 is true 8. } 9. ... 10. else{ 11. //code to be executed if all the conditions are false 12. }
  10. 10. C Switch Statement The switch statement in C language is used to execute the code from multiple conditions. It is like if else-if ladder statement. C Loops The loops in C language are used to execute a block of code or a part of the program several times. In other words, it iterates a code or group of code many times. Advantage of loops in C 1) It saves code. 2) It helps to traverse the elements of array Types of C Loops There are three types of loops in C language that is given below: 1. do while 2. while 3. for do-while loop in C It iterates the code until condition is false. Here, condition is given after the code. So at least once, code is executed whether condition is true or false. while loop in C Like do while, it iterates the code until condition is false. Here, condition is given before the code. So code may be executed 0 or more times. It is better if number of iteration is not known by the user. The syntax of while loop in c language is given below: 1. while(condition){
  11. 11. 2. //code to be executed 3. } for loop in C Like while, it iterates the code until condition is false. Here, initialization, condition and increment/decrement is given before the code. So code may be executed 0 or more times. It is good if number of iteration is known by the user. The syntax of for loop in c language is given below: 1. for(initialization;condition;incr/decr){ 2. //code to be executed 3. } do while loop in C To execute a part of program or code several times, we can use do-while loop of C language. The code given between the do and while block will be executed until condition is true. In do while loop, statement is given before the condition, so statement or code will be executed at lease one time. In other words, we can say it is executed 1 or more times. It is better if you have to execute the code at least once. do while loop syntax The syntax of C language do-while loop is given below: 1. do{ 2. //code to be executed 3. }while(condition);
  12. 12. C Functions: The function in C language is also known as procedure or subroutine in other programming languages. To perform any task, we can create function. A function can be called many times. It provides modularity and code reusability. Types of Functions There are two types of functions in C programming: 1. Library Functions: are the functions which are declared in the C header files such as scanf(), printf(), gets(), puts(), ceil(), floor() etc. 2. User-defined functions: are the functions which are created by the C programmer, so that he/she can use it many times. It reduces complexity of a big program and optimizes the code. Call by value and call by reference in C There are two ways to pass value or data to function in C language: call by value and call by reference. Original value is not modified in call by value but it is modified in call by reference. Call by value in C In call by value, original value is not modified. In call by value, value being passed to the function is locally stored by the function parameter in stack memory location. If you change the value of function parameter, it is changed for the current function only. It will not change the value of variable inside the caller method such as main(). Call by reference in C In call by reference, original value is modified because we pass reference (address). Here, address of the value is passed in the function, so actual and formal arguments shares the same address space. Hence, value changed inside the function, is reflected inside as well as outside the function. Recursion in C
  13. 13. When function is called within the same function, it is known as recursion in C. The function which calls the same function, is known as recursive function. A function that calls itself, and doesn't perform any task after function call, is know as tail recursion. In tail recursion, we generally call the same function with return statement. Storage Classes in C Storage classes are used to define scope and life time of a variable. There are four storage classes in C programming. o auto o extern o static o register C Array: Array in C language is a collection or group of elements (data). All the elements of c array are homogeneous (similar). It has contiguous memory location. Advantage of C Array 1) Code Optimization: Less code to the access the data. 2) Easy to traverse data: By using the for loop, we can retrieve the elements of an array easily. 3) Easy to sort data: To sort the elements of array, we need a few lines of code only. 4) Random Access: We can access any element randomly using the array. Declaration of C Array We can declare an array in the c language in the following way. data_type array_name[array_size]; Two Dimensional Array in C
  14. 14. The two dimensional array in C language is represented in the form of rows and columns, also known as matrix. It is also known as array of arrays or list of arrays. The two dimensional, three dimensional or other dimensional arrays are also known as multidimensional arrays. Declaration of two dimensional Array in C We can declare an array in the c language in the following way. 1. data_type array_name[size1][size2]; Passing Array to Function in C To reuse the array operation, we can create functions that receives array as argument. To pass array in function, we need to write the array name only in the function call. C Pointers: The pointer in C language is a variable, it is also known as locator or indicator that points to an address of a value. Declaring a pointer The pointer in c language can be declared using * (asterisk symbol). int *a;//pointer to int char *c;//pointer to char NULL Pointer A pointer that is not assigned any value but NULL is known as NULL pointer. If you don't have any address to be specified in the pointer at the time of declaration, you can assign NULL value. It will a better approach. int *p=NULL; In most the libraries, the value of pointer is 0 (zero).
  15. 15. C Pointer to Pointer In C pointer to pointer concept, a pointer refers to the address of another pointer. In c language, a pointer can point to the address of another pointer which points to the address of a value. Let's understand it by the diagram given below: Let's see the syntax of pointer to pointer. int **p2; Dynamic memory allocation in C The concept of dynamic memory allocation in c language enables the C programmer to allocate memory at runtime. Dynamic memory allocation in c language is possible by 4 functions of stdlib.h header file. 1. malloc() 2. calloc() 3. realloc() 4. free() Before learning above functions, let's understand the difference between static memory allocation and dynamic memory allocation. static memory allocation dynamic memory allocation memory is allocated at compile time. memory is allocated at run time. memory can't be increased while executing program. memory can be increased while exec used in array. used in linked list. Now let's have a quick look at the methods used for dynamic memory allocation.
  16. 16. malloc() allocates single block of requested memory. calloc() allocates multiple block of requested memory. realloc() reallocates the memory occupied by malloc() or calloc() functions. free() frees the dynamically allocated memory. malloc() function in C The malloc() function allocates single block of requested memory. It doesn't initialize memory at execution time, so it has garbage value initially. It returns NULL if memory is not sufficient. The syntax of malloc() function is given below: ptr=(cast-type*)malloc(byte-size) calloc() function in C The calloc() function allocates multiple block of requested memory. It initially initialize all bytes to zero. It returns NULL if memory is not sufficient. The syntax of calloc() function is given below: ptr=(cast-type*)calloc(number, byte-size) realloc() function in C If memory is not sufficient for malloc() or calloc(), you can reallocate the memory by realloc() function. In short, it changes the memory size. Let's see the syntax of realloc() function. 1. ptr=realloc(ptr, new-size)
  17. 17. free() function in C The memory occupied by malloc() or calloc() functions must be released by calling free() function. Otherwise, it will consume memory until program exit. Let's see the syntax of free() function. free(ptr) C Strings: String in C language is an array of characters that is terminated by 0 (null character). There are two ways to declare string in c language. 1. By char array 2. By string literal C gets() and puts() functions The gets() function reads string from user and puts() function prints the string. Both functions are defined in <stdio.h> header file. C Structure Union: Structure in c language is a user defined datatype that allows you to hold different type of elements. Each element of a structure is called a member. It works like a template in C++ and class in Java. You can have different type of elements in it. Defining structure The struct keyword is used to define structure. Let's see the syntax to define structure in c. 1. struct structure_name 2. { 3. data_type member1; 4. data_type member2; 5. data_type memeberN; 6. };
  18. 18. Array of Structures in C There can be array of structures in C programming to store many information of different data types. The array of structures is also known as collection of structures. Nested Structure in C Nested structure in c language can have another structure as a member. There are two ways to define nested structure in c language: 1. By separate structure 2. By Embedded structure 1) Separate structure We can create 2 structures, but dependent structure should be used inside the main structure as a member. Let's see the code of nested structure. 2) Embedded structure We can define structure within the structure also. It requires less code than previous way. But it can't be used in many structures. C Union Like structure, Union in c language is a user defined datatype that is used to hold different type of elements. But it doesn't occupy sum of all members size. It occupies the memory of largest member only. It shares memory of largest member.
  19. 19. Advantage of union over structure It occupies less memory because it occupies the memory of largest member only. Disadvantage of union over structure It can store data in one member only. Defining union The union keyword is used to define union. Let's see the syntax to define union in c. 1. union union_name 2. { 3. data_type member1; 4. data_type member2; 5. . 6. . 7. data_type memeberN; 8. }; File Handling in C: File Handling in c language is used to open, read, write, search or close file. It is used for permanent storage. Advantage of File It will contain the data even after program exit. Normally we use variable or array to store data, but data is lost after program exit. Variables and arrays are non-permanent storage medium whereas file is permanent storage medium. Functions for file handling There are many functions in C library to open, read, write, search and close file. A list of file functions are given below:
  20. 20. No. Function Description 1 fopen() opens new or existing file 2 fprintf() write data into file 3 fscanf() reads data from file 4 fputc() writes a character into file 5 fgetc() reads a character from file 6 fclose() closes the file 7 fseek() sets the file pointer to given position 8 fputw() writes an integer to file 9 fgetw() reads an integer from file 10 ftell() returns current position 11 rewind() sets the file pointer to the beginning of the file THANK YOU

×