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 Both digits and alphabets are allowed to name a variable. We can use both capital and small …

Constants in C Language

Preview

Complexity: Standard

Constants in C Language It is a quantity whose value can’t be changed in a program while executing . Constant is of following types. 1. Numeric Constant It can contain digits in It is of two types: Integer Constant Real Constant 1. Integer Constant Integer Constants can contain numbers without a decimal point.  They are …

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 It is represented as (++). It adds one to existing value of a variable. There are two ways to use it. Prefix Form In this type, increment operator is fixed before the variable name. It immediately adds one to the existing value of variable . Example: ++a. Postfix Form In this type, …

Arithmetic Operators in C

Preview

Arithmetic Operators in C These  operators are used to perform mathematical calculations in a program. Various arithmetic operators in C language are as follows: 1. + Addition operator (+) is used to add numeric values. Example 11 + 3 = 14 2. – Subtraction operator (-) is used to subtract one numeric quantity from another …

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 used to check for whether one value is smaller than another value or not. Result is  1 or 0. 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. If any input is 0, output is 0. If all inputs are 1, output is 1. Truth table for Logical AND operator Input1 Input2 Output …

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 Operator This operator is represented as & (Ampersand symbol) . If any input of this operator is 0, output is 0. If all the inputs are 1, output is 1. Values are firstly converted into their corresponding bitwise …

Assignment Operators in C | Shorthand operators in C

Preview

Assignment operators in C Assignment operators are also known as shorthand operators. They are used to increase or decrease the value of a variable. Various assignment operators are: = += -= *= /= %= &= |= ^= <<= >>= 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 also known as ternary operator has three operands. It is represented as ?: . It checks a condition and depending upon the condition, particular instruction is executed . Syntax: Expression? Code1: Code2; Expression represents some relational or logical expression. Whose output decides which block of statements will  execute. Code1 …

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() sizeof() operator finds the size of a constant, variable or a data type. Size is in terms of bytes. **Output may change depending upon the compiler. Program Output   #include<stdio.h> int main() { int num1; float …

Implicit type Conversion in C | Automatic type conversion in C

Preview

Length: 0 minutes

Implicit type Conversion in C | Automatic type conversion in C It is the process in which data type of output of an arithmetic expression is automatically converted into a particular data type.  The output of expression will be of that data type which has highest number of bytes and range. Rules of implicit conversion …

Explicit type conversion in C | Casting in C

Preview

Length: 0 minutes

Explicit type conversion in C | Casting in C It is the process of converting data type of the output of an arithmetic expression into a user specific data type as per his requirement. The syntax of casting is: (Data_Type) Arithmetic_Expression Data_Type is a valid data type of C language in which we want to …

Precedence and Associativity of Operators in C | Expressions in C

Preview

Length: 0 minutes

Precedence and Associativity of Operators in C Precedence refers oi the priority given to different operators in C language. Associativity refers to sequence of evaluation of operators in an expression if operators of same priority appear .   Precdence OPERATOR ASSOCIATIVITY 1 (), [] Left to Right 2 !, ~, ++, –, Unary +, Unary …

Control Structures in C | Control statements in C

Preview

Length: 0 minutes

Control Structures in C Control structure are C language statements used to control or change the flow of a program.  Control structures are divided into three categories: Selection Statements Iterative/Looping Statements Jumping Statements 1. Selection statements Selection statements execute a set of statements depending upon some Condition. They are categorized into two types: if statement …

Simple if statement in C language

Preview

Length: 0 minutes

If Statement in C Language It is a very useful decision making statement and it is used to control or alter the flow of execution of C language statements in a C Program. It is categorized into four types: Simple if statement If else Statement Nested if Statement If else if ladder Statement Simple if …

if else statement in C language

Preview

Length: 0 minutes

if else statement in C language if else statement contains one condition. If  condition evaluates to true, one set of statements will execute. If condition is false other set of statements will execute. Syntax of if else statement is: if(Condition-Expression) { Statements-1; } else { Statements-2; } Condition-Expression is any conditional expression. If it is true, …

Nested if statement in C Langauge

Preview

Length: 0 minutes

