C programming.

Slides:



Advertisements
Similar presentations
Lecture 2 Introduction to C Programming
Advertisements

Introduction to C Programming
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
Chapter 2 Introduction to C Programming
C Programming Language 4 Developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories 4 Used to rewrite the UNIX operating system 4 Widely used on UNIX.
1 Lecture 2  Input-Process-Output  The Hello-world program  A Feet-to-inches program  Variables, expressions, assignments & initialization  printf()
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Introduction to Computers and Programming - Class 2 1 Introduction to Computers and Programming Class 2 Introduction to C Professor Avi Rosenfeld.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
C programming an Introduction. Types There are only a few basic data types in C. char a character int an integer, in the range -32,767 to 32,767 long.
Introduction to C Programming
Basic Input/Output and Variables Ethan Cerami New York
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Computer Science 210 Computer Organization Introduction to C.
Chapter 2 Getting Started in C Programming
A First Book of ANSI C Fourth Edition
Chapter 2 Introduction to C Programming Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked.
H.Melikian Introduction on C C is a high-level programming language that forms the basis to other programming languages such as C++, Perl and Java. It.
Sales person receive RM200/week plus 9% of their gross sales for that week. Write an algorithms to calculate the sales person’s earning from the input.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Programming With C.
CMSC 104, Version 9/011 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program 104 C Programming Standards and Indentation.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Introduction to C Programming Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
Week 1 Algorithmization and Programming Languages.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
C++ for Engineers and Scientists Second Edition
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 1 Scott Marino.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
CSCE 206 Structured Programming in C
Introduction to C Programming
Chapter 1: Introduction to computers and C++ Programming
Introduction to C++ Programming
Chapter 2 Introduction to C++ Programming
CSC201: Computer Programming
Chapter 2 - Introduction to C Programming
Revision Lecture
ICS103 Programming in C Lecture 3: Introduction to C (2)
Chapter 2 - Introduction to C Programming
Introduction to C Programming
Introduction to C Programming
Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Introduction to C++ Programming
Chapter 2 - Introduction to C Programming
Introduction to C Programming
Introduction to C Topics Compilation Using the gcc Compiler
Chapter 2 - Introduction to C Programming
Introduction to C Topics Compilation Using the gcc Compiler
Chapter 2 - Introduction to C Programming
Introduction to C Programming
Introduction to C Programming
Presentation transcript:

C programming

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.

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.

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

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.

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 !!

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.

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

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

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.

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

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

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

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.

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.

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

Variables RAM number1 5 number2 10 sum 15

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

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... #@x%£!!a 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

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.

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.

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

The int Data Type #include <stdio.h> int main() { int a,b,c,d,e; d = 'A'; e = 4.3 + 4.8; 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; }

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

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.

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

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

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

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.

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

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

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.

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;

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

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

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

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

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)

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)

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

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

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

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

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

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?

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

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

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

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

“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

“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

“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;

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

“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;