0% found this document useful (0 votes)
71 views23 pages

Coe 251 Unit 1

This document introduces the C programming language. It discusses what a program and algorithm are, and compares different programming languages. It also explains key concepts in C like variables, data types, constants, and input/output functions. The document provides examples of simple C programs to demonstrate basic syntax like declarations, assignments, comments, and output statements.

Uploaded by

Joseph A-c
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views23 pages

Coe 251 Unit 1

This document introduces the C programming language. It discusses what a program and algorithm are, and compares different programming languages. It also explains key concepts in C like variables, data types, constants, and input/output functions. The document provides examples of simple C programs to demonstrate basic syntax like declarations, assignments, comments, and output statements.

Uploaded by

Joseph A-c
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

COE 251

Introducing the C Programming


Language
UNIT 1

J. Yankey
[email protected]// 0264665015
Jan 2014
Introducing the C Programming
language
Programming and Problem Solving
-From Problem to Solution

• What is a
program?

• What is an
algorithm?
Programming Languages

• A vocabulary and set of grammatical rules for instructing a computer to perform


specific tasks.
• A program is written as a series of human understandable computer instructions
• A programming language is like any other language:
• You need to learn the grammatical rules (syntax)

• You need to learn the meaning of words and how to use them (keywords)

• You cannot learn all the words of a language at a go (I think).

• There are different ways of saying the same thing.

• You need to understand a language to act on what is being said. (compiler)


Programming Language levels of abstraction

FOURTH GENERATION LANGUAGE

HIGH-LEVEL LANGUAGE

LOW LEVEL LANGUAGE (ASSEMBLY)

MACHINE LANGUAGE (CPU Specific)

HARDWARE
• Why do we have to invent another language like C to learn
programming?

• Why not use a language we already know.


• Like Twi or Ga or Hausa
Comparing Programming Languages – September 2014
Popularity Job Vacancies Popularity and Jobs

1. C PHP Java

2. Java Java C language

3. Objective C Objective C C++

4. C++ SQL C#

5. C# Android (java) Objective C

6. PHP Ruby PHP

7. Visual Basic Javascript Python

8. Python C# Ruby

9. Perl C++

10. Ruby Action Script

11. Python

12. C
How does the computer know the rules of the
language?
• The Backus-Naur Form
• Sentence = Subject Verb Object
• Subject = Noun
• Object = noun
• Verb = love
• Verb = eat
• Noun = I
• Noun = Adwoa
• Noun = C Programming
• (Replace all non-terminals until we are left with only terminals)
Grammar for C expressions

• Expression = Expression operator Expression


• Expression = number

• …. (will continue in next 2 lessons)


Into the C language - A simple program
CODE BLOCK
#include <stdio.h>
main()
{
int num1, num2, num3; //declare variables
printf("Enter first number:");
scanf("%d", &num1);
printf("Enter second number:");
scanf("%d", &num2);
num3 = num1 + num2; //add the two numbers
/*show output to the user*/
printf("The sum of %d and %d is %d", num1, num2, num3);

}
('pagecreate', '#wrapper_question'

CODE BLOCK • A C program basically consists of the following


#include <stdio.h>
main() parts:
{ • Preprocessor Commands
int num1, num2, num3; //declare
variables • Functions
printf("Enter first number:");
scanf("%d", &num1); • Variables
printf("Enter second number:"); • Statements & Expressions
scanf("%d", &num2);
num3 = num1 + num2; //add the two • Comments
numbers
/*show output to the user*/ • Can you explain the code one line at a time?
printf("The sum of %d and %d is %d",
num1, num2, num3);
}
Identifiers
A C identifier is a name used to identify a variable, function, or any user-defined
item.
1. An identifier must start with a letter A to Z or a to z or an underscore _
2. Identifier can be followed by zero or more letters, underscores and digits (0-9)
3. C does not allow punctuation characters [@,#,%,&,…] within identifiers
4. C is a case sensitive programming language

cars zara abc new_data a_123 kofi50


j _temp cx200b retVal
2database cars@ 9_variable
Keywords

• The following list shows the reserved words in C. These reserved


words may not be used as constant or variable or any other identifier
names.
Variables

• A variable is a name given to a storage area that our programs can


manipulate.

• Each variable in C has a specific type which determines:


• The size and layout of the variable's memory

• The range of values that can be stored within that memory

• The set of operations that can be applied to the variable.


DATA TYPES AND VARIABLES
Data Types
Declarations and Assignments
• Every variable in a C program must be declared before it is used.
• When you declare a variable you are telling the compiler—and, ultimately, the
computer—what kind of data you will be storing in the variable.

SYNTAX
type_name variable_name_1, variable_name_2, . . .;

EXAMPLE
int count, numberOfStudents, number_of_people;
double average;

• Use simple but unambiguous names.


• Which is better: x = y * z or distance = speed * time?
• The most direct way to change the value of a variable is to use an
assignment statement.
• An assignment statement always consists of a variable on the left-
hand side of the equal sign and an expression on the right-hand side.
• The expression on the right-hand side of the equal sign may be a
variable, a number, or a more complicated expression.
Assignment Statements
In an assignment statement, first the expression on the right-hand side of the equal sign
is evaluated and then the variable on the left-hand side of the equal sign is set equal to
this value.
SYNTAX
Variable = Expression;
EXAMPLES
distance = rate * time;
count = count + 2;

• Assignments may be made during variable declarations.


• What is assignment compatibility?
Constants
• Constants (literals) refer to fixed values that the program may not alter during its
execution.
• There are two simple ways in C to define constants:
1. Using #define preprocessor.
2. Using const keyword.

SYNTAX
#define identifier value
const type variable = value;
EXAMPLE
#define LENGTH 10
const int LENGTH = 10;

• Why use a constant when you could use a variable?


Character and string literals
• Character literals are enclosed in single quotes, e.g., 'x' and can be
stored in a simple variable of char type.
• A character literal can be a plain character (e.g., 'x'), or an escape
sequence (e.g., '\t').
• Certain characters in C when preceded by a backslash will have
special meaning. Examples are the newline (\n) or tab (\t).
• String literals or constants are enclosed in double quotes "". A string
contains characters that are similar to character literals: plain
characters, escape sequences, etc.
WHAT IS THE OUTPUT?
1.
#include <stdio.h>
main()
{
printf("Hello\tWorld\n\n");
}

2.
#include <stdio.h>
#define LENGTH 10
#define WIDTH 5
#define NEWLINE '\n'
main()
{
int area;
area = LENGTH * WIDTH;
printf("value of area : %d", area);
printf("%c", NEWLINE);
}
WHAT IS THE OUTPUT?
3.
#include <stdio.h>
int main()
{
const int LENGTH = 10;
const int WIDTH = 5;
const char NEWLINE = '\n';
int area;

area = LENGTH * WIDTH;


printf("value of area : %d", area);
printf("%c", NEWLINE);

return 0;
}
Thank You

For any concerns, please contact


[email protected]
[email protected]
0322 191132
Jan 2014

You might also like