0% found this document useful (0 votes)
49 views11 pages

C Programing

C is a general-purpose programming language developed in the early 1970s at Bell Labs. Many important ideas of C stem from the earlier language BCPL. Key developments include Dennis Ritchie writing C at Bell Labs in 1972, and the publication of The C Programming Language book in 1978 which caused C to revolutionize the computing world. The ANSI standard for C was completed in 1988 to provide a modern, comprehensive definition. C has various built-in data types like int, float, and char. Variables store data in memory locations and have unique identifiers. C provides many operators for arithmetic, relational, logical, bitwise, and assignment operations. Control flow is implemented through branching with if/else statements and looping

Uploaded by

OFFICIAL
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
49 views11 pages

C Programing

C is a general-purpose programming language developed in the early 1970s at Bell Labs. Many important ideas of C stem from the earlier language BCPL. Key developments include Dennis Ritchie writing C at Bell Labs in 1972, and the publication of The C Programming Language book in 1978 which caused C to revolutionize the computing world. The ANSI standard for C was completed in 1988 to provide a modern, comprehensive definition. C has various built-in data types like int, float, and char. Variables store data in memory locations and have unique identifiers. C provides many operators for arithmetic, relational, logical, bitwise, and assignment operations. Control flow is implemented through branching with if/else statements and looping

Uploaded by

OFFICIAL
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 11

Brief History of C

C is a general-purpose language which has been closely associated with the UNIX operating
system for which it was developed - since the system and most of the programs that run it are
written in C.

Many of the important ideas of C stem from the language BCPL, developed by Martin Richards.
The influence of BCPL on C proceeded indirectly through the language B, which was written by
Ken Thompson in 1970 at Bell Labs, for the first UNIX system on a DEC PDP-7. BCPL and B
are "type less" languages whereas C provides a variety of data types.

In 1972 Dennis Ritchie at Bell Labs writes C and in 1978 the publication of The C Programming
Language by Kernighan & Ritchie caused a revolution in the computing world.

In 1983, the American National Standards Institute (ANSI) established a committee to provide a
modern, comprehensive definition of C. The resulting definition, the ANSI standard, or "ANSI
C", was completed late 1988.

Data type:
C has a concept of 'data types' which are used to define a variable before its use. The definition
of a variable will assign storage for the variable and define the type of data that will be held in
the location.

The value of a variable can be changed any time.

C has the following basic built-in datatypes.

 int
 float
 double
 char

Variables

Variables are memory location in computer's memory to store data. To indicate the memory
location, each variable should be given a unique name called identifier. Variable names are just
the symbolic representation of a memory location. Examples of variable name: sum, car_no,
count etc.

Escape Sequences

Sometimes, it is necessary to use newline(enter), tab, quotation mark etc. in the program which
either cannot be typed or has special meaning in C programming. In such cases, escape sequence
are used. For example: \n is used for newline. The backslash( \ ) causes "escape" from the
normal way the characters are interpreted by the compiler.

C - Operators
An operator is a symbol that tells the compiler to perform specific mathematical or logical
functions. C language is rich in built-in operators and provides the following types of operators −

 Arithmetic Operators
 Relational Operators
 Logical Operators
 Bitwise Operators
 Assignment Operators
 Misc Operators

Control Statements
C provides two sytles of flow control:

 Branching
 Looping

Branching is deciding what actions to take and looping is deciding how many times to take a
certain action.
Branching:
Branching is so called because the program chooses to follow one branch or another.

if statement

This is the simplest form of the branching statements.

It takes an expression in parenthesis and an statement or block of statements. if the expression is


true then the statement or block of statements gets executed otherwise these statements are
skipped.

NOTE: Expression will be assumed to be true if its evaulated values is non-zero.

if statements take the following form:

Show Example

if (expression)
statement;

or

if (expression)
{
Block of statements;
}

or

if (expression)
{
Block of statements;
}
else
{
Block of statements;
}

or

