Programming in C (1)
Programming in C (1)
•The next line int main() is the main function where the
program execution begins.
1 Basic Types
auto else long switch They are arithmetic types and are further classified
into: (a) integer types and (b) floating-point types.
break enum register typedef
2 Enumerated types
case extern return union
They are again arithmetic types and they are used
char float short unsigned to define variables that can only assign certain
discrete integer values throughout the program.
const for signed void
continue goto sizeof volatile 3 The type void
The type specifier void indicates that no value is
default if static while available.
do int struct _Packed 4 Derived types
They include (a) Pointer types, (b) Array types, (c)
double Structure types, (d) Union types and (e) Function
types.
The following list shows the reserved words in C.
These reserved words may not be used as constants Data types in c refer to an extensive system used for
or variables or any other identifier names. declaring variables or functions of different types. The type of
a variable determines how much space it occupies in storage
and how the bit pattern stored is interpreted.
Char and Integer type Floating point type
Type Storage Value range
size
Type Storage Value Precision
size range
char 1 byte -128 to 127 or 0 to 255
unsigned 1 byte 0 to 255 float 4 byte 1.2E-38 to 6 decimal
char 3.4E+38 places
signed char 1 byte -128 to 127 double 8 byte 2.3E-308 to 15 decimal
1.7E+308 places
-32,768 to 32,767 or -
2 or 4 long double 10 byte 3.4E-4932 to 19 decimal
int 2,147,483,648 to
bytes 1.1E+4932 places
2,147,483,647
unsigned 2 or 4 0 to 65,535 or 0 to
int bytes 4,294,967,295
What are your findings?
short 2 bytes -32,768 to 32,767
unsigned 2 bytes 0 to 65,535
short
long 8 bytes -9223372036854775808 to
9223372036854775807
unsigned 8 bytes 0 to 18446744073709551615
long
Variable type
Sr.No. Type & Description
1 char
Typically a single octet(one byte). This A variable is nothing but a name given to a storage area
is an integer type. that our programs can manipulate. Each variable in C has
a specific type, which determines the size and layout of
2 int
the variable's memory; the range of values that can be
The most natural size of integer for the
stored within that memory; and the set of operations that
machine.
can be applied to the variable.
3 float
A single-precision floating point value. The name of a variable can be composed of letters, digits,
and the underscore character. It must begin with either a
4 double letter or an underscore. Upper and lowercase letters are
A double-precision floating point value.
distinct because C is case-sensitive. Based on the basic
5 void types explained in the previous chapter, there will be the
Represents the absence of type. following basic variable types −
String
Strings are actually one-dimensional array of
characters terminated by a null character '\0'.
Thus a null-terminated string contains the
characters that comprise the string followed by
a null.
The following declaration and initialization
create a string consisting of the word "Hello". To
hold the null character at the end of the array,
the size of the character array containing the
string is one more than the number of
characters in the word "Hello.“
1 strcpy(s1, s2);
Copies string s2 into string s1.
2 strcat(s1, s2);
Concatenates string s2 onto the end of
string s1.
3 strlen(s1);
Returns the length of string s1.
4 strcmp(s1, s2);
Returns 0 if s1 and s2 are the same; less
than 0 if s1<s2; greater than 0 if s1>s2.
5 strchr(s1, ch);
Returns a pointer to the first occurrence of
character ch in string s1.
6 strstr(s1, s2);
Returns a pointer to the first occurrence of
string s2 in string s1.
Output =
String input
You can use the scanf() function to read a string. You can use gets() function to read a line of
The scanf() function reads the sequence of characters string. And, you can use puts() to display the
until it encounters a whitespace (space, newline, tab string.
etc.). Even though Dennis Ritchie was entered in the
above program, only "Ritchie" was stored in the name
string. It's because there was a space after Ritche
String input
VS
Can you
follow the
lesson so
far?
Nested if
4 nested loops You can use one or more 3 goto statement Transfers
loops inside any other while, for, or control to the labeled
do..while loop. statement.
How for loop works?
When you need to execute a block of code several number of
times then you need to use looping concept in C language. In C
Programming Language for loop is a statement which allows
code to be repeatedly executed. It contains 3 parts.
The code block (loop body) inside the braces is executed once.
Then, the test expression is evaluated. If the test expression is true, the loop body is
executed again. This process goes on until the test expression is evaluated to 0
(false). When the test expression is false (nonzero), the do…while loop is terminated.
Example
1.First, we have initialized a variable 'num' with
value 1. Then we have written a do-while loop.
It is sometimes
desirable to skip some
statements inside the
loop or terminate the
loop immediately
without checking the
test expression.
Function How to define a function?
A function is a group of statements that A function definition in C programming consists of a function
together perform a task. Every C program header and a function body. Here are all the parts of a function −
has at least one function, which is main(),
•Return Type − A function may return a value. The return_type is
and all the most trivial programs can define
the data type of the value the function returns. Some functions
additional functions. perform the desired operations without returning a value. In this
case, the return_type is the keyword void.
You can divide up your code into separate
functions. How you divide up your code •Function Name − This is the actual name of the function. The
among different functions is up to you, but function name and the parameter list together constitute the
logically the division is such that each function signature.
function performs a specific task.
•Parameter − A parameter is like a placeholder. When a function
is invoked, you pass a value to the parameter. This value is
A function declaration tells the compiler referred to as actual parameter or argument. The parameter list
about a function's name, return type, and refers to the type, order, and number of the parameters of a
parameters. A function definition provides function. Parameters are optional; that is, a function may contain
the actual body of the function. no parameters.
Function definition
Function definition contains the block of code to
perform a specific task i.e. in this case, adding
two numbers and returning it.
Passing argument to function Return value
Local variable
Static variable
A function's arguments are used to receive the
necessary values by the function call. They are
matched by position; the first argument is passed to
the first parameter, the second to the second
parameter and so on.
Pass by value
Pass by value
Pass by reference
#include <stdio.h>
void swap(int *x, int *y);
int main () {
int a = 100;
int b = 200;
swap(&a, &b);
return 0; }
#include<stdio.h>
int main() printf("\nData after sorting: ");
{
int i,a[10],temp,j; for(j=0;j<10;j++)
{ printf(" %d", a[j]); }
printf("Enter any 10 num in array: \n"); return 0;
}
for(i=0;i<10;i++)
{ scanf("%d",&a[i]);
}
printf("\nData before sorting: ");
for(j=0;j<10;j++)
{ printf(" %d",a[j]); }
for(i=0;i<10-1;i++)
{ for(j=0;j<10-i-1;j++)
{ if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp; } } }
Sorting of Array (bubble sort)
#include<stdio.h> if (position!=c)
{
int main() swap=array[c];
{ int array[100], n, c, d, position, swap; array[c]=array[position];
array[position]=swap; } }
printf("How many elements you want to
enter: "); printf("Sorted list in ascending
scanf("%d", &n); order:\n");
printf("Enter any %d elements: \n", n);
for (c=0; c<n; c++)
for (c=0; c<n; c++) { printf("%d ", array[c]); }
{ scanf("%d", &array[c]); } return 0;
}
for (c=0; c<(n-1); c++)
{ position=c;
for (d=c+1; d<n; d++)
{ if (array[position]>array[d])
{ position=d; } }
Sorting with Array (selection sort)
#include<stdio.h>
int main() printf("After Sorting elements: ");
{ int i, j, num, temp, arr[20]; for(i=0; i<num; i++)
{ printf("%d ", arr[i]); }
printf("Enter size of array: "); return 0;
scanf("%d", &num); }
for(i=0;i<p;++i) return 0;
for(j=0;j<q;++j) }
scanf("%d",&b[ i ][ j ]);
Examples:
1.int table[5][5][20];
2.float arr[5][6][5][6][5];
Multi-Dimensional Array
arr[0][1][2] = 32;
arr[1][0][1] = 49;
Multi-Dimensional Array