C Programming Language

This tutorial covers basic concepts of  C. Topics covered are Basics of C, Constants, Data  types, Operators, Control Structures, Arrays, Functions, pointers, structure, union, data file handling, characters and strings along with different libraries.

Lessons

Introduction to C Language

Preview

Complexity: Standard

Introduction to C Language C Language is a high level programming language developed at “AT & T’s Bell Laboratories”, USA in 1972. It was written by Dennis Ritchie and Brian Kernighan. Features of C Language C is a robust language having built-in functions, data types and operators to write complex programs. C Language is a …

What is a variable in C Language

Preview

What is a variable in C Language A variable may be defined as a named memory location. The value of a variable can vary during the execution of a program. Rules to name a variable in C Language There are few basics rules which must be kept in mind while defining a variable. They are …

Constants in C Language

Preview

Complexity: Standard

Constants in C Language A Constant may be defined as a quantity whose value can’t be changed during the execution of a program. Constant can be divided into following categories. 1. Numeric Constant Numeric Constant can contain digits in It can be categorized into two types: Integer Constant Floating point Constant/Real Constant 1. Integer Constant …

Data Types in C Language

Preview

Data types in C Language Data type is the way to specify the type of data and range of values that can be stored in a variable. C supports three types of data types:- Primary Data Types Derived Data Types User Defined Data Types Primary Data Types There are four primary data types provided by …

Display output in C – printf() function

Preview

Display output in C- printf() function printf() function is used to display output in C. Syntax of printf function() is: printf(“Control string”, arg1,arg2……); Control string specifies the format to enter the data. arg1,arg2,….. are constants, expression or variables whose values should be displayed. 1. print int data: Format Specifier: %d                                                                                 Display int values. Output #include<stdio.h> …

Read values in C- scanf() function

Preview

Read values in C- scanf() function scanf() function() is used to read different types of values in C. Syntax: scanf(“Control-string”, &arg1,&arg2,&arg3,……); Control-string is the format to enter data. It has % sign followed by format specifier indicating the type of variable whose value is to be input . &arg1,&arg2,&arg3,… refer to variables whose values is …

C – Unformatted Output Function

Preview

C – Unformatted Output Function Unformatted output functions provided by C are :  1. putchar() This function is used to display value of a character variable or constant. The header file required is <stdio.h>. Program to demonstrate putchar() #include<stdio.h> int main() { char n=’K’; printf(“\ncharacter=”); putchar(n); return (0); } Output character=K 2. putch() It is …

C – Unformatted Input Functions

Preview

Complexity: Standard

Unformatted Input functions Various unformatted input functions provided by C are: 1. getchar() It is used to read value of a character variable. While using this function, we need to assign this function to the character variable whose value we want to read. The header file required for this function is <stdio.h>. Program to demonstrate …

4 Types of Operators in C

Preview

Operators These are the symbols which are used to perform some calculation or manipulation on values which may be in the form of variables, constants or expressions in a C program. Operand It is the variable, constant or expression on which an operator performs some calculation, comparison or some other manipulation. Example    6+5       …

Increment Decrement Operators in C (++ / –)

Preview

1. Increment Operator This operator is represented as ++. It is used to add 1 to the existing value of a variable. It can be used in two ways:- Prefix Form In this form, the increment operator is placed before the operand. It adds one to the existing value of variable then takes the incremented …

Arithmetic Operators in C

Preview

Arithmetic Operators in C Arithmetic operators are used to perform mathematical calculations in a program. Various arithmetic operators in C language are as follows: 1. + Addition operator is represented as plus (+) sign. It is used to add values of numeric variables, constants or expressions. Example 11 + 3 = 14 2. – Subtraction …

Relational Operators in C | Comparison operators in C

Preview

Relational Operators in C These operators are used to perform comparison between values in a program. Various relational operators in C language are as follows:  1. < [Less Than] Less Than operator is represented as (<) sign. It is used to check whether one value is smaller than another value or not. Examples 10 < …

Logical Operators in C

Preview

Logical Operators in C There are three logical operators. 1. Logical And(&&) Logical And operator is represented as double ampersand sign (&&). It is used to combine two relational expressions. Truth table for Logical AND operator Input1 Input2 Output 0 0 0 0 1 0 1 0 0 1 1 1 Example 10 >3 && …

