Presentation is loading. Please wait.

Presentation is loading. Please wait.

C programming.

Similar presentations


Presentation on theme: "C programming."— Presentation transcript:

1 C programming

2 History C is a high-level programming language developed by Dennis Ritchie in 1972. He named it C because there was an existing programming language called B.

3 Learning C C programs need to be compiled and turned into machine (binary) code before the program can be executed . To learn C you must PRACTICE!!!! Find a C compiler.

4 Stages of compilation Source code Lexical analysis Preprocessing
Parsing Code generation Object code Linking Executable file

5 Why C ? One of the most popular programming languages.
One of the most powerful programming languages. Other languages like C++, Java, Perl and even JavaScript and Flash ActionScript are all based on C in terms of the way we write the code.

6 Syntax The C code you write is called the SOURCE CODE or the SYNTAX.
Syntax is a mixture of: C keywords like int, for and return. • Constants and variables.• Operators like + (addition), || (or), etc. Note that C is CASE SENSITIVE! For example, words like cat, Cat, cAt and CAT are all considered different from one another. Also, the amount of WHITE SPACE you use in a C program does not affect the way it's compiled. Use extra spaces and line breaks to make your programs more readable - indentation of code is very common. Obviously, you can NOT put spaces or line breaks in the middle of keywords like this: str uct !!

7 Creating Executable Programs
There are several tasks that need to be performed before you can run a program: coding, compiling linking. You have to write the SOURCE CODE in C. You declare which header files you want to include within the source code. The source code must be saved with the extension .c. Then you run the COMPILER, which translates the source code into machine code. This produces output in a format that the computer can understand. LINKER basically "links" the source code to other library or object files so that the final program is produced.

8 Your First Program #include <stdio.h> int main() {
printf("Hello World!\n"); return (0); }

9 #include When the preprocessor finds #include it looks for the file specified and replaces #include with the contents of that file. This makes the code more readable and easier to maintain if you needed to use common library functions. #include <stdio.h> “stdio.h” is the file name. This type of files are called header file.

10 The main Function All C programs must have a main function. You can only have one, but you can place it anywhere within the code. The program always start with the main function and ends when the end of main is reached. The main function is special, as it returns an integer by default, which is why you'll see return 0; at the end of the program. Zero is usually returned to indicate error-free function termination.

11 The main Function int main() { printf("Hello World!\n"); return 0; }

12 Statement The entire line including keywords, arguments, parentheses, and a semicolon (;) is called a statement. Each statement must end with a semicolon.

13 printf It prints the given arguments to the screen. It is not Printf
It is printf printf(“Hello world”); printf(“COMPE 111 Introduction to Computer Engineering”);

14 Special Characters \n newline. Cursor goes to next line.
\t horizontal tab. Cursor moves to the next tab. \r carriage return. Cursor moves to the beginning of the same line. \a alert. It produces a sound. \\ write backslash. \” write double quote.

15 Write program Welcome To C
Write a program to write the text given above using 3 printf statement. Write a program to write the text given above using 1 printf statement.

16 Variables Variables are like containers in your computer's memory - you can store values in them and retrieve or modify them when necessary. To INITIALIZE a variable means to store a value in it for the first time, which is done using the ASSIGNMENT OPEARTOR, like this: x = 2

17 Variables RAM number1 5 number2 10 sum 15

18 Assignment Putting a value to a variable. number = 25; sum = 23 + 56;
number = number + 1;

19 Naming Constants and Variables
Names... Example CANNOT start with a number 2i CAN contain a number elsewhere h2o CANNOT contain any arithmetic operators... r*s+t CANNOT contain any other punctuation marks... CAN contain or begin with an underscore _height_ CANNOT be a C keyword struct CANNOT contain a space im stupid CAN be of mixed cases XSquared

20 An Introduction to the 4 Data Types
In C, there are four basic DATA TYPES: int, char, float, double. Each one has its own properties. For instance, they all have different sizes. We must give each variable a data type to allow and restrict the type of data we can assign to it.

21 printf (some more) Printing constant and variables: int number;
Printf(“Number is %d \n”, number); Argument 1: “Number is %d \n”, format control string Argument 2: number, the value to be printed %d indicates that an integer will be printed.

22 printf (some more) What is the output of the following statement?
int number1, number2; number1=5; number2=8; printf(“number 1 : %d \n number 2 : %d \n”, number1, number2);

23 The int Data Type #include <stdio.h> int main() { int a,b,c,d,e;
d = 'A'; e = ; printf("a = %d\n", a); printf("b = %d\n", b); printf("c = %d\n", c); printf("d = %d\n", d); printf("e = %d\n", e); printf("b+c = %d\n", b+c); return 0; }

24 The int Data Type The output of the example is:
a = 10 b = 4 c = 4 d = 65 e = 9 b+c = 8

25 scanf It is used to get a value from user (keyboard).
scanf(“%d”, &number1); Argument 1: format control string (an integer number will be read). Argument 2: address of a variable Result of the scanf statement: number1 = 3; & is the address operator. &number1 means the address of the variable number1.

26 scanf Input more than one variable scanf(“%d %d”, &number1, &number2);
Use scanf together with printf to make your program more user fiendly. printf(“Enter a number : ”); scanf(“%d”, &number);

