Download presentation
Presentation is loading. Please wait.
1
CS1061 C Programming Lecture 2: A Few Simple Programs A. O’Riordan, 2004
2
First Program The best way to get started with C is to actually look at a program. main(){} notes: The word "main" is very important, and must appear once, and only once in every C program. This is the point where execution is begun when the program is run. We will see later that this does not have to be the first statement in the program but it must exist as the entry point.
3
First Program (continued) notes: Following the "main" program name is a pair of parentheses which are an indication to the compiler that this is a function. The two curly brackets, properly called braces, are used to define the limits of the program itself. The actual program statements go between the two braces and in this case, there are no statements because the program does absolutely nothing. You can compile and run this program, but since it has no executable statements, it does nothing.
4
Second Program Another sample C program is given below: /*Sample program */ #include int main(){ printf(“C is fun!\n”); return(0); }
5
Second Program (continued) notes : This program outputs text to the monitor. Notice the semi-colon at the end of the statements. C uses a semi- colon as a statement terminator, so the semi-colon is required as a signal to the compiler that this line is complete. The first printf() outputs a line of text and returns the carriage. printf() is a function used for displaying text. This function is part of the C library. \n is a special newline character (see later). The #include is a pre-processor directive which includes declarations of library functions. The # character must appear as the first character in a line. stdio.h is needed because of the printf().
6
Second Program (continued) return() is a standard command that causes the program to ‘ return ’ a value. Strictly speaking it is not needed here as it is the last line of main() and the program will terminate anyway. /*... */ indicates a comment which will not be parsed by the compiler. Comments are totally ignored by the compiler, they're just annotations you can add to make it easier to read your code. The comment has the following syntax: A comment begins with the two characters /* (no space between them) and ends with the characters */. A comment can span many lines. Comments cannot be nested.
7
A Word on Functions Functions are the basic unit of any C program, or any structured programming language for that matter. Functions are called procedures or subroutines in other languages. If a function is well written it may be reusable from one project to the next. Every C program has to have at least one function called main(). This is the first function that is executed. This function may then call other functions. It is conventional to place the main() function at the start of the program followed by all the other functions in the file. It is considered good software engineering practice to keep functions short.
8
A Word on Functions (continued) A programmer can create his own functions Advantage: the programmer knows exactly how it works Disadvantage: time consuming Programmers will often use the C library functions Use these as building blocks Avoid re-inventing the wheel If a pre-made function exists, generally best to use it rather than write your own Library functions carefully written, efficient, and portable
9
“Form” of Fucnction A function has the form: return_type function_name (parameters){ } The important thing to note here is the order. Firstly we have the function name (and return type and parameters). Any C function may have parameters, which is a mechanism for passing extra information to a function. Then we have the body of the function enclosed by curly braces {}.
10
One last program #include main(){ int index; index = 13; printf("The value of the index is %d\n",index); index = 27; printf("The value of the index is %d\n",index); index = 10; } notes: This uses a variable to store a numeric value.
11
One last program (continued) notes: int index; is used to define an integer variable named "index". The "int" is a reserved word in C, and can therefore not be used for anything else. Variables are locations in memory where a value can be stored int means the variables can hold integers (e.g. -1, 3, 0, 47) there are three statements that assign a value to the variable "index", but only one at a time. The first one assigns the value of 13 to index, and its value is printed out. The second assignment gives index the value 27 and the third 10. the syntax of printf() is discussed later.
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.