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
What is Programming Language?
PreviewIntroduction to C Language
PreviewIntroduction 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
PreviewWhat 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
PreviewConstants 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
PreviewData 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
PreviewDisplay output in C- printf() function in C Language 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 type data in C Language Format …
Read values in C- scanf() function
PreviewRead values in C- scanf() function in C 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 …
C – Unformatted Output Functions
PreviewC – Unformatted Output Functions Unformatted output functions provided by C are : 1. putchar() function in |C This function is used to display value of a character variable or constant. The header file required is <stdio.h>. Syntax: putchar(var); var refers to the character variable or character constant whose value we want to display using putchar() …
C – Unformatted Input Functions
PreviewUnformatted Input functions in C Language 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>. …
4 Types of Operators in C
PreviewOperators 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 | Increment operator in C | Decrement operators in C
PreviewIncrement Decrement Operators in C | Increment operator in C | Decrement operator in C 1. Increment Operator in C (++) Increment operator in C is used to add 1 to the existing value of a variable. It can be used in two ways:- Prefix Form of increment operator in C In prefix form of …
Arithmetic Operators in C
PreviewArithmetic 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
PreviewRelational Operators in C | Comparison operators in C These operators are used to perform comparison between values . 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. …
Logical Operators in C | Logical AND in C | Logical Or in C | Logical NOT in C
PreviewLogical Operators in C | Logical AND in C | Logical Or in C | Logical NOT in C There are three logical operators. 1. Logical AND in C (&&) Logical AND (&&) is used to combine two relational expressions. If any input of this operator is 0, output will be 0. Truth table for …
Bitwise operators in C | Bitwise Operator programs in C | Bitwise AND in C | Bitwise OR in C | Bitwise XOR in C | Bitwise complement in C | Bitwise left shift in C | Bitwise right shift in C
PreviewBitwise operators in C | Bitwise AND in C | Bitwise OR in C | Bitwise XOR in C | Bitwise complement in C | Bitwise left shift in C | Bitwise right shift in C Bitwise operators work with the binary form of numbers in C. There are six bitwise operators provided by C . …
Assignment Operators in C | Shorthand operators in C
PreviewAssignment Operators in C | Shorthand 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 …
Conditional operator in C | Ternary Operator in C
PreviewConditional operator in C | Ternary Operator in C Conditional operator/ternary operator has three operands. It is represented as ?: . It tests a condition and depending upon condition, specific instruction gets executed . Syntax: Condition? Statement: Statement2; Condition represents a conditional expression. Statement1 represents the statement to be executed if condition is true. Statement2 …
sizeof() operator in C | comma operator in C | Special operators in C
Previewsizeof() operator in C | comma operator in C | Special operators in C There are many special operators provided by C language which are as follows: sizeof() Comma 1. sizeof() operator in C This operator returns the size of a variable, constant or data type in terms of bytes. Program to demonstrate the use …
Implicit type Conversion in C | Automatic type conversion in C
PreviewImplicit 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
PreviewExplicit 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
PreviewPrecedence 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 | Selection statements in C | Types of Looping in C
PreviewControl Structures in C | Control statements in C | Selection statements in C | Types of Looping 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 in …
Simple if statement in C language
PreviewSimple if statement in C language Simple if statement in C language 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 …
if else statement in C language
Previewif 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(Test) { Block1; } else { Block2; } Test is any conditional expression …
Nested if statement in C Language
PreviewNested if statement in C Language In nested if statement, one if statement block is enclosed within another if statement block. The syntax of nested if statement is: if(Test-1) { if(Test-2) Block-1; else Block-2; } else { if(Test-3) …
if else if ladder statement in c language | ladder if statement in C
Previewif else if ladder statement in c language | ladder if statement in C 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: …
switch statement in C | Working of switch statement in C
Previewswitch statement in C | Working of switch statement in C It is a multi-branch selection statement that works with a variable or expression whose value is compared with a list of values known as cases of switch statement. If value of expression/variable matches with any of the case value, the block of statements associated …
for statement in c language | Nesting of for in c language | for loop in C
Previewfor statement in c language | Nesting of for in c language | for loop in C 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 …
while statement in C++ | while loop in C++
Previewwhile statement in C++ | while loop in C++ 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) { …
while statement in C language | while loop in C language
Previewwhile statement in C language | while loop 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; …
do while statement in c | do while loop in C
Previewdo while statement in C | do while loop 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 …
Jumping statements in C | break statement in C | continue statement in C | goto statement in C | exit in C
PreviewJumping statements in C | break statement in C | continue statement in C | goto statement in C | exit in C There are four types of jumping statements in C language. 1. break statement in C break is a keyword of C language. break statement in C is used to transfer the control …
Introduction to characters in C
PreviewIntroduction 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
PreviewCharacter 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 | Reading and displaying a string variable in C | Initialization of a string variable in C
PreviewStrings in c language with examples | Reading and displaying a string variable in C | Initialization of a string variable in C String is group of one or more characters within double quotes (“). Example: “Lovejot” , “2019” , “Rs4000”, “[email protected]” 1. What is a String variable in C ? An array of char …
String functions in c language with examples | strcpy, strcat, strcmp, strrev, strlen in C
PreviewString functions in c language with examples | strcpy, strcat, strcmp, strrev, strlen in C We need to include string.h header file in a program to use string functions. Different string functions in c language are: 1. strcpy() strcpy() is one of the most popular String functions in c language . This function is used store …
Array of strings in c language with examples
PreviewArray of strings in c language with examples | String array in C Language 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 …
Introduction to Array in C | One dimensional array in C
PreviewIntroduction to Array in C Language 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 in C Language …
Two dimensional array in C | Initialization of Two dimensional array in C
PreviewTwo dimensional array in C | Initialization of 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 …
Multi dimensional Array in C
PreviewMulti dimensional Array in C Language 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 …
Introduction to function in C Language
PreviewIntroduction to function in C Language It is defined as named block of statements . Advantages of function in C Language It is very easy to find and correct errors . It reduces complexity . Testing becomes easier. Bigger problems can be divided into smaller sections . They can be reused. Simple to understand a …
Types of function in C Language | Function types in C Language
PreviewTypes of function in C Language | Function types in C Language A function is categorized into two types: 1. Library Functions in C Language These are predefined functions that exist in header files of C Language. These are meant for input/output, complex mathematical functions and various other tasks. 2. User Defined Function in C Language …
Arguments passing in C | Call by value in C | Call by reference in C
PreviewArguments passing in C | Call by value in C | Call by reference in C Arguments can be passed to a function in two ways: Call by value Call by reference Call by Value in C Language In this method, values of actual arguments get copied to formal arguments one to one basis. Changes …
Recursion in C Language | Programs of recursion in C Language
PreviewRecursion in C Language | Programs of recursion in C Language Function repeatedly calling itself is called recursion. We must specify some condition to stop recursion otherwise function may run infinitely. Program using recursion to find factorial of a number . #include<stdio.h> int factorial(int no) { if(no==1||no==0) return(1); else return(no*factorial(no-1)); } int main() { int …
Scope of variable in C Language | Local variable in C Language | Global variable in C Language
PreviewScope of variable in C Language | Local variable in C Language | Global variable in C Language Scope is the lifetime of variables. Variables have two types depending upon scope. Local variable Global variable a. Local variable in C Language Variable declared inside a function body is called local variable. It can exist only …
Storage class in C Language | auto , register, extern, static in C Language
PreviewStorage classes in C Language | auto , register, extern, static in C Language Storage class specifies the scope of a variable in a program. It specifies the whether the variable will be stored in RAM or registers. It specifies which parts of a program can access the variable and how long the variable stays …
Introduction to pointer in C Language | pointer operations in C Language | Null pointer in C
PreviewIntroduction to pointer in C Language | pointer operations in C Language 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 …
Pointer with Array in C | One dimensional array with Pointer | Two dimensional array with pointer
PreviewPointer with Array in C | One dimensional array with Pointer | Two dimensional array with pointer Pointers are associated closely with arrays because arrays are implemented internally in the form of pointers. One dimensional array with Pointer in C Language Suppose we have a one dimensional array as int a[4]={10,33,55,07}; We can refer to …
Pointer with Strings in C Language | String pointer in C Language
PreviewPointer with Strings in C Language | String pointer in C Language 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 …
Pointer with function in C | Pointer to function in C
PreviewPointer with function in C We can use pointers with functions in following ways: Function returning pointer In C language, a function can even return a pointer like other data types. The syntax for function returning a pointer is: Data_type *fun_name(Arguments); Program to demonstrate a function returning pointer #include<stdio.h> int *sum(int a,int b) { int …
void type pointer in C Language | using void pointer in C
Previewvoid type pointer in C Language | using void pointer in C 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); } …
Pointer to pointer in c
PreviewPointer to pointer in C Language 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 in C #include<stdio.h> int main() { …
Dynamic memory allocation in C
PreviewDynamic 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 | Structure variable in C | Accessing structure members in C
PreviewIntroduction to Structure in C Structure is a user defined data of C language. It is a collection of different values of same or different data types. It is basically used to group logically related data. In C language, syntax for defining a structure is: struct <Struct_name> { <datatype-1> var1; <datatype-2> var2; : : <datatype-N> …
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 dd; int mm; int yy; }; struct std { int roll; char sname[20]; struct dob d1; }; int main() …
Structure type Array
PreviewStructure 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
PreviewStructure type arguments in C Structure type variables can be passed as arguments to functions. . In this case structure variable is passed as an actual argument to a function Program to demonstrate passing structure type variable as argument . #include<stdio.h> struct demo { int x,y; }; void disp(struct demo d1) { ++d1.x; – -d1.y;printf(“\nd1.x=%d”,d1.x); …
Function returning structure in C
PreviewFunction 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
PreviewStructure 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
PreviewSelf 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 NODE { float data; struct NODE *link; }; int main() { struct NODE *START = NULL ; struct NODE *N ; N …
Introduction to Union in C Language
PreviewIntroduction to Union in C Language 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. …
Data File Handling in C
PreviewData 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. Human beings …
Steps to create a data file in C
PreviewSteps 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
PreviewFile 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
PreviewRandom Access There are two predefined functions in C language to provide random access in files: fseek() ftell() 1. fseek() fseek() function can be used to take file pointer to a particular position inside a file. Syntax: fseek(Pointer, Position, Starting); Pointer is name of file pointer. Position is number of characters to be jumped. Staring …
Command Line Arguments in C
PreviewCommand Line Arguments in C We can provide values at command prompt using command line arguments. We have to specify the program name followed by values to be sent as arguments . Command line arguments to main() have two parameters passed as main ( int argc, char *argv[] ) argc specifies the number of arguments …
C program to find reverse of a number
PreviewC program to find reverse of a number #include<stdio.h> #include<conio.h> int main() { int m,n,r; 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(); return 0; } Explanation /* n=123 r=0 m=3 r=3 n=12 m=2 r=32 n=1 m=1 r=321 n=0 */ …
C program to check whether a number is palindrome or not
PreviewC 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(); } Popular Books of Computer Science