27 Practice Write a program which Inputs two integer numbers
Adds these two numbers Writes the result to the screen

28 practice /* Addition program */ #include <stdio.h> Main() {
int number1, number2, sum; printf(“Enter first integer : ”); scanf(“%d”, &number1); printf(“Enter second integer : ”); scanf(“%d”, &number2); sum = number1 + number2; printf(“Sum is %d \n”, sum); return 0; }

29 Programming guidelines
Use newline (\n) when necessary. Not good printf(“First number : %d”, number1); printf(“Second number : %d”, number2); Good printf(“First number : %d \n”, number1); Use indentation.

30 Programming guidelines
Use indentation. Main() { printf(“welcome”); printf(“to C! \n”); }

31 Programming guidelines
Put an explanation at the beginning of each function. /* First program in C */ Main() { printf(“welcome”); printf(“to C! \n”); }

32 Programming guidelines
Place a space after each comma to make programs more readable. Use meaningful variable and constant names (total, average, sum, etc.) Combine multiple-word variables like “total_commission” or “totalCommision” Start with a lowercase letter to a variable name.

33 Programming guidelines
Do not forget that C is a case sensitive language. Do not place variable declarations among executable statements. Separate the declarations and executable statements with a blank line. Place spaces on either side of an operator. sum = number1 + number2; sum=number1+number2;

34 Arithmetic operations in C
operation operator example Addition a + b, Subtraction a – b, – 7 Multiplication * a * b, * 7 Division / a / b, 45 / 7 Modulus/remainder % a % b, 45 % 7

35 Arithmetic operations in C
Integer division int result; result = 17 / 5; result = 3 result = 7 / 4; result = 1 Modulus /remainder result = 17 % 5; result = 2 result = 7 % 4; result = 3

36 Order of evaluation (Precedence)
operation operator Parentheses () Multiplication, division *, /, % remainder Addition, subtraction +, -

37 Order of evaluation (Precedence)
Evalaute the order of the following operations a * (b + c) + c * (d + e) y = a * x * b + c * d + e g = a + b / c + d % e – (f + g) + a * b / c

38 Algorithm (example) get (GPA) if (GPA > 3.5) then put (status)
status <- “full scholarship” else if (GPA > 3.0) then status <- “half scholarship” else status <- “no scholarship” put (status)

39 Decision get (GPA) yes no yes no put (status) GPA>3.5 GPA>3.0
Status <-”full-scholarship” GPA>3.0 yes Status <-”half-scholarship” no Status <-“no-scholarship” put (status)

40 IF Structure Single selection structure if (condition) statement;
Single selection structure with single statement if (grade > 60) printf(“passed”);

41 IF Structure Single selection structure with multiple statements
if (grade > 60) { printf(“Passed \n”); printf(“Congratulations!”); }

42 IF Structure Double selections structure with single statement
if (grade > 60) printf (“Passed”); else printf (“Failed”);

43 IF Structure Double selections structure with multiple statements
if (grade > 60){ printf (“Passed”); printf(“Congratulations!”); } Else { printf (“Failed”); printf (“Sorry”);

44 IF Structure Multiple selections if (grade > 90) printf (“AA”);
else if (grade > 80) printf (“BB”); else if (grade > 70) printf (“CC”); else printf(“DD”);

45 Relational operators x < y Is x less than y?
x<= y Is x less than y or equal to y? x > y Is x greater than y? x >= y Is x greater than y or equal to y? x != y Is x unequal to y? x ==y Is x equal to y?

46 Logical operators AND && a && b OR || a || b NOT ! !(a) Examples:
if (a > 5 && b < 10) printf(“condition is satisfied”);

47 Logical operators Examples: if (a > 5 && b < 10)
printf(“condition is satisfied”); if ( number1 == 5 || number1 == 6) printf (“condition is satisfied”); if (!(number != 0)) printf (“Successful”);

48 Precedence of operators
() ! * / % + - <, >, >=, <= == != && || =

49 Loop total <- 0 number <- 0 no yes put (status) Number<5
get (grade) total <- total + grade number <- number + 1 average <-total / number put (status)

50 “for” repetition structure
Format: for (counter = 1; counter <= 10; counter = counter + 1) printf(“%d \n”, counter); Increment of control variable Execution condition keyword Control variable initialization

51 “for” repetition structure
Write a program which Gets a decimal value from user Does the following actions 10 times: Adds one to the number, Writes the result on the screen

52 “for” repetition structure
#include <stdio.h> int main() { int number, i; printf(“Enter a decimal number :”); scanf(“%d”, &number); for (i=1; i <= 10; i = i + 1) { number = number + 1; printf(“%d \n”, number); } return 0;

53 “for” repetition structure
Write a program which does the following operations 5 times: Gets two decimal number from user Calculates the remainder by dividing the first number by the second number.

54 “for” repetition structure
#include <stdio.h> int main() { int num1, num2, i; for (i=1; i <= 5; i = i + 1) { printf(“Enter first number :”); scanf(“%d”, &num1); printf(“Enter second number :”); scanf(“%d”, &num2); printf(“%d \n”, num1 % num2); } return 0;


Download ppt "C programming."

Similar presentations


Ads by Google