C programming language Code Warrior C programming language
Provide me the solutions (codes) to the exercises given in the c programming manual.
I will attach the manual you can find the exercises there.
please no plagiarism !!! 0% similarity
please provide me the % of similarity in trunit in . C Tutorial Using Freescale Code
Warrior IDE
By
Dr. Wilfrido Moreno, Luis Jaimes, Prashanth Chetlur Adithya & Sunil Kumar Jain
Table of Contents
Setting up the Environment for Code Warrior (If already installed go to next section) …………………… 3
First time running Code Warrior ……………………………………………………………………………………………… 3
First Program: Hello World………………………………………………………………………………………………….. 5
Compiling or Building a Program ………………………………………………………………………………………….. 8
Running the Program. ………………………………………………………………………………………………………… 8
Implementation and testing tools ………………………………………………………………………………………….. 10
Software development process………………………………………………………………………………………….. 10
Introduction to C programming …………………………………………………………………………………………….. 12
Variables, Types and Arithmetic Expressions. ………………………………………………………………………. 12
Conditional Operators ………………………………………………………………………………………………………. 16
If Statement …………………………………………………………………………………………………………………. 16
Switch case in C ……………………………………………………………………………………………………………. 19
Exercises …………………………………………………………………………………………………………………………. 21
Program Looping …………………………………………………………………………………………………………………. 22
for Loops …………………………………………………………………………………………………………………………. 22
while Loops ( Condition-Controlled Loops ) …………………………………………………………………………. 24
do-while Loops ………………………………………………………………………………………………………………… 25
Exercises …………………………………………………………………………………………………………………………. 26
Preprocessor Directives ………………………………………………………………………………………………………… 27
Macro Expansion ……………………………………………………………………………………………………………… 27
File Inclusion ……………………………………………………………………………………………………………………. 28
Conditional Compilation ……………………………………………………………………………………………………. 28
Functions ……………………………………………………………………………………………………………………………. 29
When to Use Functions …………………………………………………………………………………………………….. 29
Exercises …………………………………………………………………………………………………………………………. 33
Introduction to Pointers ……………………………………………………………………………………………………….. 34
Pointer Pointing to another Pointer ……………………………………………………………………………………. 36
Exercises …………………………………………………………………………………………………………………………. 38
Arrays and Multi-dimensional Arrays ……………………………………………………………………………………… 39
Declaring Arrays ………………………………………………………………………………………………………………. 39
Array Initialization ……………………………………………………………………………………………………………. 40
Exercises: ………………………………………………………………………………………………………………………… 44
1
Arrays, Pointers and Functions………………………………………………………………………………………………. 45
Function passing parameters by value vs. pass by reference …………………………………………………. 45
Pass By Value ……………………………………………………………………………………………………………….. 45
Pass by Reference …………………………………………………………………………………………………………. 45
Passing arrays as function parameters………………………………………………………………………………… 46
Access Array Positions Using Pointers…………………………………………………………………………………. 47
Return Pointer from Functions …………………………………………………………………………………………… 49
Exercises …………………………………………………………………………………………………………………………. 51
Structures …………………………………………………………………………………………………………………………… 52
Exercises …………………………………………………………………………………………………………………………. 54
Bitwise Operators ………………………………………………………………………………………………………………… 55
2
Setting up the Environment for Code Warrior (If already installed go to next
section)
Please follow the steps given below in order to setup Code Warrior and to start programming in C.
1. Download Code Warrior IDE Version 10.6:
http://www.freescale.com/webapp/sps/site/prod_summary.jsp?code=MQX&fpsp=1&tab=Desig
n_Tools_Tab
2. http://sourceforge.net/projects/mingw/files/Installer/
Start installing MinGW, choose C:/ as the installation directory. Then, install Code Warrior IDE Version
10.6
First time running Code Warrior
Follow steps below to start Code Warrior.
1. Go to Programs – > Code Warrior.
2. Select the workspace as shown in Figure 1.
This folder corresponds to the work directory and it is the place where Code Warrior stores all the
projects. A new folder can be created by clicking on the Browse button.
Figure 1. Workspace
3
3. The following welcome interface window will appear as shown in Figure 2.
Figure 2. Welcome Window
4. After a couple of seconds, the welcome window (Figure 2) will turn in the Working Vista, as
shown in Figure 3. Click on C/C++ on the upper left corner to access the C/C++ vista.
Figure 3. C/C++ Vista
At this point, the Code Warrior environment has been setup to create the first program.
4
First Program: Hello World
1. Go to File ->New -> Projects Click on Projects, as shown in Figure 4.
Figure 4. Starting a New Project
2. Check the box Show All Wizards, choose C/C++, and then C project, as shown in Figure 5
Figure 5. New Project window
5
3. Make sure to check the box on the window Confirm Enablement, see Figure 6.
Figure 6. Selecting a C Project
4. Assign a name to the project [e.g., Project_one], select Hello World ANSI C Project and
MinCW GCC as Toolchains, as shown in Figure 7.
Figure 7. Create C Project of Selected Type Window
6
5. Enter your name in the Author section and click on the Finish button. Finally, the project would
be added in CodeWarrior Projects as shown in Figure 8.
Figure 8. Program Project_one.c
6. (Optional step) In order to remove the warnings from the #includes directives as shown in
Figure 8, right click on the Project_one:Debug. Go to Properties -> CC++ General ->
Preprocessor Include Paths Macros and select the Providers tab. Then, check the box CDT
GCC Builtin Compiler Settings [Shared] and click on the Finish button, see Figure 9.
Figure 9. Including Paths
7
Compiling or Building a Program
In order to compile the program, Go to Project ->Build Project. If there are not errors, the output will
look like Figure 10.
Figure 10. Compiling Output
Running the Program.
In order to run or execute the program go to Run -> Run Configuration, as shown in Figure 11.
Figure 11. Running a Program
8
Since this program does not have any argument, none of the tap in the run configuration
window is used. Finally, click the Run button. The following message should be the output in the
console, as shown in Figure 12.
Figure 12. Console Output of the Project_one.c program.
9
Implementation and testing tools
To aid in the design and development process, there are a number of support tools commercially
available to make the process easier. We provide a brief definition of some of these design tools.
Assembler: An assembler converts a microcontroller control algorithm written in assembly language to
processor specific machine code.
Compiler: A compiler translates a high-level language such as C first into assembly language and then
into processor specific machine code.
Emulator: An emulator is a software program that simulates the operation (emulates) of a hardware
microcontroller. The emulator is used frequently during system development and testing.
Programmer: Usually microcontroller algorithms are programmed in a host personal computer (PC)
using an assembler or compiler. The resulting machine code is downloaded from the host PC to the
microcontroller via a hardware programmer. For some microcontrollers this is simply a serial RS-232
compatible cable. For other microcontrollers it consists of a programming pod which converts the
machine code to appropriate programming signals to program the memory resident within the
microcontroller.
Logic analyzer: A logic analyzer is a piece of test equipment that allows the simultaneous viewing of
multiple channels of signals from a microcontroller. It is especially useful for examining the timing
relationships between related microcontroller signals.
In system programming (ISP): Some microcontrollers are equipped with ISP capability. This means that
a program can be downloaded into the microcontroller while the microcontroller is resident within its
systems.
Software development process
The software development process is illustrated in Figure below. This is a general overview description
of the code development process. It provides a generic description of the end-to-end process from C
source file development to downloading machine code in the target processor. Details for a specific
software suite coupled with a specific target controller may have slight differences.
As the system designer, you will write the C source files that implement the required control algorithm.
The source files consist of C source code and also header files. Header files may include functions you
have written, functions provided with your compiler, and also personality data for the specific processor
you are using as the target system.
The C source files are compiled using a compiler for the specific target processor. The output from the
compiler will be assembly code which is automatically routed to the assembler portion of the software
development suite. The output from the assembler is object code which is linked with library files
10
provided with the compiler. The overall output will be machine code for download into the target
processor. The software development suite may also provide some auxiliary files. These files provide
information on memory usage and code placement within memory. These files are handy if system
troubleshooting is required. The machine code is downloaded into the target controller via a
programming cable or pod.
Figure 13. Software development suite
11
Introduction to C programming
So far, the tutorial describes how to setup the Code Warrior environment and create a simple program.
This section explains the basic syntax and elements of a C program and describes how to write a
program from the beginning. By the end of this section, you will feel comfortable to write, debug and
execute a simple C program.
Every C program begins with a function called “main”. This function is simply a collection of commands
that will perform either an operation or a series of operations. The main function is always called when
the program first executes, see Figure 13
From the function main, other functions can be called. For example in Figure 13, the function
printf is called, this function comes with the compiler MinGW that was installed during system set
up. In order to acces the compiler functions, a header with the #include directive must be included at
the beginning of the program [before the main function], see Figure 13. The function printf allows to
display any text message in the console. Finally, the control is returned to the operating system by
using the directive return 0.
//Hello Project_one.c
#include
int main (void)
{
printf (“Hello Word.n”);
return 0;
}
Figure 14. Program_one.c
Variables, Types and Arithmetic Expressions.
Variables are the elements used in programming to store data temporarily. There are several types of
variables within the C Library to be used as required, see Table 1. Since this manual does not pretent to
be a complete reference of C language, the reader is invited to check a formal textbook on the C
Programming Language (ANSI C).
Type
Size
(bytes)
Prinf chars
Range
char
1
%c
-128 to 127 or 0 to 255
unsigned char
1
%c
0 to 255
12
signed char
1
%c
-128 to 127
int
2 or 4
%d, %x, %o, %i
-32,768 to 32,767 or -2,147,483,648 to
2,147,483,647
unsigned int
2 or 4
%u, %x, %o
0 to 65,535 or 0 to 4,294,967,295
Short int
2
%hi, %hx, %ho
-32,768 to 32,767
unsigned short
2
%hu, %hx, %ho
0 to 65,535
Long int
4
%li, %lx, %lo, %d
-2,147,483,648 to 2,147,483,647
unsigned long
4
%lu, %lx, %lo
0 to 4,294,967,295
float
4
%f,%e, %g, %a
1.2E-38 to 3.4E+38
double
8
%f, %e, %g, %a
2.3E-308 to 1.7E+308
long double
10
%Lf, %Le, %Lg
3.4E-4932 to 1.1E+4932
Table 1. Datatypes in C
A variable of type char stores a single character, variables of type int store integer numbers, and
variables of type float stores numbers with decimal places.
Each of these variable types – char, int, and float – is a keyword that is used when a variable is declared &
defined. The syntax used in this case is:
Example #1:
int myVariable;
Figure 15 – Example of Declaration of a Variable
Here, the type of variable is integer and the actual variable name is myVariable. This variable can be
used later to store a value.
Example #2:
myVariable = 5;
Figure 16. Initialization
Clarification
Here, the expression in Figure 15, unlike math, doesnt mean that myVariable is equal to 5, this
expression actually implies that the value on the right (i.e., five) will be assigned to the variable on the
left (i.e., myVariable).
Lets review this concept using the following example.
13
#include
int main (void) {
int triangularNumber;
triangularNumber = 1+2+3+4+5+6+7+8;
printf(“The eighth triangular number is %dn”, triangularNumber);
return 0;
}
Figure 17. Project_two.c
Program Explanation
The body of program is completely between braces. A variable named triangularNumber is declared
as an integer, this variable is used to store the sum of 1+2+3+4+5+6+7+8. The result of the program will
be displayed in the console section.
Note that, In order to use the printf() function in any program, there is a need to include the compiler
standard library .
Lets write this program using CodeWarrior from scratch using an empty C project instead of a Hello
Word template. Follow the following steps:
Go to New -> Project -> C project.
Figure 17 shows the C project window, name the project Program_two. Choose the option Empty
project from project type and MinGW GCC from toolchains as is illustrated in Figure 17. The final
outcome is an empty project.
Figure 18. Creating an Empty Project
14
Because this is an empty project, a source file is added to the program. In order to do that, right click on
Project_two -> New ->Source File.
Figure 19. Adding a source File to the project Project_two
Select the source file name as Project_two.c and click on Finish. The program can be written in
Project_two.c file as shown in Figure 16.
Build and Run the program, and make sure that the output of the program is similar to the one shown in
Figure 19.
Figure 20. Program Project_two.c
15
Conditional Operators
A characteristic that distinguishs a computer with programming language from a simple calculator is the
ability to take decisions. As in the English language, an if statement is used. This statement is usually
used along with a set of relational operators, see Table 2.
Symbol
Meaning
Example
>
greater than
5 > 4 is TRUE
<
less than
4 < 5 is TRUE
>=
greater than or
4 >= 4 is TRUE
Purchase answer to see full
attachment
Why Choose Us
Top quality papers
We always make sure that writers follow all your instructions precisely. You can choose your academic level: high school, college/university or professional, and we will assign a writer who has a respective degree.
Professional academic writers
We have hired a team of professional writers experienced in academic and business writing. Most of them are native speakers and PhD holders able to take care of any assignment you need help with.
Free revisions
If you feel that we missed something, send the order for a free revision. You will have 10 days to send the order for revision after you receive the final paper. You can either do it on your own after signing in to your personal account or by contacting our support.
On-time delivery
All papers are always delivered on time. In case we need more time to master your paper, we may contact you regarding the deadline extension. In case you cannot provide us with more time, a 100% refund is guaranteed.
Original & confidential
We use several checkers to make sure that all papers you receive are plagiarism-free. Our editors carefully go through all in-text citations. We also promise full confidentiality in all our services.
24/7 Customer Support
Our support agents are available 24 hours a day 7 days a week and committed to providing you with the best customer experience. Get in touch whenever you need any assistance.
Try it now!
How it works?
Follow these simple steps to get your paper done
Place your order
Fill in the order form and provide all details of your assignment.
Proceed with the payment
Choose the payment system that suits you most.
Receive the final file
Once your paper is ready, we will email it to you.
Our Services
No need to work on your paper at night. Sleep tight, we will cover your back. We offer all kinds of writing services.
Essays
You are welcome to choose your academic level and the type of your paper. Our academic experts will gladly help you with essays, case studies, research papers and other assignments.
Admissions
Admission help & business writing
You can be positive that we will be here 24/7 to help you get accepted to the Master’s program at the TOP-universities or help you get a well-paid position.
Reviews
Editing your paper
Our academic writers and editors will help you submit a well-structured and organized paper just on time. We will ensure that your final paper is of the highest quality and absolutely free of mistakes.
Reviews
Revising your paper
Our academic writers and editors will help you with unlimited number of revisions in case you need any customization of your academic papers