Presentation is loading. Please wait.

Presentation is loading. Please wait.

Last week: We talked about: History of C Compiler for C programming

Similar presentations


Presentation on theme: "Last week: We talked about: History of C Compiler for C programming"— Presentation transcript:

1 Last week: We talked about: History of C Compiler for C programming
C applications Anatomy of a C program

2 Running your code: How to run your C code?
First save your code in your machine using a “.c” extension. For example, you can call your “hello world” program hello.c Then use your C compiler to convert human-readable code to machine-readable code gcc hello.c -o hello The above command tells gcc to compile hello.c into a form that machine can execute. “-o” tells it to name the compiled program 'hello' The above command will create a hello.exe file if you are using windows. If your code is error-free(more in next slides), you can then execute your code in the command prompt window.

3 Running your code: cont'd
To execute your hello.exe in windows machine.. Just type “hello” in the command prompt window. The output looks something similar to this one: IMP note: Please make sure that your current working directory in command prompt window is the same one where your C program is saved. Running a C code on Linux/Mac will follow very similar steps. Only few tweaks are needed. For example, terminal should be used in Linux instead of cmd.

4 Various Error in C Compile Error Runtime Syntax Logical

5 Compile or Syntax Errors
Refers to a state when a compiler fails to compile a piece of computer program source code, either due to errors in code, or, more unusually, due to errors in the compiler itself. A compilation error message often helps programmers debugging the source code for possible errors Code: Error message: #include <stdio.h> /* To shorten example, not using argp */ int main() { printf ("Hello, world!\n") return 0; } semicolon.c: In function `main': somefile.cpp:24: parse error before `return' In this example: a semicolon (;) at the end of the line printf is missing. Adding a semicolon will get rid of this error.

6 Runtime Errors Runtime errors occur when the program is running, and thus, they can only occur if there is a program to run (i.e., it must have compiled and linked without errors). When you run the executable and something goes wrong then we call it a run-time error. Code: Error message: int points = 95; int iter = 0; int avg; avg = points / iter; The program would crash saying: Floating exception Here, we tried to divide points with iter(=0)

7 Logical Errors Logical Error
This is an error that the programmer injected into the program No error Is given the program will yield incorrect output

8 Programming Style Bad Coding: Good Coding:
“There is no programming language, no matter how structured, that will prevent programmers from making bad programs.” ~ Larry Flon A working but uncommented program is like a time bomb waiting to explode. Code that looks obvious to a programmer as he writes it is often confusing and cryptic when he revisits it a week later. The comment starts with /* The comment ends with */ So, please make a habit of commenting your code wherever necessary. Only one rule exists for good programming: Make your program clear, concise and simple as possible. Bad Coding: Good Coding:

9 Elements of a Program Variables: Instructions Data (variables)
In computer programming, you need two things: Data (variables) Instructions (Code) Variables: Variables are the basic building blocks of a program. We must order our materials before we start constructing a building. Similarly, we must declare our variables before we can use them. C allows us to store values in variables. Each variable has a variable type. Variables are case sensitive. Also, you can not use reserved words for variable names. Such as, int, while, for, float … Instructions Instructions tell the computer what to do with the variables.

10 Variable types: int - numerical value
char - alpha/numeric character array double – precision no less than a float ( roughly double the size of a float, refer to IEEE 754 standard or refer to float.h) float – precision is not less than a double Pointer (int, double, char) – a memory location

11 Integer Variables An int variable can store a value in the range of to Think of an int variable as no fractional parts are allowed. Declare an int you can use the following; int x; int x = 10; Note the “=“ symbol is use for assignment operations

12 Double and Floats Variables
Float, of floating point, number has about seven digits of precision and a range of about 1.E-36 to 1.E+36. A floats takes four bytes to store Double, of double precision, number has about 13 digits of precision and a range of about 1..E-303 to 1.E A double takes eight bytes to store. float x; double y; float x = 10.0; double y = 2.567;

13 Character Variables C only has the concept of numbers and characters.
A character of ‘A’ != ‘a’, as these values refer to ASCII values A character is a single character stored in one byte char c; or char c = ‘X’.. Difference between “X” and ‘X’ ‘X’ – means a character of 1 byte long containing the value ‘X’ “X” – s string with a length of 2 bytes containing the value “X”

14 Data types:

15 Variable nomenclature:
Characters Allowed : Underscore(_) Capital Letters ( A – Z ) Small Letters ( a – z ) Digits ( 0 – 9 ) Blanks & Commas are not allowed No Special Symbols other than underscore(_) are allowed First Character should be alphabet or Underscore Variable name Should not be Reserved Word (int, for, if,... etc) Valid names Invalid names Num1 _NUM iNt num 1 1num int


Download ppt "Last week: We talked about: History of C Compiler for C programming"

Similar presentations


Ads by Google