C programming
C programming
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.
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
*/
}
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);
}
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;
Defining a 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.