CS140: Intro to CS An Overview of Programming in C by Erin Chambers.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

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. A Simple Program: Print a Line of Text // My First C++ Program #include int main( ) { cout
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.
1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)
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.
Basic Elements of C++ Chapter 2.
Computer Science 210 Computer Organization Introduction to C.
CECS 121 EXAM 1. /* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
High-Level Programming Languages: C++
Introduction to “Programming in C”
Programming in Python Part I Dr. Fatma Cemile Serçe Atılım University
1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1) Linda M c Iver.
Computer Science 101 Introduction to Programming.
CHAPTER 2 PART #3 INPUT - OUTPUT 1 st semester King Saud University College of Applied studies and Community Service Csc
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.
/* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
CHAPTER 2 PART #1 C++ PROGRAM STRUCTURE 1 st semester H 1 King Saud University College of Applied studies and Community Service Csc 1101 By:
A First C Program /* Print a Message */ #include main() { printf("This is a test!\n"); } The program is compiled and run as cc pg32.c  a.out  This is.
NA2204.1jcmt CSE 1320 Intermediate Programming C Program Basics Structure of a program and a function type name (parameters) { /* declarations */ statement;
CECS 121 Test 1. Functions allow you to group program statements under one name C and C++ are case-sensitive so main(), MAIN(), and Main() are all different.
CS140: Intro to CS An Overview of Programming in C (part 3) by Erin Chambers.
Chapter 2 part #1 C++ Program Structure
CS140: Intro to CS An Overview of Programming in C by Erin Chambers.
Khalid Rasheed Shaikh Computer Programming Theory 1.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
Computer Programming A simple example /* HelloWorld: A simple C program */ #include int main (void) { printf (“Hello world!\n”); return.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Objective Write simple computer program in C++ Use simple Output statements Become familiar with fundamental data types.
NOTE: C programs consist of functions one of which must be main. C programs consist of functions one of which must be main. Every C program begins executing.
From Algorithms to Programs Both are sets of instructions on how to do a task Algorithm: –talking to humans, easy to understand –in plain (English) language.
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.
Basic concepts of C++ Presented by Prof. Satyajit De
Chapter 1.2 Introduction to C++ Programming
CSCE 206 Structured Programming in C
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Computer Science 210 Computer Organization
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
CSE 220 – C Programming C Fundamentals.
Chapter 2 - Introduction to C Programming
Decisions Chapter 4.
Chapter 2 part #1 C++ Program Structure
Basic Elements of C++.
Chapter 2 - Introduction to C Programming
Introduction to C Programming
Basic Elements of C++ Chapter 2.
Computer Science 210 Computer Organization
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Introduction to C Topics Compilation Using the gcc Compiler
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
Chapter 2 - Introduction to C Programming
Lecture3.
C Programming Getting started Variables Basic C operators Conditionals
Introduction to C Topics Compilation Using the gcc Compiler
Programs written in C and C++ can run on many different computers
Chapter 2 - Introduction to C Programming
An Overview of C.
DATA TYPES There are four basic data types associated with variables:
Introduction to C Topics Compilation Using the gcc Compiler
Introduction to C Programming
Chapter 1 c++ structure C++ Input / Output
Chapter 2 part #1 C++ Program Structure
Presentation transcript:

CS140: Intro to CS An Overview of Programming in C by Erin Chambers

Getting started ● Please log in to the computer, click on the startup menu (located in bottom left corner) ● Select “Utilities” -> “Kate” to open a text editor

A First Program #include main(void) { printf(“Hello, World!\n”); return 0; }

First Program – Line by line <-- Loads standard input/output options #include main(void) { printf(“Hello, World!\n”) return 0; } <-- starts the program: void means that there is no return value or input parameters <-- prints a string, which is in between quotation marks <-- marks end of program

Running our program ● We need to compile our C program using the compiler called cc ● The compiler then outputs an executable file which will run our program, usually called a.out ● To run it, open up a Konsole (the little black screen icon at the bottom), type “cc filename.c”, then type “./a.out” at a command prompt

Remember data types? ● Numbers: int, float ● Strings: str ● Characters: char ● Booleans: bool

Other data types: a simple example #include main(void) { int x, y, divide, remainder; x = 10; y = 13; divide = x / y; remainder = x % y; printf(“The quotient is %d, and the remainder is %d \n”, divide, remainder); return 0; }

Printing the value of a variable ● %d – tells the compiler that we will be printing an integer in this spot, which will appear after the comma ● Other possibilities – %c for a character – %f for a floating point number

Floats #include main(void) { float x, y, divide; x = 10; y = 13; divide = x / y; printf(“%f divided by %f is %f”, x, y, divide); return 0; }

Input #include main(void) { int x, y, divide, remainder; printf(“Enter two integers: “) scanf(“%d%d”, &x, &y); divide = x / y; remainder = x % y; printf(“The quotient is %d, and the remainder is %d \n”, divide, remainder); return 0; }

If statements ● For more control, might want to specify some code be executed if some condition is true ● Use the if-else statement and boolean expressions ● Boolean expressions in C:, =, ==, != ● ! for 'not', && for 'and', || for 'or' ● Syntax: if (boolean expression) { statements } else { statements }

If statements – an example #include main(void) { int x, y, z; printf(“Enter two integers: \n”); scanf(“%d%d”, &x, &y); if (x > y) max = x; else max = y; printf(“The maximum is %d \n”, max); return 0; }

More complicated if statements if (x > y) if (x > z) max = x; else max = z; else if (y > z) max = y; else max = z;

Loops ● While loops are a way of performing the same code multiple times ● While the boolean expression evaluates to true, the code in the while loop will be performed; as soon as it is false, the while loop ends and the program continues

While loop - example #include main(void) { int i = 10; while (i >0) { printf(“T minus %d and counting\n”, i); i = i – 1; }

Another while loop #include main(void) { int number, sum; printf(“Enter a number:”); scanf(“%d”, &number); while (number != 0) { sum = sum + number; printf(“Enter another number, or 0 to stop:”); } printf(“The sum is %d\n”, sum); return 0; }

Comments in a program ● In C, any line beginning with // will be ignored by the compiler. ● Since code can be difficult to decipher, this provides a great way to comment your code, or describe what each line does.

A program with comments // Written by Erin Chambers #include main(void) { //print my first message printf(“Hello, World!\n”); return 0; }