if (expression)
{
Block of statements;
}
else if(expression)
{
Block of statements;
}
else
{
Block of statements;
}

? : Operator

The? : Operator is just like an if ... else statement except that because it is an operator you can
use it within expressions.

? : is a ternary operator in that it takes three values, this is the only ternary operator C has.

? : takes the following form:

Switch statement:

The switch statement is much like a nested if .. else statement. Its mostly a matter of preference
which you use, switch statement can be slightly more efficient and easier to read.

switch( expression )
{
case constant-expression1: statements1;
[case constant-expression2: statements2;]
[case constant-expression3: statements3;]
[default : statements4;]
}

Using break keyword:

If a condition is met in switch case then execution continues on into the next case clause also if it
is not explicitly specified that the execution should exit the switch statement. This is achieved by
using break keyword.

Looping
Loops provide a way to repeat commands and control how many times they are repeated. C
provides a number of looping way.

While loop

The most basic loop in C is the while loop. A while statement is like a repeating if statement.
Like an If statement, if the test condition is true: the statements get executed. The difference is
that after the statements have been executed, the test condition is checked again. If it is still true
the statements get executed again. This cycle repeats until the test condition evaluates to false.
while ( expression )
{
Single statement
or
Block of statements;
}

For loop

For loop is similar to while, it's just written differently. for statements are often used to proccess
lists such a range of numbers:

for( expression1; expression2; expression3)


{
Single statement
or
Block of statements;
}

In the above syntax:

 expression1 - Initialisese variables.


 expression2 - Condtional expression, as long as this condition is true, loop will keep executing.
 expression3 - expression3 is the modifier which may be simple increment of a variable.

do...while loop

do ... while is just like a while loop except that the test condition is checked at the end of the
loop rather than the start. This has the effect that the content of the loop are always executed at
least once.

Basic syntax of do...while loop is as follows:

do
{
Single statement
or
Block of statements;
}while(expression);

break and continue statements


C provides two commands to control how we loop:
 break -- exit form loop or switch.
 continue -- skip 1 iteration of loop.

You already have seen example of using break statement. Here is an example showing usage of
continue statement.

#include

main()
{
int i;
int j = 10;

for( i = 0; i <= j; i ++ )
{
if( i == 5 )
{
continue;
}
printf("Hello %d\n", i );
}
}

Arrays
Arrays a kind of data structure that can store a fixed-size sequential collection of elements of the
same type. An array is used to store a collection of data, but it is often more useful to think of an
array as a collection of variables of the same type.

Initializing Arrays
double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0};
#include <stdio.h>

int main () {

int n[ 10 ]; /* n is an array of 10 integers */


int i,j;

/* initialize elements of array n to 0 */


for ( i = 0; i < 10; i++ ) {
n[ i ] = i + 100; /* set element at location i to i + 100 */
}

/* output each array element's value */


for (j = 0; j < 10; j++ ) {
printf("Element[%d] = %d\n", j, n[j] );
}

return 0;
}
Multi-dimensional arrays
C supports multidimensional arrays. The simplest form of the multidimensional array is the two-
dimensional array.

Initializing Two-Dimensional Arrays

Multidimensional arrays may be initialized by specifying bracketed values for each row.
Following is an array with 3 rows and each row has 4 columns.

int a[3][4] = {
{0, 1, 2, 3} , /* initializers for row indexed by 0 */
{4, 5, 6, 7} , /* initializers for row indexed by 1 */
{8, 9, 10, 11} /* initializers for row indexed by 2 */
};
#include <stdio.h>

int main () {

/* an array with 5 rows and 2 columns*/


int a[5][2] = { {0,0}, {1,2}, {2,4}, {3,6},{4,8}};
int i, j;

/* output each array element's value */


for ( i = 0; i < 5; i++ ) {

for ( j = 0; j < 2; j++ ) {


printf("a[%d][%d] = %d\n", i,j, a[i][j] );
}
}

return 0;

C - Strings
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."

char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};


