C Langage
C Langage
int main()
{
printf("Hello World!");
return 0;
}
Output
Hello World!
Here,
Variables
A variable is the name given to the memory location that stores some data.
Syntax of Variable
data_type variable_name;
data_type variable_name = initial_value;
Local Variable
Global Variable
Static Variable
Extern Variable
Auto Variable
Register Variable
Note: There are a few rules which we have to follow while naming a variable.
Data Types
The data type is the type of data that a given variable can store. Different data
types have different sizes. There are 3 types of data types in C:
Basic data types are built-in in the C programming language and are independent of
any other data type. There are x types of basic data types in C:
char c = 'a';
int integer = 24;
float f = 24.32;
double d = 24.3435;
void v;
The size of these basic data types can be modified using data type modifiers which
are:
short
long
signed
unsigned
Derived data types are derived from the basic data types. There are 2 derived data
types in C:
Arrays
Pointers
The user-defined data types are the data types that are defined by the programmers
in their code. There are 3 user-defined data types in C:
Structure
Union
Enumeration
Identifiers
Example of Identifiers
Keywords
Keywords are the reserved words that have predefined meanings in the C compiler.
They cannot be used as identifiers.
Example of Keywords
auto,
float,
int,
return,
switch
The basic input and output in C are done using two <stdio.h> functions namely
scanf() and print() respectively.
Basic Output – print()
The printf() function is used to print the output on the standard output device
which is generally the display screen.
Syntax of printf()
printf("formatted-string", ...{arguments-list});
where,
The scanf() function is used to take input from the standard input device such as
the keyboard.
Syntax of scanf()
scanf("formatted-string", {address-argument-list});
where,
int main()
{
int roll_num;
char name[50];
return 0;
}
Output
Input
Output
Format Specifiers
Format specifiers are used to describe the format of input and output in formatted
string. It is different for different data types. It always starts with %
Format Specifier
Description
%c
For b type.
%d
For signed integer type.
%f
For float type.
%lf
Double
%p
Pointer
%s
String
%u
Unsigned int
%%
Prints % character
Escape Sequence
Escape sequences are the characters that are used to represent those characters
that cannot by represented normally. They start with ( \ ) backslash and can be
used inside string literals.
Operators are the symbols that are used to perform some kind of operation.
Operators can be classified based on the type of operation they perform.
1.
Arithmetic Operators Operators that perform arithmetic operations. +, -,
*, /, %
2.
Relational Operators They are used to compare two values. <, >, <=,
>=, ==, !=
3.
Bitwise Operators They are used to perform bit-level operations on
integers. &, ^, |, <<, >>, ~
4.
Logical Operators They perform logical operations such as logical AND,
logical OR, etc. &&, ||, !
5.
Conditional Operators The conditional Operator is used to insert
conditional code. ? :
6.
Assignment Operators They are used to assign some value to the variables.
=, +=, -=, <<=
7.
Miscellaneous Operators comma, addressof, sizeof, etc. are some other
types of operators. , sizeof, &, *, ->, .
Conditional Statements
Conditional statements are used to execute some block of code based on whether the
given condition is true. There are the following conditional statements in C:
1. if Statement
if statement contains a block of code that will be executed if and only if the
given condition is true.
Syntax of if
if (condition) {
// statements
}
2. if-else Statements
The if-else statement contains the else block in addition to the if block which
will be executed if the given condition is false.
Syntax if-else
if (expression) {
// if block
}
else {
// else block
}
3. if-else-if Ladder
The if-else-if ladder is used when we have to test multiple conditions and for each
of these conditions, we have a separate block of code.
Syntax of if-else-if
if (expression) {
// block 1
}
else if (expression) {
// block 1
}
.
.
.
else {
// else block
}
The switch case statement is an alternative to the if-else-if ladder that can
execute different blocks of statements based on the value of the single variable
named switch variable.
Syntax of switch
switch (expression) {
case value1:
// statements
break;
case value2:
// statements
break;
.
.
.
default:
// defualt block
break;
}
5. Conditional Operator
The conditional operator is a kind of single-line if-else statement that tests the
condition and executes the true and false statements.
int main()
{
// conditional operator will assign 10 if 5 < 25,
// otherwise it will assign 20
int i = 5 < 25 ? 10 : 20;
if (i == 10)
printf("i is 10");
else if (i == 15)
printf("i is 15");
else if (i == 20)
printf("i is 20");
else
printf("i is not present");
}
Output
i is 10
Loops
Loops are the control statements that are used to repeat some block of code till
the specified condition is false. There are 3 loops in C:
1. for Loop
Syntax of for
2. while Loop
The while loop is also an entry-controlled loop but only the condition is the part
of is syntax.
Syntax of while
while (condition) {
// initialization
}
3. do-while Loop
Syntax of do-while
do {
// statements
} while (condition);
Jump Statements
Jump statements are used to override the normal control flow of the program. There
are 3 jump statements in C:
1. break Statement
It is used to terminate the loop and bring the program control to the statements
after the loop.
Syntax of break
break;
The continue statement skips the current iteration and moves to the next iteration
when encountered in the loop.
Syntax of continue
continue;
3. goto Statement
The goto statement is used to move the program control to the predefined label.
Syntax of goto
// Driver code
int main()
{
int i = 0;
// do_while loop
i = 1;
do {
printf("%d ", i++);
} while (i <= 10);
printf("\n");
// goto statement
i = 1;
any_label:
printf("%d ", i++);
if (i <= 10) {
goto any_label;
}
return 0;
}
Output
1 2 3 4 6 8 9 10
1 2 3 4 5 6
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
Arrays
Syntax of Arrays
Example of Arrays
// C Program to demonstrate the use of array
#include <stdio.h>
int main()
{
// array declaration and initialization
int arr[5] = { 10, 20, 30, 40, 50 };
return 0;
}
Output
Strings
Example of Strings
#include <stdio.h>
#include <string.h>
int main()
{
// declare and initialize string
char str[] = "Geeks";
// print string
printf("%s\n", str);
int length = 0;
length = strlen(str);
return 0;
}
Output
Geeks
Length of string str is 5
C String Functions
S. No.
Function
Description
1.
strlen() Find the length of the string
2.
strcmp() Compares two strings.
3.
strcpy() Copy one string to another.
4.
strcat() Concatenate one string with another.
5.
strchr() Find the given character in the string.
6.
strstr() Find the given substring in the string.
Pointers
Pointers are the variables that store the address of another variable. They can
point to any data type in C
Syntax of Pointers
data_type * ptr_name;
Note: The addressof (&) operator is used to get the address of a variable.
We can dereference (access the value pointed by the pointer) using the same *
operator.
Example of Pointers
// Driver program
int main()
{
int var = 10;
Double Pointers
Function Pointers
Structure Pointers
NULL Pointers
Dangling Pointers
Wild Pointers
Functions
Functions are the block of statements enclosed within { } braces that perform some
specific task. They provide code reusability and modularity to the program.
where,
Return Type: It is the type of optional value returned by the function. Only
one value can be returned.
Parameters: It is the data passed to the function by the caller.
2. Function Definition
3. Function Call
Calls the function by providing arguments. A function call must always be after
either function definition or function prototype.
function_name (arguments);
Example of Function
// C program to show function
// call and definition
#include <stdio.h>
// Driver code
int main()
{
// Calling sum function and
// storing its value in add variable
int add = sum(10, 30);
Sum is: 40
Type of Function
Library Functions
User-Defined Functions
Dynamic memory management allows the programmer to allocate the memory at the
program’s runtime. The C language provides four <stdlib.h> functions for dynamic
memory management which are malloc(), calloc(), realloc() and free().
1. malloc()
The malloc() function allocates the block of a specific size in the memory. It
returns the void pointer to the memory block. If the allocation is failed, it
returns the null pointer.
Syntax
2. calloc()
The calloc() function allocates the number of blocks of the specified size in the
memory. It returns the void pointer to the memory block. If the allocation is
failed, it returns the null pointer.
Syntax
3. realloc()
The realloc() function is used to change the size of the already allocated memory.
It also returns the void pointer to the allocated memory.
Syntax
4. free()
Syntax
free (ptr);
int main()
{
// using malloc to allocate the int array of size 10
int* ptr = (int*)malloc(sizeof(int) * 10);
return 0;
}
Output
Structures
A structure is a user-defined data type that can contain items of different types
as its members. In C, struct keyword is used to declare structures and we can use (
. ) dot operator to access structure members.
Structure Template
struct struct_name {
member_type1 name1;
member_type1 name1;
.
.
};
...{
...structure template...
}var1, var2..., varN;
or
Example of Structure
// Driver code
int main()
{
// variable declaration after structure template
// initialization with initializer list and designated
// initializer list
struct str1 var1 = { 1, 'A', 1.00, "GeeksforGeeks" },
var2;
struct str2 var3 = { .ff = 5.00, .ii = 5, .cc = 'a' };
return 0;
}
Output
Struct 1:
i = 1, c = A, f = 1.000000, s = GeeksforGeeks
Struct 2:
i = 1, c = A, f = 1.000000, s = GeeksforGeeks
Struct 3
i = 5, c = a, f = 5.000000
Union
A union is also a user-defined data type that can contain elements of different
types. However, unlike structure, a union stores its members in a shared memory
location rather than having separate memory for each member.
Syntax of Union
union union_name {
// members
.
.
}
Union members can be accessed using dot operator ( . ) but only one member can
store the data at a particular instance in time.
Example of Union
// driver code
int main()
{
return 0;
}
Output
Enumeration, also known as enum is a user-defined data type that is used to assign
some name to the integral constant. By default, the enum members are assigned
values starting from 0 but we can also assign values manually.
Syntax of enum
Example of enum
int main()
{
enum week day;
day = Wed;
printf("%d", day);
return 0;
}
Output
File Handling
File handling is the process of performing input and output on a file instead of
the console. We can store, retrieve, and update data in a file. C supports text and
binary files.
C File Operations
We can perform some set of operations on a file and C language provide some
functions for it.
Creating a new file – fopen() with attributes as “a” or “a+” or “w” or “w+”
Opening an existing file – fopen()
Reading from file – fscanf() or fgets()
Writing to a file – fprintf() or fputs()
Moving to a specific location in a file – fseek(), rewind()
Closing a file – fclose()
Preprocessor Directives
Description
1.
#define
Used to define a macro
2.
#undef
Used to undefine a macro
3.
#include
Used to include a file in the source code program
4.
#ifdef
Used to include a section of code if a certain macro is defined by #define
5.
#endif
Used to mark the end of #endif
6.
#ifndef
Used to include a section of code if a certain macro is not defined by
#define
7.
#if
Check for the specified condition
8.
#else
Alternate code that executes when #if fails
9.
#pragma
This directive is a special purpose directive and is used to turn on or off
some features.
Common Library Functions
C languages come bundled with some Standard Libraries that contain some useful
functions to make it easier to perform some common operations. These are as
follows:
C Math Functions
The <math.h> header file contains functions to perform the arithmetic operations.
The following table contains some common maths functions in C:
S.No.
Function Name
Function Description
1.
ceil(x)
Returns the largest integer smaller than or equal to x.
2.
floor(x)
Returns the smallest integer larger than or equal to x.
3.
fabs(x)
Returns the absolute value of x.
4.
sqrt(x)
Returns the square root of x.
5.
cbrt(x)
Returns the cube root of x.
6.
pow(x , y)
Returns the value of x raised to the power y.
7.
exp(x)
Returns the value of e(Euler’s Number) raised to the power x.
8.
fmod(x , y)
Returns the remainder of x divided by y.
9.
log(x)
Returns the natural logarithm of x.
10.
log10(x)
Returns the common logarithm of x.
11.
cos(x)
Returns the cosine of radian angle x.
12.
sin(x)
Returns the sine of radian angle x.
13.
tan(x)
Returns the tangent of radian angle x.
==========================================
#include <stdio.h>
void clean_stdin(void)
{
int c;
do {
c = getchar();
} while (c != '\n' && c != EOF);
}
===================================
Example
------------
This example deletes a stream buffer.
#include <stdio.h>
int main(void)
{
FILE *stream;
int ch;
unsigned int result = 0;
==================================================
#include <stdio.h>
int main(void)
{
FILE* f = fopen("input.txt", "w+");
if (f != NULL)
{
fputs("123x", f);
rewind(f);
demo_scanf("%u%c", f);
fclose(f);
}
return 0;
}