0% found this document useful (0 votes)
19 views

Lecture2 1 C Programming I

This document outlines an introductory C programming lecture, covering topics like the "Hello World" program, compiling source code, comment lines, header files, variable declaration, function calls and returns, and text output using printf(). It provides examples of a simple C program, compiling commands, declaring and calling functions, and using printf formatting characters. Exercises are included to write programs to print a name and calculate a simple expression. Homework assignments involve writing functions to calculate trigonometric and exponential functions.

Uploaded by

csyousangkwon
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Lecture2 1 C Programming I

This document outlines an introductory C programming lecture, covering topics like the "Hello World" program, compiling source code, comment lines, header files, variable declaration, function calls and returns, and text output using printf(). It provides examples of a simple C program, compiling commands, declaring and calling functions, and using printf formatting characters. Exercises are included to write programs to print a name and calculate a simple expression. Homework assignments involve writing functions to calculate trigonometric and exponential functions.

Uploaded by

csyousangkwon
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

LECTURE 2-1

C PROGRAMMING I

March 15, 2016


Outline
• Starting from ‘Hello World’ Program
• Compiling the Source Code
• Comment Lines
• Including Header File
• Declaration of Variables
• Function Call and Return
• Text output by ‘printf()’ function
‘Hello World’ Program
/* This is a sample C-program
It prints “Hello World!” to the screen.
*/

// One more thing. ‘main’ is a main function,


which is necessary in any C-program.

#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

• Multiple lines enclosed by /* … */ is neglected in the compiling. They can be used


as description or explanation of each lines of your C-program.

• A simple one-line comment can be inserted as // ….. . As long as there is no


carriage return, it is considered as a single line, though it may look two lines on the
screen.
Header Files

• 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);

• To include header files in your C-program, do it like ‘#include “headerfile.h” before


your main function starts.

• 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

Type Description Size


char One character. Compatible with integer type 8 bits or more

int Integer 16 bits or more

float Fractional number with single precision Machine-dependent. Usually 32 or


64 bits

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.

• Every function returns a value of that function’s type. It is done by ‘return


something’ command. Here the ‘something’ can be a number , a variable, or even
an another function with the same type. Exception is the ‘void’ type function. In
this case, you can skip the return command.

• 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);
}

float f1(float x1, float x2)


{
return x2-3.0*x1;
}
Text Output by printf()

Format

printf(“Integer %d Float %g String %s Character %c Float %3.2f\n”, 3, x, “string”, c,


11.3597*32.678);

• 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

\n move to a new line

\t Move to the next tab

\b Move backward by one space

\r Move back to the starting point of the line

\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.

For any homework submission by email, indicate your name


and student number in the email title.
Due date: 24:00 Mar. 22 Tuesday 2016
To whom: Hyun-Taek Lee, [email protected]
END OF
LECTURE 2-1

You might also like