S.N. Function & Purpose 1

strcpy(s1, s2);

Copies string s2 into string s1.

2
strcat(s1, s2);

Concatenates string s2 onto the end of string s1.

strlen(s1);

Returns the length of string s1.

strcmp(s1, s2);

Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2.

strchr(s1, ch);

Returns a pointer to the first occurrence of character ch in string s1.

strstr(s1, s2);

Returns a pointer to the first occurrence of string s2 in string s1.

#include <stdio.h>
#include <string.h>

int main () {

char str1[12] = "Hello";


char str2[12] = "World";
char str3[12];
int len ;

/* copy str1 into str3 */


strcpy(str3, str1);
printf("strcpy( str3, str1) : %s\n", str3 );

/* concatenates str1 and str2 */


strcat( str1, str2);
printf("strcat( str1, str2): %s\n", str1 );

/* total lenghth of str1 after concatenation */


len = strlen(str1);
printf("strlen(str1) : %d\n", len );
return 0;
}

C - Functions
A function is a group of statements that together perform a task. Every C program has at least
one function, which is main(), and all the most trivial programs can define additional functions.

You can divide up your code into separate functions. How you divide up your code among
different functions is up to you, but logically the division is such that each function performs a
specific task.

 Return Type − A function may return a value. The return_type is the data type of the
value the function returns. Some functions perform the desired operations without
returning a value. In this case, the return_type is the keyword void.
 Function Name − This is the actual name of the function. The function name and the
parameter list together constitute the function signature.
 Parameters − A parameter is like a placeholder. When a function is invoked, you pass a
value to the parameter. This value is referred to as actual parameter or argument. The
parameter list refers to the type, order, and number of the parameters of a function.
Parameters are optional; that is, a function may contain no parameters.
 Function Body − The function body contains a collection of statements that define what
the function does.

#include <stdio.h>

/* function declaration */
int max(int num1, int num2);

int main () {

/* local variable definition */


int a = 100;
int b = 200;
int ret;

/* calling a function to get max value */


ret = max(a, b);

printf( "Max value is : %d\n", ret );

return 0;
}

/* function returning the max between two numbers */


int max(int num1, int num2) {

/* local variable declaration */


int result;
if (num1 > num2)
result = num1;
else
result = num2;

return result;
}

C - Structures
Arrays allow to define type of variables that can hold several data items of the same kind. Similarly
structure is another user defined data type available in C that allows to combine data items of different
kinds.

Defining a Structure

To define a structure, you must use the struct statement. The struct statement defines a new data
type, with more than one member. The format of the struct statement is as follows −

struct [structure tag] {

member definition;
member definition;
...
member definition;
} [one or more structure variables];

Example:
#include <stdio.h>
#include <string.h>

struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
};

int main( ) {

struct Books Book1; /* Declare Book1 of type Book */


struct Books Book2; /* Declare Book2 of type Book */

/* book 1 specification */
strcpy( Book1.title, "C Programming");
strcpy( Book1.author, "Nuha Ali");
strcpy( Book1.subject, "C Programming Tutorial");
Book1.book_id = 6495407;
/* book 2 specification */
strcpy( Book2.title, "Telecom Billing");
strcpy( Book2.author, "Zara Ali");
strcpy( Book2.subject, "Telecom Billing Tutorial");
Book2.book_id = 6495700;

/* print Book1 info */


printf( "Book 1 title : %s\n", Book1.title);
printf( "Book 1 author : %s\n", Book1.author);
printf( "Book 1 subject : %s\n", Book1.subject);
printf( "Book 1 book_id : %d\n", Book1.book_id);

/* print Book2 info */


printf( "Book 2 title : %s\n", Book2.title);
printf( "Book 2 author : %s\n", Book2.author);
printf( "Book 2 subject : %s\n", Book2.subject);
printf( "Book 2 book_id : %d\n", Book2.book_id);

return 0;
}

You might also like