0% found this document useful (0 votes)
15 views16 pages

C programming

C Programming
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
15 views16 pages

C programming

C Programming
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 16

LEARN C PROGRAMMING

C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to


develop the UNIX operating system at Bell Labs. C was originally first implemented on the DEC
PDP-11 computer in 1972. In 1978, Brian Kernighan and Dennis Ritchie produced the first
publicly available description of C, now known as the K&R standard. The UNIX operating system,
the C compiler, and essentially all UNIX application programs have been written in C. C has now
become a widely used professional language for various reasons:
• Easy to learn
• Structured language
• It produces efficient programs
• It can handle low-level activities
• It can be compiled on a variety of computer platforms

Semicolons
In a C program, the semicolon is a statement terminator. That is, each individual statement must
be ended with a semicolon. It indicates the end of one logical entity.

Comments
Comments are like helping text in your C program and they are ignored by the compiler. They
start with /* and terminate with the characters */ as shown below:
/* my first program in C */

Identifiers
A C identifier is a name used to identify a variable, function, or any other user defined item. An
identifier starts with a letter A to Z, a to z, or an underscore ‘_’ followed by zero or more letters,
underscores, and digits (0 to 9).C does not allow punctuation characters such as @, $, and % within
identifiers. C is a case-sensitive programming language. Thus, Manpower and manpower are two
different identifiers in C.
Keywords
The following list shows the reserved words in C. These reserved words may not be used as
constants or variables or any other identifier names.

Whitespace in C
A line containing only whitespace, possibly with a comment, is known as a blank line, and a C
compiler totally ignores it.

DATA TYPES
Data types in C refer to an extensive system used for 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.

Integer Types
The following table provides the details of standard integer types with their storage sizes and value
ranges:
Floating-Point Types
The following table provides the details of standard floating-point types with storage sizes and
value ranges and their precision:

VARIABLES
A variable is nothing but a name given to a storage area that our programs can manipulate. Each
variable in C has a specific type, which determines the size and layout of the variable's memory;
the range of values that can be stored within that memory; and the set of operations that can be
applied to the variable. The name of a variable can be composed of letters, digits, and the
underscore character. It must begin with either a letter or an underscore. Upper and lowercase
letters are distinct because C is case-sensitive.

Variable Definition in C
A variable definition tells the compiler where and how much storage to create for the variable. A
variable definition specifies a data type and contains a list of one or more variables of that type as
follows:
type variable_list;
Example: int i, j, k;
char c, ch;
float f, salary;
double d;

Variable Declaration in C
A variable declaration provides assurance to the compiler that there exists a variable with the given
type and name so that the compiler can proceed for further compilation without requiring the
complete detail about the variable. A variable declaration has its meaning at the time of
compilation only, the compiler needs actual variable declaration at the time of linking the program.
A variable declaration is useful when you are using multiple files and you define.

CONSTANTS AND LITERALS


Constants refer to fixed values that the program may not alter during its execution. These fixed
values are also called literals. Constants can be of any of the basic data types like an integer
constant, a floating constant, a character constant, or a string literal. There are enumeration
constants as well. Constants are treated just like regular variables except that their values cannot
be modified after their definition.
#define identifier value
const type variable = value;

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

Operators Precedence in C
Operator precedence determines the grouping of terms in an expression and decides how an
expression is evaluated. Certain operators have higher precedence than others; for example, the
multiplication operator has a higher precedence than the addition operator. For example, x = 7 + 3
* 2; here, x is assigned 13, not 20 because operator * has a higher precedence than +, so it first
gets multiplied with 3*2 and then adds into 7.
DECISION MAKING

Decision-making structures require that the programmer specifies one or more conditions to be
evaluated or tested by the program, along with a statement or statements to be executed if the
condition is determined to be true, and optionally, other statements to be executed if the condition
is determined to be false.

if Statement
An if statement consists of a Boolean expression followed by one or more statements

if(boolean_expression)
{
/* statement(s) will execute if the boolean
expression is true */
}
if…else Statement
An if statement can be followed by an optional else statement, which executes when the Boolean
expression is false.
if(boolean_expression)
{
/* statement(s) will execute if the boolean expression is true */
}
else
{
/* statement(s) will execute if the boolean expression is false
*/
}

if...else if...else Statement