Bitwise operators in C | Bitwise Operator programs in C

Preview

Bitwise operators in C There are six bitwise operators provided by C . 1. Bitwise AND Bitwise AND operator is represented as single ampersand sign (&). If any of the input to this operator is 0, output would be 0. If all the inputs of this operator are 1, output would be 1. Values are …

Assignment Operators in C | Shorthand operators in C

Preview

Assignment operators in C Assignment/shorthand operators are used to update the value of a variable in an easy way. There are various assignment operators provided by C language. = += -= *= /= %= &= |= ^= <<= >>= Operator Description Example = It is used to assign some value to a variable. A=10 += …

Conditional operator in C | Ternary Operator in C

Preview

Conditional operator in C This operator is known as ternary operator because it has three operands. Ternary operator is represented as ?: . It is used to check a condition and depending upon the condition, particular instruction is executed by the compiler. Syntax: Condition? Block1: Block2; Condition is any relational or logical expression. Which will …

sizeof() operator in C | comma operator in C

Preview

Special operators There are many special operators provided by C language which are as follows:  sizeof()  Comma 1.  sizeof() This operator is used to find the size of a variable, constant or data type. The size returned by this operator is in terms of bytes. Program to demonstrate the use of sizeof() operator. Output (Line …

Implicit type Conversion in C | Automatic type conversion in C

Preview

Implicit type Conversion in C | Automatic type conversion in C It is the predefined method of C due to which, the output of an arithmetic expression is converted into a particular data type.  Rule says that output of expression will be of data type having highest number of bytes and range. For example In …

Explicit type conversion in C | Casting in C

Preview

Explicit type conversion in C | Casting in C It is the way to convert the output of an arithmetic expression into a specific data type. The syntax of casting is: (Data_Type) Arithmetic_Expression Data_Type is any data type of C in which we want to convert the output of an expression. Arithmetic_Expression is any arithmetic …

Precedence and Associativity of Operators in C | Expressions in C

Preview

Precedence and Associativity of Operators in C Precedence is the priority assigned to operators. When more than one operator appears in an expression, they will be evaluated as per their precedence. In expression 10+6/2, division operator (/) has more priority than addition operator (+), so 6/2 will be evaluated followed by adding 10 the the …

Control Structures in C | Control statements in C

Preview

Control Structures in C Control structure can be used to alter the normal flow of a program depending upon some Condition. Control structures can be categorized into three types: Selection Statements Iterative/Looping Statements Jumping Statements 1. Selection statements Selection statements are used to execute a particular set of statements depending upon a Condition. They are …

Simple if statement in C language

Preview

If Statement in C Language If statement is a very powerful decision making statement and it is used to control the flow of execution of statements. There are four types of if statement: Simple if If else Nested if If else if ladder Simple if statement in C language  In simple if statement, there is …

if else statement in C language

Preview

if else statement in C language In if else statement, there is only one condition. If  condition is true, one set of statements will be executed otherwise another set of statements will be executed Syntax of if else statement is: if(Condition) { Statement-Block1; } else { Statement-Block2; } Condition is any relational or logical expression …

Nested if statement in C Langauge

Preview

Nested if statement in C Langauge In nested if statement, one if statement block is enclosed within another if statement block. The syntax of nested if statement is: if(Condition-1) {             if(Condition-2)             Statement-Block-1;             else                  Statement-Block-2; } else {             if(Condition-3)             Statement-Block-3;             else                  Statement-Block-4; } Initially Condition-1 is checked. If …

if else if ladder statement in c language

Preview

if else if ladder statement in c language It is basically a multi way decision making statement. In if else if ladder statement, there are multiple conditions one after another. Depending upon these conditions, specific sets of statements are executed. The syntax of if else if ladder statement is: if(Condition-1) { Statement-Block-1; } else if(Condition-2)      …

switch statement in C | Working of switch statement in C

Preview

 Switch statement Switch statement is a multi-branch selection statement. Switch statement works with a variable or expression known as control variable or control expression. Switch tests the value of control expression/variable with a list of values known as cases of switch statement. When the value of control expression/variable matches with any of the case value, …

for statement in c language | Nesting of for in c language

Preview

for statement in c language for statement is a looping statement used to repeat a set of statements for a specified number of times. Syntax of for statement is: for(Initialize;Condition;Update) { Statements; } for is a predefined keyword of C Language . Initialize It is the way to store initial value in the control variable …

while statement in c language

Preview

while statement in c language It is an entry controlled looping statement. It is basically used when we may not be aware of how many times the loop will execute. In while statement, initialization, condition and updation are performed in different lines. The syntax of while statement is: Initialization; while(Condition) { Statement-Block; Updation; } Statement-X; …

do while statement in c

Preview

do while statement in C In do while statement initialization, condition and updation are performed in separate lines. This looping statement executes at least once. The syntax of do while statement is: Initialization; do { Statements; updation; } while(Condition); do is a keyword which specifies that do while statement has started Initialization is the way …

Jumping statements in C

Preview

Jumping Statements in C There are four types of jumping statements in C language.  1. break break is a keyword of C language.It is used to transfer the control of program out of switch or looping statement. Program of break statement Output #include<stdio.h> int main() { int i; for(i=1;i<=5;i++) { if(i==3) break; printf(“\n%d”,i); }return(0); } …

Introduction to characters in C

Preview

Introduction to characters in C Character Constant: Character constant is a single characters enclosed within the pair of single quotes (‘). It can contain any alphabet, digit, special character as well as space. We can’t store more than one character in it. Example ‘A’ , ‘5’ , ‘$’ etc. Character Variable Character variable is a …

Character functions in C

Preview

Character functions in C Character functions need ctype.h  header file to be included in the pgoram. Different character functions provided by C Language are: 1.  isalpha(): This function checks whether the character variable/constant contains alphabet or not.   2. isdigit() This function checks whether the character variable/ constant contains digit or not. 3. isalnum() This …

Strings in c language with examples

Preview

Strings in c language with examples String is group of one or more characters within double quotes (“). Example:  “Lovejot” , “2019” , “Rs4000”,  “ladderpython@gmail.com”  1. String variable? An array of char type is known as a string variable. Example: char sname[20]; In this example, sname is a string variable which can contain maximum 19 …

 String functions in c language with examples | strcpy, strcat, strcmp, strrev, strlen

Preview

 String functions in c language with examples There are various string functions which we can use in C Language. We need to include header file string.h in our program to use these functions in our program. Various string functions in c language are: strcpy() strncpy() strcat() strlen() strrev() strcmp() strcmpi() strncmp() strlwr() strupr() 1. strcpy() …

Array of strings in c language with examples

Preview

Array of strings in c language with examples It is actually a two dimensional array of char type. Example: char names[6][30]; In above example, names is an array of strings which can contain 6 string values. Each of the string value can contain maximum 30 characters. 1. Initialization of array of strings We can initialize an …

Introduction to Array in C | One dimensional array in C

Preview

Introduction to Array in C Array is a collection of values having same data type and size. Elements of array are internally stored in consecutive locations. We can access individual elements of array by using index of array elements where index is an integer value starting from 0. Types of Array There are three types …

Two dimensional array in C | Initialization of Two dimensional array in C

Preview

Two dimensional array in C Two dimensional array is an array whose elements are represented in the form of rows and columns. The syntax for declaring a two dimensional array is: <Type> Array_name[ROW][COL]; Type refers to data type of array like int, float, char etc. Array_name  is the name of array as defined by the …

Multi dimensional Array in C

Preview

Multi dimensional Arrays An array containing more than two dimensions is called multi-dimensional array. We can use more than two pairs of brackets to specify the size or refer any array element Syntax : <Data_Type> Array_name[s1][s2][s3]……………….; s1,s2,s3,…. specify the dimensions in the multidimensional array. They should be integer values. Program of  multi dimensional array. #include<stdio.h> …

Introduction to function in C Language

Preview

Introduction to function It is defined as named block of statements . Advantages It is very easy to locate and correct errors . It reduces complexity of code. Testing of a program becomes easier. Bigger softwares problems can be divided into smaller sections using functions. Functions can be reused. It is simple to understand a …

Types of function in C Language

Preview

Types of function in C Language A function is categorized into two types: 1. Library Functions These are predefined functions existing in header files.  These functions are basically meant for basic input/output, complex mathematical calculations and several other tasks. 2.  User Defined Function This type of function is created by programmers. They are mainly created …

Arguments passing in C | Call by value | Call by reference

Preview

Arguments passing in C Arguments can be passed to a function in two ways: Call by value Call by reference Call by Value It is also termed as passing arguments by value. In this case, the values of actual arguments are copied to formal arguments on one to one basis in function definition and any …

Recursion in C | Programs of recursion in c

Preview

Recursion in C Function repeatedly calling itself is called recursion. But we need to specify some condition to stop execution of recursive function otherwise function may run infinite.  Program using recursion to find factorial of a number . #include<stdio.h> int fact(int no) { if(n0==1||n0==0) return(1); else return(no*fact(no-1)); } int main() { int f1,n; printf(“\nEnter a …

Scope of variable in C | Local variable in C | Global variable in c

Preview

Scope of variable in C Scope is the lifetime of variables. Variables have two types depending upon scope. Local variable Global variable  a.  Local variable Variable declared inside a function body is called local variable. It can exist only within the function in which it is declared.  b. Global variable Variable declared before all function …

Storage class in C | auto , register, extern, static

Preview

Storage class in C Storage class is basically used to specify the scope and lifetime of a variable. It specifies the place where the variable may be stored i.e. memory or CPU registers. It also specifies the initial value of a variable, if the initial value is not specified in the program. It also specifies …

Introduction to pointer in C | pointer operations in C

Preview

Introduction to pointer It is the way to refer a memory location. A pointer variable  can contain the address of another variable. Suppose we declare integer variable a as : int a=10; Variable a has following properties : 1. Name of variable: A variable must have a name. In above example, a is a variable …

Pointer with Array in C

Preview

Pointer with Array in C Pointers are associated closely with arrays because arrays are implemented internally in the form of pointers. One dimensional array with Pointer Suppose we have a one dimensional array as int a[4]={10,33,55,07}; We can refer to array elements as: a[0] can be referred as *(a+0) a[1] can be referred as *(a+1) …

Pointer with Strings in C

Preview

Pointer with Strings in C A string variable can be declared using pointer as: char *name; name is a string variable that can hold any number of characters in it and there is no wastage of space as size will be automatically adjusted  depending upon the number of characters stored in this string variable. Program …

Pointer with function in C | Pointer to function in C

Preview

Pointer with function in C We can use pointers with functions in following ways: Passing function arguments by reference In this case, the addresses of actual arguments are stored in formal arguments in function definition and any changes made to the formal arguments in the function body are reflected back to the calling function and …

void type pointer

Preview

void pointer In C language we can also declare a void type pointer variable. void type pointer variable can contain address of variable of any other data type . Program of void pointer. #include<stdio.h> int main() { int var1=20; void *var2; var2=&var1; printf(“var2=%u”,var2); return(0); } Output var2=65524 Best Books of C

Pointer to pointer in c

Preview

Pointer to pointer A pointer variable in C language can also contain address of another pointer variable. This concepts is called pointer to pointer. We can declare a pointer to pointer as: int **ptr; Here, ptr is a pointer to pointer. Program of pointer to pointer. #include<stdio.h> int main() { int var1=20,*var2,**ptr; var2=&var1; ptr=&var2; printf(“\n*ptr=%u”,*ptr); …

Dynamic memory allocation in C

Preview

Dynamic memory allocation in C We can dynamically allocate and de-allocate memory to variables in C language.  Different functions for dynamic memory allocation/de-allocation are: malloc() free() 1. malloc() function malloc() is a predefined function of C language that can dynamically allocate memory to variables. It allocates a block of fixed size block from memory. Header …

Introduction to Structure in C

Preview

Introduction to Structure in C Structure is a user defined data type provided by C language. It is a collection of heterogeneous types of elements i.e. it is a collection of different data types, size and range. It is a tool for handling a group of logically related data items. For example, we can use …

Nesting of Structure in C

Nesting of Structure in C Defining one structure within another structure. is known as  nesting of structure. We can do it as follows:   Program to demonstrate nesting of structure. #include<string.h> #include<stdio.h> struct dob { int day; int month; int year; }; struct student { int rollno; char name[20]; struct dob d; }; int main() …

Structure type Array

Preview

Structure type Array Structure type array can be created just like array of basic data types is created. Example: struct emp e[20]; In this example, array with name e of structure emp has been declared with size 20. Program of structure type array. #include<string.h> #include<stdio.h> struct emp { int empid; char ename[20]; }; int main() …

Structure type arguments in C

Preview

Structure type arguments in C  Structure type variables can be passed as arguments to functions. Two methods to pass structure type arguments are: To pass each structure element as argument independently. To pass structure type variable as argument. To pass each structure element as argument independently In this case Each individual element of a structure …

Function returning structure in C

Preview

Function returning structure in C We can make return type of any function to be a structure. The very main thing to be noted in this case is that the function must be returning the variable of structure type. Program to specify a structure as the return type of function. #include<stdio.h> struct data { int …

Structure with Pointers in C

Preview

Structure with Pointers in C Syntax to declare structure type pointer variable is: struct Struct_name  *var; var represents pointer variable of structure. We can use pointer variable of structure to access its elements pointer in two ways: (->) arrow operator (.) dot operator Program to demonstrate use of pointer variable of a structure. #include<stdio.h> struct …

Self Referential Structure

Preview

Self Referential Structure Self referential structure contains a pointer variable of itself . it is mainly used for implementing data structures like stack, queue, linked list, trees and graphs in C. Example struct STACK { int *top; struct STACK *Link; }; Best Books of C

Introduction to Union

Preview

Introduction to Union It looks same as structure but implementation is different. Difference between structure and union is that the members of structure get different memory locations but members of a union share common memory location. C Language Compiler allocates memory to hold that element of union which has the largest size. Syntax union <uname> …

Data File Handling in C

Preview

Data File Handling in C Data File handling is the way to store data permanently on computer. There are two ways of storing data in files: 1. Text Format In this case data is stored in form of ASCII characters. Data is stored in files with every line ended  by new line character. They can …

Steps to create a data file in C

Preview

Steps to create a data file in C 1. Declaration of file pointer variable File pointer is pointer variable of predefined structure FILE. When we need to store some data in a file, we have to specify the name of data file by using its file pointer variable. We can declare a file pointer variable …

File Input output in C

Preview

File Input output in C Different predefined functions of C language for file input/output are: 1.  fputc()/putc() function These functions are used to insert single character to a specified file. Syntax: fputc(C, Fpointer); OR putc(C, Fpointer);   C refers to character variable or constant whose value should be stored into a file. Fpointer  is the …

Random Access of Files in C

Preview

Random Access We can randomly position file pointer at any position in the data file. There are two functions to do this: fseek() ftell() 1. fseek() function fseek() function is used to take file pointer to a particular position data file. Syntax: fseek(Fpointer, Position, Initial); Fpointer is name of file pointer variable. Position number of …

Command Line Arguments in C

Preview

Command Line Arguments in C It is possible to values at command prompt using command line arguments in C. We need to specify the name of program followed by values we want to pass as arguments . To implement command line arguments main() must have two parameters as: main ( int argc, char *argv[] ) …

C program to find reverse of a number

Preview

C program to find reverse of a number #include<stdio.h> #include<conio.h> void main() { int m,n,r; clrscr(); printf(“\nEnter a number:=”); scanf(“%d”,&n); //123 r=0; while(n>0) { m=n%10; //3 2 1 r=r*10+m; //3 32 321 n=n/10; //12 1 0 } printf(“\nReverse=%d”,r); getch(); } Explanation /* n=123 r=0 m=3 r=3 n=12 m=2 r=32 n=1 m=1 r=321 n=0 */ Amozo …

C program to check whether a number is palindrome or not

Preview

C program to check whether a number is palindrome or not. #include<stdio.h> #include<conio.h> void main() { int m,n,r,t; printf(“\nEnter a number:=”); scanf(“%d”,&n); //n=1221 t=n; //t=1221 r=0; while(n>0) { m=n%10; r=r*10+m; n=n/10; } //r=1221 if(t==r) printf(“\nNumber is palendrome”); else printf(“\nNumber is not palendrome”); getch(); } pTron Bassbuds in-Ear True Wireless Bluetooth Headphones (TWS) with Mic – …