0% found this document useful (0 votes)
4 views14 pages

Cprogramming

jhg

Uploaded by

thailandjunior98
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views14 pages

Cprogramming

jhg

Uploaded by

thailandjunior98
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

lOMoARcPSD|59466988

C Program Structure

Computer Book for Beginners (Bamenda University of Science & Technology)

Scan to open on Studocu

Studocu is not sponsored or endorsed by any college or university


Downloaded by Thailand Junior ([email protected])
lOMoARcPSD|59466988

SWE235 : Programming II . Notes on C Programming

C Program Structure

A C program basically consists of the following parts:


➢ Preprocessor Commands
➢ Functions
➢ Variables
➢ Statements & Expressions
➢ Comments

Semicolons ;
In 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.

For example, following are two different statements:


printf("Hello, World! \n");
return 0;

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 or 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. Here are
some examples of acceptable identifiers:

Keywords
The following list shows the reserved words in C. These reserved words may not be used as
constant or variable or any other identifier names.

C Data Types
In the C programming language, data types 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.

CopyRight @ Eugene Tebo January 2023


1
Downloaded by Thailand Junior ([email protected])
lOMoARcPSD|59466988

SWE235 : Programming II . Notes on C Programming

The types in C can be classified as follows:

C 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.
Based on the basic types explained in previous chapter, there will be the following basic variable
types:

Variable Definition in C:
A variable definition means to tell the compiler where and how much to create the storage 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;

Here, type must be a valid C data type including char, w_char, int, float, double, bool or any user
defined object, etc., and variable_list may consist of one or more identifier names separated by
commas. Some valid declarations are shown here:

CopyRight @ Eugene Tebo January 2023


2
Downloaded by Thailand Junior ([email protected])
lOMoARcPSD|59466988

SWE235 : Programming II . Notes on C Programming

The line int i, j, k; both declares and defines the variables i, j and k; which instructs the compiler
to create variables named i, j and k of type int.

Variables can be initialized (assigned an initial value) in their declaration. The initializer consists of
an equal sign followed by a constant expression as follows:

Some examples are:

Defining Constants
There are two simple ways in C to define constants:
1. Using #define preprocessor.
2. Using const keyword.

The #define Preprocessor

Following is the form to use #define preprocessor to define a constant:

Following example explains it in detail:

When the above code is compiled and executed, it produces the following result:

The const Keyword

CopyRight @ Eugene Tebo January 2023


3
Downloaded by Thailand Junior ([email protected])
lOMoARcPSD|59466988

SWE235 : Programming II . Notes on C Programming

You can use const prefix to declare constants with a specific type as follows:

Following example explains it in detail:

Note that it is a good programming practice to define constants in CAPITALS

C Operators
An operator is a symbol that tells the compiler to perform specific mathematical or logical
manipulations. 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

We will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by
one.

Arithmetic Operators
Following table shows all the arithmetic operators supported by C language. Assume
variable A holds 10 and variable B holds 20, then:

Try the following example to understand all the arithmetic operators available in C

CopyRight @ Eugene Tebo January 2023


4
Downloaded by Thailand Junior ([email protected])
lOMoARcPSD|59466988

SWE235 : Programming II . Notes on C Programming

programming language:

When you compile and execute the above program, it produces the following result:

Relational Operators
Following table shows all the relational operators supported by C language. Assume variable A
holds 10 and variable B holds 20, then:

CopyRight @ Eugene Tebo January 2023


5
Downloaded by Thailand Junior ([email protected])
lOMoARcPSD|59466988

SWE235 : Programming II . Notes on C Programming

Logical Operators
Following table shows all the logical operators supported by C language. Assume variable A holds 1
and variable B holds 0, then:

Try the following example to understand all the logical operators available in C
programming language:

CopyRight @ Eugene Tebo January 2023


6
Downloaded by Thailand Junior ([email protected])
lOMoARcPSD|59466988

SWE235 : Programming II . Notes on C Programming

When you compile and execute the above program, it produces the following result:

Assignment Operators
There are following assignment operators supported by C language:

CopyRight @ Eugene Tebo January 2023


7
Downloaded by Thailand Junior ([email protected])
lOMoARcPSD|59466988

SWE235 : Programming II . Notes on C Programming

Try the following example to understand all the assignment operators available in C
programming language:

CopyRight @ Eugene Tebo January 2023


8
Downloaded by Thailand Junior ([email protected])
lOMoARcPSD|59466988

SWE235 : Programming II . Notes on C Programming

When you compile and execute the above program, it produces the following result:

Decision Making in C
Decision making structures require that the programmer specify one or more conditions to be
evaluated or tested by the program, along with a statement or statements

CopyRight @ Eugene Tebo January 2023


9
Downloaded by Thailand Junior ([email protected])
lOMoARcPSD|59466988

SWE235 : Programming II . Notes on C Programming

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.

Following is the general form of a typical decision making structure found in most of the
programming languages:

C programming language assumes any non-zero and non-null values as true, and if it is
either zero or null, then it is assumed as false value. C programming language provides
following types of decision making statements.

if statement
An if statement consists of a boolean expression followed by one or more statements.

Syntax
The syntax of an if statement in C programming language is:

if...else statement
An if statement can be followed by an optional else statement, which executes when the
boolean expression is false.
Syntax
The syntax of an if...else statement in C programming language is:

CopyRight @ Eugene Tebo January 2023


10
Downloaded by Thailand Junior ([email protected])
lOMoARcPSD|59466988

SWE235 : Programming II . Notes on C Programming

The 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.

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)

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.

C Loops
There may be a situation, when you need to execute a block of code several number 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


and following is the general form of a loop statement in most of the programming
languages

C programming language provides the following types of loops to handle looping


requirements.

CopyRight @ Eugene Tebo January 2023


11
Downloaded by Thailand Junior ([email protected])
lOMoARcPSD|59466988

SWE235 : Programming II . Notes on C Programming

while loop in C
A while loop statement in C programming language repeatedly executes a target
statement as long as a given condition is true.
Syntax
The syntax of a while loop in C programming language is:

for loop in C
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.
Syntax
The syntax of a for loop in C programming language is:

do...while loop in C
Unlike for and while loops, which test the loop condition at the top of the loop,
the do...while loop in C programming language checks its condition at the bottom of the
loop.
A do...while loop is similar to a while loop, except that a do...while loop is guaranteed to
execute at least one time.

C Functions
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 usually is so 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.

The C standard library provides numerous built-in functions that your program can call. For
example,
• function strcat() to concatenate two strings,
• function memcpy() to copy one memory location to another location
• ….and many more functions.

A function is known with various names like a method or a sub-routine or a procedure, etc.

CopyRight @ Eugene Tebo January 2023


12
Downloaded by Thailand Junior ([email protected])
lOMoARcPSD|59466988

SWE235 : Programming II . Notes on C Programming

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
}

A function definition in C programming language consists of a function header and


a function body. Here are all the parts of a function:

➢ 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.

Example
Following is the source code for a function called max(). This function takes two parameters
num1 and num2 and returns the maximum between the two:

CopyRight @ Eugene Tebo January 2023


13
Downloaded by Thailand Junior ([email protected])

You might also like