An if statement can be followed by an optional else if...else statement, which is very useful to test
various conditions using single if...else if statement
• An if can have zero or one else's and it must come after any else if's.
• An if can have zero to many else if's and they must come before the else.
if(boolean_expression 1)
{
/* Executes when the boolean expression 1 is true */
}
else if( boolean_expression 2)
{
/* Executes when the boolean expression 2 is true */
}
else if( boolean_expression 3)
{
/* Executes when the boolean expression 3 is true */
}
else
{
/* executes when the none of the above condition is true */
}
Nested if Statements
It is always legal in C programming to nest if-else statements, which means you can use one if or
else if statement inside another if or else if statement(s).
if( boolean_expression 1)
{
/* Executes when the boolean expression 1 is true */
if(boolean_expression 2)
{
/* Executes when the boolean expression 2 is true */
}
}

switch Statement
A switch statement allows a variable to be tested for equality against a list of values. Each value
is called a case, and the variable being switched on is checked for each switch case.
switch(expression){
case constant-expression :
statement(s);
break; /* optional */
case constant-expression :
statement(s);
break; /* optional */
/* you can have any number of case statements */
default : /* Optional */
statement(s);
}

The? : Operator:
We have covered conditional operator? in the previous chapter which can be used to replace
if...else statements. It has the following general form:
Exp1? Exp2: Exp3;
Where Exp1, Exp2, and Exp3 are expressions. Notice the use and placement of the colon.
The value of a? expression is determined like this:
• Exp1 is evaluated. If it is true, then Exp2 is evaluated and becomes the value of the entire?
expression.
• If Exp1 is false, then Exp3 is evaluated and its value becomes the value of the expression
LOOPS
You may encounter situations when a block of code needs to be executed several numbers of times.
In general, statements are executed sequentially: The first statement in a function is executed first,
followed by the second, and so on. Programming languages provide various control structures that
allow for more complicated execution paths. A loop statement allows us to execute a statement or
group of statements multiple times. Given below is the general form of a loop statement in most
of the programming languages:

while Loop
A while loop in C programming repeatedly executes a target statement as long as a given condition
is true.
while(condition)
{
statement(s);
}

for Loop
A for loop is a repetition control structure that allows you to efficiently write a loop that needs to
execute a specific number of times.
for ( init; condition; increment )
{
statement(s);
}

do…while Loop
Unlike for and while loops, which test the loop condition at the top of the loop, the do...while loop
in C programming checks its condition at the bottom of the loop. A do...while loop is similar to a
while loop, except the fact that it is guaranteed to execute at least one time.
do
{
statement(s);
}while( condition );

Nested Loops
This refers to loop inside a loop
for ( init; condition; increment )
{
for ( init; condition; increment )
{
statement(s);
}
statement(s);
}

Loop Control Statements


Loop control statements change execution from its normal sequence. When execution leaves a
scope, all automatic objects that were created in that scope are destroyed.

break Statement
The break statement in C programming has the following two usages: When a break statement is
encountered inside a loop, the loop is immediately terminated and the program control resumes at
the next
statement following the loop.
• It can be used to terminate a case in the switch statement
• If you are using nested loops, the break statement will stop the execution of the innermost
loop and start executing the next line of code after the block.
continue Statement
The continue statement in C programming works somewhat like the break statement. Instead of
forcing termination, it forces the next iteration of the loop to take place, skipping any code in
between. For the for loop, continue statement causes the conditional test and increment portions
of the loop to execute. For the while and do...while loops, continue statement causes the program
control to pass to the conditional tests.

goto Statement
A goto statement in C programming provides an unconditional jump from the ‘goto’ to a labeled
statement in the same function.

NOTE: Use of goto statement is highly discouraged in any programming language because it
makes difficult to trace the control flow of a program, making the program hard to understand and
hard to modify. Any program that uses a goto can be rewritten to avoid them.

goto label;
..
.
label: statement;

The Infinite Loop


A loop becomes an infinite loop if a condition never becomes false. The for loop is traditionally
used for this purpose. Since none of the three expressions that form the ‘for’ loop are required, you
can make an endless loop by leaving the conditional expression empty
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.
A function declaration tells the compiler about a function's name, return type, and parameters. A
function definition provides the actual body of the function.

Defining a Function

The general form of a function definition in C programming language is as follows:

return_type function_name( parameter list )


{
body of the function
}

Function Declarations
A function declaration tells the compiler about a function name and how to call the function. The
actual body of the function can be defined separately. A function declaration has the following
parts:
Syntax
return_type function_name( parameter list );

Calling a Function
While creating a C function, you give a definition of what the function has to do. To use a function,
you will have to call that function to perform the defined task. When a program calls a function,
the program control is transferred to the called function. A called function performs a defined task
and when its return
statement is executed or when its function-ending closing brace is reached, it returns the program
control back to the main program. To call a function, you simply need to pass the required
parameters along with
the function name, and if the function returns a value, then you can store the returned value.

You might also like