Uploaded by s2e0b0a6s

02a running C

advertisement
How to Run a C Program
CSE 1310 – Introduction to Computers and Programming
Vassilis Athitsos & Bhanu Jain
University of Texas at Arlington
1
What’s Programming
• What is computer programming?
– Interpretation of a task or algorithm in a computer language.
• What is an algorithm?
– A set of instructions for accomplishing a task.
– Input and Output
2
What’s Programming
• How about preparing salad?
– Steps:
•
•
•
•
Clean and cut vegetables
Add dressing & cheese
Add Croutons
Mix
3
What’s Programming
• The algorithm for sorting three integers in ascending
order, given 20, 5, 8.
• Steps: 5, 8, 20
• To determine the concrete steps involved in solving a
problem, we may
– Logically represent the problem
– Implement the logic in computer languages (c, c++, java,
python, perl…)
• Given a thousand integers?
4
Why Program?
• Manually operating – not possible
– Repeat many times
– Google search engine (Searching in a File)
5
Initial Steps
• Install Code::Blocks or any other IDE. You can download it from
http://www.codeblocks.org/downloads/binaries
• These are steps that you do just once. Once the IDE is installed, you can
run any C program you like on your computer.
• Check install_CodeBlocks.pdf on Canvas.
• Code::Blocks tutorial http://wiki.codeblocks.org/
• Code:Blocks directory
– *.layout and *.depend files – state information for the IDE
– *.cbp defines a Code::Blocks project.
– .cbp is the extension for Code:Blocks solution file
6
What does a C compiler do?
• C Compilers are software programs
• They translate source code from a high-level
programming language to a lower level language
(e.g., assembly language, object code, or machine
code)
• They create an executable program.
7
Writing Code (1)
• If you create a new project Code::Blocks will create a C file,
that looks like this:
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("Hello world!\n");
return 0;
}
8
Writing Code (2)
• The place where you put your code is indicated with the text
// TODO code application logic here
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("Hello world!\n");
// TODO code application logic here
return 0;
}
9
Writing Code (3)
• For the first assignments, all you have to do is replace that
"TODO" line with your own lines of code.
#include <stdio.h>
#include <stdlib.h>
int main()
{
// TODO code application logic here
return 0;
}
10
Writing Code (4)
• Text that starts with /* and ends with */ is NOT PART OF THE
PROGRAM.
• This text is called "comments", and is meant to be read by
humans, not the computer.
• We will usually not care about (and not show) the comments
that Code::Blocks generates. You can delete them if you want.
/*
* Comments
*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
// TODO code application logic here
return 0;
}
11
A First Example
• Remember: for a good part of the class, your code will go where
the TODO placeholder is.
#include <stdio.h>
#include <stdlib.h>
int main()
{
// TODO code application logic here
return 0;
}
12
A First Example
• Here we have replaced the TODO placeholder with a line of
code.
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("My name is Bhanu Jain!\n");
return 0;
}
• To run this code, go to Build and Run ().
13
Failure to Run
• A lot of times you will get errors, and you will not be able to run
the program.
• Do not panic, this is a very common thing to happen.
• For example, on this code we introduce an error on purpose, to
see what happens (we delete the semicolon after "Hello
world").
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("My name is Bhanu Jain!\n")
return 0;
}
• Now, when we try to run this code, we get what is shown on the
14
next slide:
Error Message
• You will be seeing this window a lot.
• Now, the output window will show you an error message, which you
should try to fix.
• Note the red square on line 6 of the code.
• This is CodeBlock's way of telling you that that line is not correct.
• You will learn how to fix these errors during the course.
• Fix and Run. If you get more errors, keep fixing them till the code 15runs.
Warning. What happened here?
16
Warning
• We didn’t specify the function return type and are now
trying to return an integer ‘0’.
17
First program
• Void is an empty data type that has no value.
• Void data type in functions is used when we don't want to
return any value to the calling function
18
Warning
• We said the function will not return anything by typing
void and are now trying to return an integer ‘0’.
• Void is an empty data type that has no value.
• Void data type in functions is used when we don't want to
return any value to the calling function
19
Running Existing Code
• Oftentimes you may want to run code from somewhere (the
slides, the textbook, the course website, etc.).
• To do that, you need to make sure that your project is named
appropriately.
• Step 1: Look at the code that you want to run.
• Step 2: When you create your project on Code::Blocks, use that
name for your project.
• Step 3: Copy the code that you want to run to the main.c file that
Code::Blocks created.
20
First Example
#include<stdio.h>
int main()
{
int num; /* define a variable called num */
num = 4; /* assign a value to num */
printf("This is a simple example.\n");
printf("This is a program with %d statements.\n", num);
return 0;
}
21
First Example
• # include <stdio.h>
– Tell compiler to include the information included in stdio.h
– Include directive cause the content of another file to be included
• void main( )
– A function name
– C programming consists of one or more functions (basic modules)
– Parenthesis identify a function
– Similar to the function defined in math
– Arguments and return
• /* a … */
– Enclose comments (block), “//” – single line
– Intended for the reader and ignored by the compiler
22
First Example
• { - the beginning of the function body (statements separated by “;”)
• int num;
– A declaration statement
– num is an identifier
– Declare a variable before using it
– Traditionally, declare it at the beginning
– Lowercase letters, uppercase letters, digits, the underscore
– First character must be a letter or an underscore
– Not key words
23
First Example
• num = 1;
– an assignment statement
– Set space in memory
– Reassign later
• printf(“ ”)
– Part of the standard C library, a function
• %d
– Placeholder/format specifier - where and in what form to print
• } – the end of the function
• \n
– Start a new line
24
A “Good” Program
• There are different criteria by which one
program may be considered better than another.
Some examples are:
–
–
–
–
Readability – collaborative work
Maintainability – self-updated
Scalability – large-scale data set
Performance (e.g., how fast it runs or how much memory it uses)
25
Download