Nested if statement in C Langauge Nested if statement contains one if statement block taken inside another if statement. The syntax of nested if statement is: if(Condition-Expression1) {             if(Condition-Expression2)             Statements1;             else                  Statements2; } else {             if(Condition-Expression3)             Statements3;             else                  Statements4; } Initially Condition-Expression1 is checked. If it is …

if else if ladder statement in c language

Preview

Length: 0 minutes

if else if ladder statement in c language In if else if ladder statement, multiple conditions are defined one after another .  Different sets of statements are executed depending upon these conditions. Syntax : if(Condition-Expression1) { Statements-1; } else if(Condition-Expression2)      { Statements-2; } else if (Condition-Expression3) { Statements-3; } : : else      { Statements-N; } …

switch statement in C | Working of switch statement in C

Preview

Length: 0 minutes

 Switch statement in C It is a selection statement that works with an expression or variable. It compares the value of expression or variable with values called cases of switch statement. It value of expression or variable matches with any case value, statements linked with that case gets executed. This Expression or variable can be …

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

Preview

Length: 0 minutes

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:  In this part, we assign some value to the control variable which …

while statement in c language

Preview

Length: 0 minutes

while statement in c language While statement is an entry controlled iterative statement. The syntax of while statement is: Initialize; while(Condition) { Statements; Update; } while is a keyword to start while statement. Initialize:  In this part, we assign some value to the control variable which controls number of times the loop should execute. Example …

do while statement in c

Preview

Length: 0 minutes

do while statement in C do while statement is an exit controlled looping statement. initialization, condition and updation are performed in separate lines. It executes at least once. The syntax of do while statement is: Initialize; do { Statements; update; } while(Condition); do is a keyword which specifies that do while statement has started Initialize:  …

Jumping statements in C

Preview

Length: 0 minutes

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

Length: 0 minutes

Introduction to characters in C Character Constant: Character constant is a single characters enclosed within the pair of single quotes (‘). It can contain alphabet, digit, special character and space. Example ‘A’ , ‘5’ , ‘$’ etc. Character Variable Character variable is a variable of char data type. Example : char gender; Character Variable Initialization: …

Character functions in C

Preview

Length: 0 minutes

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

Length: 0 minutes

Strings in c language with examples String is defined as collection of multiple characters within inverted commas(“). Example:  “Lovejot” , “2019”  1. String variable Character type array is known as string variable. Example: char sname[20]; 2. Initialization of a string variable We can intialize string variable as char var[Size]={List of characters}; Example:  char name[6]={‘r’,’o’,’h’,’i’,’t’}; OR …

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

Preview

Length: 0 minutes

 String functions in c language with examples We should use string.h header file in a program to use string functions in a C program. Various string functions are: 1. strcpy() This function can be used to copy some value into a string variable  Syntax: strcpy(Target, Source); Target is the string variable in which want to …

Array of strings in c language with examples

Preview

Length: 0 minutes

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

Introduction to Array in C | One dimensional array in C

Preview

Length: 0 minutes

Introduction to Array in C Array is a collection of values having same data type and size. Values in array are internally stored in consecutive locations. Elements of an  array can be processed by using index positions of elements where  index starts from 0 that means first array element is at at index 0. There …

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

Preview

Length: 0 minutes

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

Length: 0 minutes

Multi dimensional Arrays An array containing multiple dimensions(more than two) is known as multi-dimensional array. We can use more than two pairs of brackets to specify the size or refer any array element Syntax : <Type> Array_name[size1][size2][size3]……………….; size1,size2,size3,…. specify the dimensions in the multidimensional array. They should be integer values. Program  #include<stdio.h> int main() { …

Introduction to function in C Language

Preview

Length: 0 minutes

Introduction to function in C Language It is defined as named block of statements . Advantages Easy to diagnose errors in program . Reduced complexity. Easier testing of a program . Functions help to divide big softwares into smaller sections. Reusability. Simple to understand . Efficient memory utilization. Reduces repetition of code. Creating a function …

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

Length: 0 minutes

Arguments passing in C Arguments can be passed to a function in two ways: Call by value Call by reference Call by Value In this case, actual arguments are copied to formal arguments one after another. Changes made to the formal arguments have no effect on the actual arguments . Program #include<stdio.h> void show(int a) …

