C Programming Study Material
C Programming Study Material
C Programming Study Material
C Programming
Unit – I: Introduction to C
History of C Language
History of C language is interesting to know. Here we are going to discuss a brief
history of the c language.
C programming language was developed in 1972 by Dennis Ritchie at bell laboratories of
AT&T (American Telephone & Telegraph), located in the U.S.A.
Dennis Ritchie is known as the founder of the c language.
It was developed to overcome the problems of previous languages such as B, BCPL, etc.
Initially, C language was developed to be used in UNIX operating system. It inherits many
features of previous languages such as B and BCPL.
Features of C Language
C is the widely used language. It provides many features that are given below:
1. Simple
2. Machine Independent or Portable
3. Mid-level programming language
4. structured programming language
5. Rich Library
6. Memory Management
7. Fast Speed
8. Pointers
9. Recursion
10. Extensible
To write the first c program, open the C console and write the following code:
#include <stdio.h>
int main(){
printf("Hello Students!!");
return 0;
}
#include <stdio.h> includes the standard input output library functions. The printf() function is
defined in stdio.h .
int main() The main() function is the entry point of every program in c language.
printf() The printf() function is used to print data on the console.
return 0 The return 0 statement, returns execution status to the OS. The 0 value is used for
successful execution and 1 for unsuccessful execution.
Variables in C
A variable is a name of the 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.
int a;
float b;
char c;
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
Unit – III: Operators, keywords, Identifiers, Format Specifiers, Constants & Static
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. | Dynamic Method Dispatch
There are following types of operators to perform different types of operations in C
language.
o Arithmetic Operators
o Relational Operators
o Logical Operators
o Bitwise Operators
o Ternary or Conditional Operators
o Assignment Operator
Unit – IV: C control statements ( if-else, switch case, looping statements, break, continue,
goto, label )
1. #include<stdio.h>
2. int main(){
3. int number=0;
4. printf("Enter a number:");
5. scanf("%d",&number);
6. if(number%2==0){
7. printf("%d is even number",number);
8. }
9. return 0;
10. }
Output
Enter a number:4
4 is even number
enter a number:5
Output
Output
C Switch Statement
The switch statement in C is an alternate to if-else-if ladder statement which allows
us to execute multiple operations for the different possibles values of a single
variable called switch variable. Here, We can define various statements in the
multiple cases for the different values of a single variable.
1. switch(expression){
2. case value1:
3. //code to be executed;
4. break; //optional
5. case value2:
6. //code to be executed;
7. break; //optional
8. ......
9.
10. default:
11. code to be executed if all cases are not matched;
12. }
1. while(condition){
2. //code to be executed
3. }
Let's see the simple program of while loop that prints 1 to 10.
1. #include<stdio.h>
2. int main(){
3. int i=1;
4. while(i<=10){
5. printf("%d \n",i);
6. i++;
7. }
8. return 0;
9. }
Let's see the simple program of for loop that prints 1 to 10.
1. #include<stdio.h>
2. int main(){
3. int i=0;
4. for(i=1;i<=10;i++){
5. printf("%d \n",i);
6. }
7. return 0;
8. }
1. do{
2. //code to be executed
3. }while(condition);
There is given the simple program of c language do while loop where we are printing
the 1 to 10.
1. #include<stdio.h>
2. int main(){
3. int i=1;
4. do{
5. printf("%d \n",i);
6. i++;
7. }while(i<=10);
8. return 0;
9. }
goto example
Let's see a simple example to use goto statement in C language.
1. #include <stdio.h>
2. int main()
3. {
4. int num,i=1;
5. printf("Enter the number whose table you want to print?");
6. scanf("%d",&num);
7. table:
8. printf("%d x %d = %d\n",num,i,num*i);
9. i++;
10. if(i<=10)
11. goto table;
12. }
C Functions
In c, we can divide a large program into the basic building blocks known as function.
The function contains the set of programming statements enclosed by {}. A function
can be called multiple times to provide reusability and modularity to the C program.
In other words, we can say that the collection of functions creates a program. The
function is also known as procedureor subroutinein other programming languages.
Advantage of functions in C
There are the following advantages of C functions.
By using functions, we can avoid rewriting same logic/code again and again in a
program.
We can call C functions any number of times in a program and from any place in a
program.
We can track a large C program easily when it is divided into multiple functions.
Reusability is the main achievement of C functions.
However, Function calling is always a overhead in a C program.
Function Aspects
There are three aspects of a C function.
Function call Function can be called from anywhere in the program. The parameter
list must not differ in function calling and function declaration. We must pass the
same number of functions as it is declared in the function declaration.
Types of Functions
There are two types of functions in C programming:
Library Functions: are the functions which are declared in the C header files such as
scanf(), printf(), gets(), puts(), ceil(), floor() etc.
User-defined functions: are the functions which are created by the C programmer,
so that he/she can use it many times. It reduces the complexity of a big program and
optimizes the code.
C Array
An array is defined as the collection of similar type of data items stored at
contiguous memory locations. Arrays are the derived data type in C programming
language which can store the primitive type of data such as int, char, double, float,
etc. It also has the capability to store the collection of derived data types, such as
pointers, structure, etc. The array is the simplest data structure where each data
element can be randomly accessed by using its index number.
Properties of Array
The array contains the following properties.
Each element of an array is of same data type and carries the same size, i.e., int = 4
bytes.
Elements of the array are stored at contiguous memory locations where the first
element is stored at the smallest memory location.
Elements of
the array can be
randomly
accessed
since we can
calculate the
address of
each
element of
the array with
the given base address and the size of the data element.
Advantage of C Array
1) Code Optimization: Less code to the access the data.
2) Ease of traversing: By using the for loop, we can retrieve the elements of an array easily.
3) Ease of sorting: To sort the elements of the array, we need a few lines of code only.
4) Random Access: We can access any element randomly using the array.
Disadvantage of C Array
1) Fixed Size: Whatever size, we define at the time of declaration of the array, we can't
exceed the limit. So, it doesn't grow the size dynamically like LinkedList which we will
learn later.
Declaration of C Array
We can declare an array in the c language in the following way.
data_type array_name[array_size];
int marks[5];
int main()
return 0;
}
int main()
{
int arr[5] = { 10, 20, 30, 40, 50 };
int arr1[] = { 1, 2, 3, 4, 5 };
// array initialization using for loop
float arr2[5];
for (int i = 0; i < 5; i++) {
arr2[i] = (float)i * 2.1;
}
return 0;
}
Types of Array in C
There are two types of arrays based on the number of dimensions it has. They are as follows:
1. One Dimensional Arrays (1D Array)
2. Multidimensional Arrays
C Strings
The string can be defined as the one-dimensional array of characters terminated by a null ('\
0'). The character array or the string is used to manipulate text such as word or sentences.
Each character in the array occupies one byte of memory, and the last character must always
be 0. The termination character ('\0') is important in a string since it is the only way to
identify where the string ends. When we define a string as char s[10], the character s[10] is
implicitly initialized with the null in the memory.
322.5K
There are two ways to declare a string in c language.
1. By char array
2. By string literal
String Example in C
Let's see a simple example where a string is declared and being printed. The '%s' is
used as a format specifier for the string in c language.
1. #include<stdio.h>
2. #include <string.h>
3. int main(){
4. char ch[11]={'C', 'o', 'm', 'p', 'u', 't', 'e', 'r','\0'};
5. char ch2[11]="Computer";
6.
7. printf("Char Array Value is: %s\n", ch);
8. printf("String Literal Value is: %s\n", ch2);
9. return 0;
10. }
1. #include<stdio.h>
2. #include <string.h>
3. int main(){
4. char name[50];
5. printf("Enter your name: ");
6. gets(name); //reads string from user
7. printf("Your name is: ");
8. puts(name); //displays string
9. return 0;
10. }
C String Functions
No. Function Description
3) strcat(first_string, concats or joins first string with second string. The result of the
second_string) string is stored in first string.
4) strcmp(first_string, compares the first string with second string. If both strings are
second_string) same, it returns 0.