The document discusses the basic structure of C programs. It explains that a C program contains functions that are organized into different sections, including a documentation section, link section, definition section, main function section, and optional subprogram section. It provides details on what each section is used for and example code to demonstrate the main components of a C program, including functions, variables, data types, and memory organization.
Command-line arguments are given after the name of the program in command-line shell of Operating Systems.
To pass command line arguments, we typically define main() with two arguments : first argument is the number of command line arguments and second is list of command-line arguments.
Command-line arguments are given after the name of the program in command-line shell of Operating Systems.
To pass command line arguments, we typically define main() with two arguments : first argument is the number of command line arguments and second is list of command-line arguments.
Importance of loops in any programming language is immense, they allow us to reduce the number of lines in a code, making our code more readable and efficient.
This C tutorial covers every topic in C with the programming exercises. This is the most extensive tutorial on C you will get your hands on. I hope you will love the presentation. All the best. Happy learning.
Feedbacks are most welcome. Send your feedbacks to dwivedi.2512@gmail.com. You can download this document in PDF format from the link, http://www.slideshare.net/dwivedi2512/learning-c-an-extensive-guide-to-learn-the-c-language
Importance of loops in any programming language is immense, they allow us to reduce the number of lines in a code, making our code more readable and efficient.
This C tutorial covers every topic in C with the programming exercises. This is the most extensive tutorial on C you will get your hands on. I hope you will love the presentation. All the best. Happy learning.
Feedbacks are most welcome. Send your feedbacks to dwivedi.2512@gmail.com. You can download this document in PDF format from the link, http://www.slideshare.net/dwivedi2512/learning-c-an-extensive-guide-to-learn-the-c-language
C Preprocessor directives:
i\ Before a C program is compiled in a compiler, source code is processed by a program called preprocessor. This process is called preprocessing.
Commands used in preprocessor are called preprocessor directives and they begin with “#” symbol.
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERSveerababupersonal22
It consists of cw radar and fmcw radar ,range measurement,if amplifier and fmcw altimeterThe CW radar operates using continuous wave transmission, while the FMCW radar employs frequency-modulated continuous wave technology. Range measurement is a crucial aspect of radar systems, providing information about the distance to a target. The IF amplifier plays a key role in signal processing, amplifying intermediate frequency signals for further analysis. The FMCW altimeter utilizes frequency-modulated continuous wave technology to accurately measure altitude above a reference point.
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Dr.Costas Sachpazis
Terzaghi's soil bearing capacity theory, developed by Karl Terzaghi, is a fundamental principle in geotechnical engineering used to determine the bearing capacity of shallow foundations. This theory provides a method to calculate the ultimate bearing capacity of soil, which is the maximum load per unit area that the soil can support without undergoing shear failure. The Calculation HTML Code included.
Final project report on grocery store management system..pdfKamal Acharya
In today’s fast-changing business environment, it’s extremely important to be able to respond to client needs in the most effective and timely manner. If your customers wish to see your business online and have instant access to your products or services.
Online Grocery Store is an e-commerce website, which retails various grocery products. This project allows viewing various products available enables registered users to purchase desired products instantly using Paytm, UPI payment processor (Instant Pay) and also can place order by using Cash on Delivery (Pay Later) option. This project provides an easy access to Administrators and Managers to view orders placed using Pay Later and Instant Pay options.
In order to develop an e-commerce website, a number of Technologies must be studied and understood. These include multi-tiered architecture, server and client-side scripting techniques, implementation technologies, programming language (such as PHP, HTML, CSS, JavaScript) and MySQL relational databases. This is a project with the objective to develop a basic website where a consumer is provided with a shopping cart website and also to know about the technologies used to develop such a website.
This document will discuss each of the underlying technologies to create and implement an e- commerce website.
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)MdTanvirMahtab2
This presentation is about the working procedure of Shahjalal Fertilizer Company Limited (SFCL). A Govt. owned Company of Bangladesh Chemical Industries Corporation under Ministry of Industries.
Cosmetic shop management system project report.pdfKamal Acharya
Buying new cosmetic products is difficult. It can even be scary for those who have sensitive skin and are prone to skin trouble. The information needed to alleviate this problem is on the back of each product, but it's thought to interpret those ingredient lists unless you have a background in chemistry.
Instead of buying and hoping for the best, we can use data science to help us predict which products may be good fits for us. It includes various function programs to do the above mentioned tasks.
Data file handling has been effectively used in the program.
The automated cosmetic shop management system should deal with the automation of general workflow and administration process of the shop. The main processes of the system focus on customer's request where the system is able to search the most appropriate products and deliver it to the customers. It should help the employees to quickly identify the list of cosmetic product that have reached the minimum quantity and also keep a track of expired date for each cosmetic product. It should help the employees to find the rack number in which the product is placed.It is also Faster and more efficient way.
Forklift Classes Overview by Intella PartsIntella Parts
Discover the different forklift classes and their specific applications. Learn how to choose the right forklift for your needs to ensure safety, efficiency, and compliance in your operations.
For more technical information, visit our website https://intellaparts.com
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Basic structure of c programming
1. Basic structure of C programming:
To write a C program, we first create functions and then put them together. A C program
may contain one or more sections. They are illustrated below.
1. Documentation section: The documentation section consists of a set of comment
lines giving the name of the program, the author and other details, which the
programmer would like to use later.
2. Link section: The link section provides instructions to the compiler to link functions
from the system library such as using the #include directive.
3. Definition section: The definition section defines all symbolic constants such using
the #define directive.
4. Global declaration section: There are some variables that are used in more than
one function. Such variables are called global variables and are declared in the
global declaration section that is outside of all the functions. This section also
declares all the user-defined functions.
5. main () function section: Every C program must have one main function section.
This section contains two parts; declaration part and executable part
2. 1. Declaration part: The declaration part declares all the variables used in the
executable part.
2. Executable part: There is at least one statement in the executable part.
These two parts must appear between the opening and closing braces.
The program execution begins at the opening brace and ends at the
closing brace. The closing brace of the main function is the logical end of the
program. All statements in the declaration and executable part end with a
semicolon.
6. Subprogram section: If the program is a multi-function program then the
subprogram section contains all the user-defined functions that are called in the
main () function. User-defined functions are generally placed immediately after the
main () function, although they may appear in any order.
All section, except the main () function section may be absent when they are not required.
C – Preprocessordirectives
PREV NEXT
C PREPROCESSOR DIRECTIVES:
Before a C program is compiled in a compiler, source code is processed by a program called preprocessor. This
process is called preprocessing.
Commands used in preprocessor are called preprocessor directives and they begin with “#” symbol.
Below is the list of preprocessor directives that C programming language offers.
Preprocessor Syntax/Description
Macro
Syntax: #define
This macro defines constant value and can be any
of the basic data types.
Header file
inclusion
Syntax: #include <file_name>
The source code of the file “file_name” is included
in the main program at the specified place.
Conditional
compilation
Syntax: #ifdef, #endif, #if, #else,#ifndef
Set of commands are included or excluded in
source program before compilation with respect to
the condition.
Other directives
Syntax: #undef, #pragma
#undef is used to undefine a defined macro
variable. #Pragma is used to call a function before
and after main function in a C program.
A program in C language involves into different processes. Below diagram will help you to understand all the processes that
a C program comes across.
3. There are 4 regions of memory which are created by a compiled C program. They are,
1. First region – This is the memory region which holds the executable code of the program.
2. 2nd
region – In this memory region, global variables are stored.
3. 3rd
region – stack
4. 4th region – heap
DO YOU KNOW DIFFERENCE BETWEEN STACK & HEAP MEMORY IN C LANGUAGE?
Stack Heap
Stack is a memory region where “local
variables”, “return addresses of function
calls” and “arguments to functions” are
hold while C program is executed.
Heap is a memory region
which is used by dynamic
memory allocation
functions at run time.
CPU’s current state is saved in stack
memory
Linked list is an example
which uses heap memory.
DO YOU KNOW DIFFERENCE BETWEEN COMPILERS VS INTERPRETERS IN C LANGUAGE?
Compilers Interpreters
Compiler reads the entire source
code of the program and converts
it into binary code. This process
Interpreter reads the program
source code one line at a time and
executing that line. This process is
4. is called compilation.
Binary code is also referred as
machine code, executable, and
object code.
called interpretation.
Program speed is fast. Program speed is slow.
One time execution.
Example: C, C++
Interpretation occurs at every line
of the program.
Example: BASIC
KEY POINTS TO REMEMBER:
1. Source program is converted into executable code through different processes like precompilation, compilation,
assembling and linking.
2. Local variables uses stack memory.
3. Dynamic memory allocation functions use the heap memory.
EXAMPLE PROGRAM FOR #DEFINE,#INCLUDE PREPROCESSORS INC LANGUAGE:
#define – This macro defines constant value and can be any of the basic data types.
#include <file_name> – The source code of the file “file_name” is included in the main C program where “#include
<file_name>” is mentioned.
C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdio.h>
#define height 100
#define number 3.14
#define letter 'A'
#define letter_sequence "ABC"
#define backslash_char '?'
void main()
{
printf("value of height : %d n", height );
printf("value of number : %f n", number );
printf("value of letter : %c n", letter );
printf("value of letter_sequence : %s n", letter_sequence);
5. 15
16
17
printf("value of backslash_char : %c n", backslash_char);
}
OUTPUT:
value of height : 100
value of number : 3.140000
value of letter : A
value of letter_sequence : ABC
value of backslash_char : ?
EXAMPLE PROGRAM FOR CONDITIONAL COMPILATION DIRECTIVES:
A) EXAMPLE PROGRAM FOR #IFDEF, #ELSE AND #ENDIF IN C:
“#ifdef” directive checks whether particular macro is defined or not. If it is defined, “If” clause statements are
included in source file.
Otherwise, “else” clause statements are included in source file for compilation and execution.
C
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>
#define RAJU 100
int main()
{
#ifdef RAJU
printf("RAJU is defined. So, this line will be added in "
"this C filen");
#else
printf("RAJU is not definedn");
#endif
return 0;
}
OUTPUT:
RAJU is defined. So, this line will be added in this C file
6. B) EXAMPLE PROGRAM FOR #IFNDEF AND #ENDIF IN C:
#ifndef exactly acts as reverse as #ifdef directive. If particular macro is not defined, “If” clause statements are
included in source file.
Otherwise, else clause statements are included in source file for compilation and execution.
C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <stdio.h>
#define RAJU 100
int main()
{
#ifndef SELVA
{
printf("SELVA is not defined. So, now we are going to "
"define heren");
#define SELVA 300
}
#else
printf("SELVA is already defined in the program”);
#endif
return 0;
}
OUTPUT:
SELVA is not defined. So, now we are going to define here
C) EXAMPLE PROGRAM FOR #IF, #ELSE AND #ENDIF IN C:
“If” clause statement is included in source file if given condition is true.
Otherwise, else clause statement is included in source file for compilation and execution.
C
7. 1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>
#define a 100
int main()
{
#if (a==100)
printf("This line will be added in this C file since "
"a = 100n");
#else
printf("This line will be added in this C file since "
"a is not equal to 100n");
#endif
return 0;
}
OUTPUT:
This line will be added in this C file since a = 100
EXAMPLE PROGRAM FOR UNDEF IN C LANGUAGE:
This directive undefines existing macro in the program.
C
1
2
3
4
5
6
7
8
9
10
#include <stdio.h>
#define height 100
void main()
{
printf("First definedvalue for height : %dn",height);
#undef height // undefining variable
#define height 600 // redefining the same for new value
printf("value of height after undef & redefine:%d",height);
}
8. OUTPUT:
First defined value for height : 100
value of height after undef & redefine : 600
EXAMPLE PROGRAM FOR PRAGMA IN C LANGUAGE:
Pragma is used to call a function before and after main function in a C program.
C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <stdio.h>
void function1( );
void function2( );
#pragma startup function1
#pragma exit function2
int main( )
{
printf ( "n Now we are in main function" ) ;
return 0;
}
void function1( )
{
printf("nFunction1 is called before main function call");
}
void function2( )
{
printf ( "nFunction2 is called just before end of "
"main function" ) ;"
9. 24 }
OUTPUT:
Function1 is called before main function call
Now we are in main function
Function2 is called just before end of main function
MORE ON PRAGMA DIRECTIVE IN C LANGUAGE:
Pragma command Description
#Pragma startup
<function_name_1>
This directive executes function named
“function_name_1” before
#Pragma exit
<function_name_2>
This directive executes function named
“function_name_2” just before termination
of the program.
#pragma warn – rvl
If function doesn’t return a value, then
warnings are suppressed by this directive
while compiling.
#pragma warn – par
If function doesn’t use passed function
parameter , then warnings are suppressed
#pragma warn – rch
If a non reachable code is written inside a
program, such warnings are suppressed by
this directive.
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.
The C standard library provides numerous built-in functions that your
program can call. For example, strcat() to concatenate two
10. strings, memcpy() to copy one memory location to another location,
and many more functions.
A function can also be referred as a method or a sub-routine or a
procedure, etc.
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 consists of a function header and
afunction 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
Given below is the source code for a function called max(). This function
takes two parameters num1 and num2 and returns the maximum value
between the two −
/* function returning the max between two numbers */
int max(int num1, int num2) {
/* local variable declaration */
int result;
11. if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
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 −
return_type function_name( parameter list );
For the above defined function max(), the function declaration is as
follows −
int max(int num1, int num2);
Parameter names are not important in function declaration only their
type is required, so the following is also a valid declaration −
int max(int, int);
Function declaration is required when you define a function in one source
file and you call that function in another file. In such case, you should
declare the function at the top of the file calling the function.
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.
12. 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. For example −
#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 : %dn", 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;
}
13. We have kept max() along with main() and compiled the source code.
While running the final executable, it would produce the following result
−
Max value is : 200
Function Arguments
If a function is to use arguments, it must declare variables that accept
the values of the arguments. These variables are called the formal
parameters of the function.
Formal parameters behave like other local variables inside the function
and are created upon entry into the function and destroyed upon exit.
While calling a function, there are two ways in which arguments can be
passed to a function −
S.N. Call Type & Description
1 Call by value
This method copies the actual value of an argument into the formal
parameter of the function. In this case, changes made to the parameter
inside the function have no effect on the argument.
2 Call by reference
This method copies the address of an argument into the formal parameter.
Inside the function, the address is used to access the actual argument used
in the call. This means that changes made to the parameter affect the
argument.
By default, C uses call by value to pass arguments. In general, it means
the code within a function cannot alter the arguments used to call the
function.
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.
14. 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 the previous chapter, there will be
the following basic variable types −
Type Description
char Typically a single octet(one byte). This is an integer type.
int The most natural size of integer for the machine.
float A single-precision floating point value.
double A double-precision floating point value.
void Represents the absence of type.
C programming language also allows to define various other types of
variables, which we will cover in subsequent chapters like Enumeration,
Pointer, Array, Structure, Union, etc. For this chapter, let us study only
basic variable types.
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;
Here, type must be a valid C data type including char, w_char, int, float,
double, bool, or any user-defined object; and variable_list may consist
of one or more identifier names separated by commas. Some valid
declarations are shown here −
int i, j, k;
char c, ch;
float f, salary;
double d;
The line int i, j, k; declares and defines the variables i, j, and k; which
instruct the compiler to create variables named i, j and k of type int.
15. 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 −
type variable_name = value;
Some examples are −
extern int d = 3, f = 5; // declaration of d and f.
int d = 3, f = 5; // definition and initializing d and f.
byte z = 22; // definition and initializes z.
char x = 'x'; // the variable x has the value ' x'.
For definition without an initializer: variables with static storage duration
are implicitly initialized with NULL (all bytes have the value 0); the initial
value of all other variables are undefined.
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 definition has its meaning at the time of
compilation only, the compiler needs actual variable definition at the time
of linking the program.
A variable declaration is useful when you are using multiple files and you
define your variable in one of the files which will be available at the time
of linking of the program. You will use the keyword extern to declare a
variable at any place. Though you can declare a variable multiple times
in your C program, it can be defined only once in a file, a function, or a
block of code.
Example
Try the following example, where variables have been declared at the
top, but they have been defined and initialized inside the main function −
#include <stdio.h>
// Variable declaration:
extern int a, b;
extern int c;
extern float f;
int main () {
16. /* variable definition: */
int a, b;
int c;
float f;
/* actual initialization */
a = 10;
b = 20;
c = a + b;
printf("value of c : %d n", c);
f = 70.0/3.0;
printf("value of f : %f n", f);
return 0;
}
When the above code is compiled and executed, it produces the following
result −
value of c : 30
value of f : 23.333334
The same concept applies on function declaration where you provide a
function name at the time of its declaration and its actual definition can
be given anywhere else. For example −
// function declaration
int func();
int main() {
// function call
int i = func();
}
// function definition
int func() {
return 0;
}
17. Lvalues and Rvalues in C
There are two kinds of expressions in C −
lvalue − Expressions that refer to a memory location are called "lvalue" expressions.
An lvalue may appear as either the left-hand or right-hand side of an assignment.
rvalue − The term rvalue refers to a data value that is stored at some address in
memory. An rvalue is an expression that cannot have a value assigned to it which
means an rvalue may appear on the right-hand side but not on the left-hand side of
an assignment.
Variables are lvalues and so they may appear on the left-hand side of an
assignment. Numeric literals are rvalues and so they may not be
assigned and cannot appear on the left-hand side. Take a look at the
following valid and invalid statements −
int g = 20; // valid statement
10 = 20; // invalid statement; would generate compile -time error