You are on page 1of 22

C Programming – Lecture I

Instructor Özgür ZEYDAN

C programming language
The C programming language is incredibly popular.
C has been around for several decades and has won
widespread acceptance because it gives
programmers maximum control and efficiency.
Many other programming languages like C++, Java
and Python were developed using C. Moreover,
many modern operating systems are written in C.
The simplest way to define C is to call it a computer
programming language, meaning you can write
software with it that a computer can execute. That
means that you can use C to create lists of
instructions for a computer to follow.

Brief History of C Language
The language C was developed in the early
1970s at Bell Laboratories, primarily credited
to the work of Ken Thompson and Dennis
Ritchie.
Programmers needed a more user-friendly set
of instructions for the UNIX operating system,
which at the time required programs written in
assembly language.
By 1973, C was stable enough that UNIX itself
could be rewritten using this innovative new
higher-level language.

Structure of a C program

C Compiler: Borland C++ Creating a new file .

Your First C Program Your First C Program #include <stdio.h> #include <conio. return 0. getch(). } .h> main() { printf("hello civil engineers\n").

Compiling your first program Project > Compile (ALT + F9) Compile Screen Running your first program Debug > Run (CTRL + F9) .

Output Screen #include<stdio. C has a large number of standard libraries like stdio. and so on. write output to the screen. It is an extremely useful library. process text files stored on the disk. time and math libraries. The standard I/O library lets you: read input from the keyboard.h> This line includes the "standard I/O library" into your program.h> This C program starts with #include <stdio. . including string.

Every C program must have a function named main somewhere in the code.Headers . At run time. In C.h> printf() → output command scanf() → input command #include<conio.h files are called header files #include<stdio.h> getch() → reads key (program waits until key pressed) clrscr() → clears output screen main() function The line int main() declares the main function. the {and } symbols mark the beginning and end of a block of code. program execution starts at the first line of the main function. .

The return 0. The portion in quotes is called the format string and describes how the data is to be formatted when printed. . line causes the function to return an error code of 0 (no error) to the shell that started execution. return 0. the screen). The format string can contain: string literals such as “Hello civil engineers” symbols for carriage returns (\n) operators as placeholders for variables.printf() The printf statement in C allows you to send output to standard out (for us.

clrscr(). getch(). Comments are useful for a variety of reasons. Best programmers comment as they write the code.h> #include <conio.Modifying first program /* Programmer : Özgür ZEYDAN */ #include <stdio. .h> main() { printf("hello civil engineers\n"). Primarily they serve as internal documentation for program structure and functionality. getch(). not after the fact. } Comments /* Programmer : Özgür ZEYDAN */ Comments can be inserted into C programs by bracketing text with the /* and */ delimiters. printf("\nhello civil engineers").

" A variable has a name (in this case. This line says. You can use the value in b by saying something like: printf("%d". you will frequently want your program to "remember" a value.Variables As a programmer. You can not use Reserved Words as variable names. The way your program remembers things is by using variables. b) and a type (in this case. you will want to remember it somewhere so you can use it later. C is a case sensitive language. For example: int b. It means variables SUM is different than Sum or sum. or if it calculates a value. "I want to create a space called b that is able to hold one integer value. . if your program requests a value from the user. You can store a value in b by saying something like: b = 5. an integer). int. Variable names can not start with digits or special characters. Variable Names Variable names starts with a letter or underscore “_” and followed by either letters or digits or underscore “_”. b). For example.

b = 7.h> #include <conio. getch(). c). c = a + b. c. printf("%d + %d = %d\n". b. return 0. } .1 #include <stdio.h> int main() { int a. a = 5.Reserved Words Keywords auto double int struct break case char const continue default do else enum extern float for goto if long register return short signed sizeof static switch typedef union unsigned void volatile while Example . b. a.

and at the end of the printf line there are the three variable names: a.1 The line int a. . It matches the second %d with b and substitutes 7. Then it prints the completed line to the screen: 5 + 7 = 12. c. The next line initializes the variable named a to the value 5. Integer variables hold whole numbers. declares three integer variables named a. b. The next line adds a and b and "assigns" the result to c.Explanation of Example . The next line sets b to 7. C matches up the first %d with a and substitutes 5 there. The printf statement then prints the line "5 + 7 = 12." Placeholders The %d placeholders in the printf statement act as placeholders for values. b and c. It matches the third %d with c and substitutes 12. b and c. There are three %d placeholders.

The program always use a = 5 and b = 7. b and c Display “write a number” Read a Display “write a number” Read b Calculate c = a+b Display “c = a+b” Stop . user can not change the values of a and b.2 In previous example. Let us write a better program: Programs asks user to write first number and then initialize the value of a to that number. Finally program displays the output. Then program calculates c as c = a + b. Programs asks user to write first number and then initialize the value of b to that number. Example – 2 (Pseudocode) Start Variables: a.Example .

