Lecture2 1 C Programming I
Lecture2 1 C Programming I
C PROGRAMMING I
#include “stdio.h”
#include “stdlib.h”
int main(void)
{
printf("Hello World!");
return 0;
}
Compiling the Source Code
• For compilation of the source code, type ‘gcc –o hello hello.c’
• It yields ‘hello.o’ and ‘hello’. The last file is the executable. The files ending with .o
is the object files, which can be utilized as a kind of libraries when you work with
multiple .c files.
• ‘gcc –c hello.c’ yields just the object file. This can be used as a library like ‘gcc –o
world world.c hello.o
• Try to compile hello.c, and run it on the linux prompt. What do you see?
Comment Lines
• Header files ends with .h conventionally. Inside the header file, there are usually
declaration (or definition) of variables and functions. Note that in C-program, all the
variables and functions should be declared before they are used. It is a different
feature from the bash-script.
e.g) int a; a=3; or float f(float), y; y=f(3.2);
• Usually stdio.h and stdlib.h are included in almost every C-source code, since they
have declarations for commonly used input/output functions, and other frequently
used functions such as exit();
Declaration of Variables
• Every variable should be declared before it is used.
• The declaration can be made in a separate header file.
• A variable declared out side a function block is called global, which can be used
by any functions
• A variable declared inside a function is called local, and it can be used by that
function only.
• Various type of variables: Int, long int, unsigned int, char, float, double, …
• For more details, see http://en.wikipedia.org/wiki/C_data_types
double Fractional number with doubled precision Doubled precision from float
Function Call and Return
• In the hello.c, ‘printf()’ is one example of functions. It is declared in stdio.h, and
defined in one of the C-libraries
• Every function has a type in the same as the variables. It should be declared before
called. e.g.) float func2(float,int,char);
• The declared function should be defined somewhere else outside the body calling
that function.
• main is an integer type function. You cannot change the type of main function. And
you can also skip ‘return’ in the main function, since no other function can take the
returned value. i.e., main() is the upper-most function. This is not called by other
functions.
Function Call and Return - Example
#include “stdio.h”
#include “stdlib.h”
Int main ()
{
float f1(float,float);
float x,y;
x=2.3;
y=f1(1.5, 2.0*x);
printf(“Answer %f\n”,y);
}
Format
• Any character except some control characters inside “ “ is printed on the screen as it
is.
• Control characters: \n, \b, %, etc.
• To put “ mark, use \”
• The character next to % sign represents the type of printed values. d-integer, s-
string, c-character, f or g – floating number, %3.2f – 3.2 digits, etc.
• In the right side of “ “, actual values corresponding to each % are written in the
same order. These values can be a variable, number, string itself, or even a function
or calculation.
Control Characters in printf()
Expression Description
\a Beep
\\ Print \
\’ Print ‘
\” Print “
Exercise
Exercise 1
Make a C-program which print your name on the screen. Compile it, and run it.
Exercise 2
Make a C-program which calculates 5.237+1.456 and print the result on the screen.
Homework
Question 1
Make a C-program, which contains a function to calculate the sin-value of a given
argument.
Question 2
Make a C-program, which contains two functions, one is to calculate the exp(-x) and
the other to calculate x2. From the main routine, call those functions with any value
for the arguments, and multiply the results. Print it on the screen.
Note: To use mathematical functions, you have to include “math.h” as a header. For
compiling, use –lm option. For example, gcc –o a a.c –lm Here –l means to ‘link’ a
library. ‘m’ means the mathematical library.