C___11 (1)
C___11 (1)
Programming Languages
C Lecture 1 : 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
A First Program
makes input
and output available
#include <stdio.h>
to us
header
int main(void)
{
printf("This is my first C program.\n");
return(0);
}
Java Reminder
Program C Java
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
Average3.c
#include <stdio.h>
int main(void)
{
int value1, value2, value3;
float sum, average;
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>
Features so far
• Include
• Variable types: int, float, char
• Read using scanf
– requires & for address of variable being read
• Print using printf
• Format strings: %f (float), %d (int), %u
(unsigned int), %c (char), %s (character
array)
• Comments /*.. */ or //
if and if-else and if-else if - else
If (boolean_expression 1)
{ /statements }
else if ( boolean_expression 2)
{ /* statements */ }
else if ( boolean_expression 3)
{ /* statements */ }
else
{ /* statements */ }
IsItNeg.c – illustrate if
#include <stdio.h>
// Tell a user if a number is negative
int main(void)
{ float number;
/* Ask the user for a number */
printf("Please enter a number ? ");
scanf("%f", &number);
// Print whether the number is negative or not
if (number < 0){
printf("%f is a negative number\n", number); }
else {
printf("%f is NOT a negative number\n", number); }
return(0); }
Relational operators
Operator Meaning Example
== equals x == y
!= is not equal to 1 != 0
> greater than x+1 > y
< less than x-1 < 2*x
>= greater than or x+1 >= 0
equal to
<= less than or equal -x +7 <= 10
to
Integer Division
Declaring Constants
•The general form of the constant declaration is:
const datatype ConstantName =
ConstantValue,
AnotherConstantName =
AnotherConstantValue;