c. printf("Enter the second value:"). b. getch(). scanf("%d".h> #include <conio. a. c). c = a + b. &a). printf("Enter the first value:").h> int main() { int a. &b). scanf("%d".Example – 2 (C code) #include <stdio. printf("%d + %d = %d\n". return 0. b. } Example – 2 .

Also note the & in front of a and b. This is the address operator in C: It returns the address of the variable. as well as structure types. If you leave out the & operator. int. You must use the & operator in scanf on any variable of type char. Placeholders You can print all of the normal C types with printf by using different placeholders: int (integer values) uses %d float (floating point values) uses %f double (decimal values) uses %f char (single character values) uses %c character strings (arrays of characters) use %s double (decimal values) uses %lf in scanf . which for us is generally the keyboard. or float. Note that scanf uses the same sort of format string as printf . you will get an error when you run the program.Scanf() The scanf function allows you to accept input from standard in.

c). &a. b. c = a + b. Make all necessary changes. .3 Write a C program that gives following output screen.Example – 2 (Better C code) #include <stdio. &b).h> int main() { int a. printf("Enter two values (press spacebar in between): "). b. printf("%d + %d = %d\n".h> #include<conio. } Example . getch(). scanf("%d %d". return 0. a. c.

b.%d = %0. return 0. b. printf("Enter two values (press spacebar in between): ").0f \n". c = a . printf(“%5d”. → → _15 ___15 → → 5682 __5682 .h> #include <conio. a.0f \n". b=5682. &a. c).h> int main() { int a. a. scanf("%d %d". } Formatting Outputs Integers: %nd n: minimum width on the output where value is printed on the right. c). b. a. b. c = (double) a / b. printf(“%2d”.b.b). c = a * b. printf("%d . c). getch(). printf("%d + %d = %0. a=15. printf("%d * %d = %0. &b). c = a + b.a). printf(“%6d”.2f \n".3 #include <stdio.a). b. printf("%d / %d = %4. a.Example .0f \n". printf(“%3d”. c).b). double c.

&me).c).6480 _________ 1 5 . return(0).2f”.h> main() { char me[20]. printf(“%9.648.me).4f”. → __15.4 #include<stdio. printf("Please write your name: "). printf("Nice to meet you.h> #include<conio. } . getch().c). 65 -____ Example . → printf(“%4.Formatting Outputs Doubles: %n.mf n: minimum total width of the output m: exact width for the output of part after decimal point c=15. %s". scanf("%s".

. + or - Addition Subtraction Evaluated last. *. . they are evaluated left to right. Now. they are evaluated left to right. If there are several pairs of parentheses “on the same level” (i. not nested). write a C code of that program that converts F value. If there are several. We have written an algorithm and drawn a flowchart for celcius to fahrenhayt conversion program. the expression in the innermost pair is evaluated first. If there are several. /.e.5 Remember from previous week. Example . they are Modulus evaluated left to right. to C value.Arithmetic Operators and Precedence C o p e ra tio n Arithm e tic o p e ra to r Alg eb ra ic exp ressio n C e xp ressio n Addition + f+7 f + 7 Subtraction * p–c p .c b * m Multiplication Division Modulus / bm x/y % r mod s x / y r % s Operator(s) Operation(s) Order of evaluation (precedence) () Parentheses Evaluated first. which is given by user. If the parentheses are nested. or % Multiplication Division Evaluated second.

2.1f". getch().h> main() { float c. f: fahrenhayt */ printf("Fahrenhayt to celcius conversion program.5 #include <stdio.f.Example . c=(f-32)*5/9.\n"). printf("Write F value : "). scanf("%f". /* c: celcius. Write a C program that converts centimeters to inches and feet. printf("\C value is %4. Write a C program that converts gallons to liters and cubic meters. } Homework 1.h> #include <conio.c). return(0).&f). .

6 Write a C program that calculates area and circumference of a circle whose radius is given by the user.1415962 A = π * r2 C = 2* π * r Example .&r). printf("This program calculates area and circumference of a circle."). c=2*PI*r.6 #include<stdio. return(0).c).h> #define PI 3. printf("Radius : ").Example .h> #include<conio.1415962 main() { int r. printf("\nArea: %f. You can define pi by writing this line immediately after header lines: #define PI 3. } . double a. a=PI*r*r. getch().a. scanf("%d".c. Circumference: %f".

7 Write a program that asks user to write radius (r) and height (h) of a cylinder and then calculates: Volume ( V = πr2h ) Side area ( 2πrh ) Total area ( 2πr2 + 2πrh ) .Example .