Recursion in C | Programs of recursion in C

Preview

Length: 0 minutes

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

Length: 0 minutes

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 inside a function is called local variable. It exists only within the function in which it is declared.  b. Global variable Variable declared before all functions is known as …

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

Preview

Length: 0 minutes

Storage class in C Storage class specifies the scope of a variable in a program. It demonstrates whether variable gets stored in RAM or registers. There are four types of storage classes in C language. auto  register  static  extern  1.  auto  They are also called automatic, local or internal variables. This storage class specifies  automatic …

Introduction to pointer in C | pointer operations in C

Preview

Length: 0 minutes

Introduction to pointer Pointer is used to reference a memory location. Pointer variable  in C be used to store address of other variable. Different properties of variable are: 1. Name: A variable must have a name. It must follow rules of valid identifier in C. 2. Value of variable: Variable should be assigned some value. …

Pointer with Array in C

Preview

Length: 0 minutes

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

Length: 0 minutes

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

Length: 0 minutes

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

Length: 0 minutes

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 Sellers

Pointer to pointer in c

Preview

Length: 0 minutes

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 #include<stdio.h> int main() { int var1=20,*var2,**ptr; var2=&var1; ptr=&var2; printf(“\n*ptr=%u”,*ptr); printf(“\n**ptr=%d “,**ptr); return(0); } …

Dynamic memory allocation in C

Preview

Length: 0 minutes

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

Length: 0 minutes

Introduction to Structure in C Structure is a user defined data type which is a collection of different variables of different types.  Syntax for structure is: struct <struct_name> { <type1> var1; <type2> var2; : : <typeN> var-N; }; struct is the keyword specifying the beginnning of structure. struct_name is the name of structure. which should …

Nesting of Structure in C

Length: 0 minutes

Nesting of Structure in C Defining one structure within another structure. is known as  nesting of structure. We can do it as follows: Program  #include<stdio.h> struct DATE { int d; int m; int y; }; struct stud { int rno; char n[20]; struct DATE d1; }; int main() { struct stud s1; s1.rno=1; strcpy(s.n,”LadderPython”); s.d1.d=1; …

Structure type Array

Preview

Length: 0 minutes

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

Length: 0 minutes

Structure type arguments in C  Two methods to pass structure type arguments are: Pass structure elements as arguments independently. Pass structure variable as argument. To pass structure elements as argument independently Program #include<stdio.h> struct emp { int empid; float sal; };void show(int i,float s) { printf(“\n%d,%f”,i,s); } int main() { struct emp e1={101,5500.00}; show(e1.empid,e1.sal); return(0); …

Function returning structure in C

Preview

Length: 0 minutes

Function returning structure in C Return type of any function can be a structure but the function must return the variable of structure type. Program #include<stdio.h> struct data { int a,b; };struct data show(struct data d) { ++d.a; –d.b; return(d); } int main() {struct data d1={34,55}; struct data s; s=show(d1); printf(“\ns.a=%d”,s.a); printf(“\ns.b=%d”,s.b); return(0); } Output …

Structure with Pointers in C

Preview

Length: 0 minutes

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

Length: 0 minutes

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 Sellers

Introduction to Union

Preview

Length: 0 minutes

Introduction to Union It looks same as structure but difference between structure and union is that the members of structure get different memory locations but members of a union share common memory location. Syntax union <uname> { <Data-type1> var1; <Data-type2> var2; : }; uname is the name of union specified by programmer. Data-type1, Data-type2,…  are …

Data File Handling in C

Preview

Length: 0 minutes

Data File Handling in C Data File handling is the way to store data permanently on some storage device. There are two ways of storing data in files: 1. Text – Format In text format, data is stored directly in form of ASCII characters. Every line is terminated  by a new line character. They can …

Steps to create a data file in C

Preview

Length: 0 minutes

Steps to create a data file in C 1. File Pointer Declaration  File pointer is a 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 as …

File Input output in C

Preview

Length: 0 minutes

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(CH, Fpointer); OR putc(CH, Fpointer); CH refers to character variable or constant whose value should be stored into a file. Fpointer  is the file …

Random Access of Files in C

Preview

Length: 0 minutes

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

Length: 0 minutes

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 – …