ICS 2175 Lecture 4b Programming in C
ICS 2175 Lecture 4b Programming in C
programming
Getting Started: in C
Objective
• Intro to C
• Tools we will use
• Program file structure
• Variables
• Read from screen and print to screen
• Decisions (If)
C Orientation
• Created in 1972 to write operating systems (Unix
in particular)
– By Dennis Ritchie
– Bell Labs
• Evolved from B
• Can be portable to other hardware (with careful
design – use Plauger’s The Standard C Library
book)
• Built for performance and memory management –
operating systems, embedded systems, real-time
systems, communication systems
C Standardization
• 1989 ANSI and ISO -> Standard C
• 1999 C99
• 2011 C11
return 0; }
} }
C Program Phases
• Editor - code by programmer
• Compiling using gcc:
– Preprocess – expand the programmer’s code
– Compiler – create machine code for each file
– Linker – links with libraries and all compiled
objects to make executable
• Running the executable:
– Loader – puts the program in memory to run it
– CPU – runs the program instructions
Copyright © Pearson, Inc. 2013. All
Rights Reserved.
Copyright © Pearson, Inc. 2013. All
Rights Reserved.
Run First Program
• Write in notepad++
• Transfer with Filezilla
• Connect to panther as terminal (putty) using
SSH (Secure Shell)
• More filename to see the file
• gcc filename -o filename without c -g (ex:
gcc hello.c -o hello -g )
• ./hello
Using variables
#include <stdio.h>
int main(void)
{
int sum, value1, value2, value3;
float average;
value1 = 2;
value2 = 4;
value3 = 6;
sum = 2 + 4 + 6;
average = sum / 3;
printf("The average of %d , %d, %d is %f\n", value1,
value2, value3, average);
return(0);
}
int main(void)
{
int value1, value2, value3;
float sum, average;
comments
Character Data
• All of our programs so far have used
variables to store numbers, not words.
• We can store one or more characters by
writing:
char x, s[10];
– x can hold one and only one character
– s can hold up to nine characters (reserving 1 for
ending null)
• For now, we use character data for input and
output only.
A program that uses a character variable
#include <stdio.h>