Seminar 1 - Week 1_oop
Seminar 1 - Week 1_oop
Contents
1. Objectives.............................................................................................................................................. 1
2. Basic C notions ...................................................................................................................................... 1
The form of a C program ........................................................................................................................... 1
Data types ................................................................................................................................................. 2
Variable declarations and initialization..................................................................................................... 2
Input and output functions ....................................................................................................................... 2
3. Simple programs ................................................................................................................................... 2
4. Procedural programming in C - Functions ............................................................................................ 3
1. OBJECTIVES
- get familiar with the C programming language
- solve simple problems
- procedural programming in C
2. BASIC C NOTIONS
pre-processor directives
global declarations
T f1()
{
local variables’ declarations;
C statements;
}
T f2()
{
local variables’ declarations;
C statements;
}
……
int main()
{
local variables’ declarations;
C statements;
return 0;
}
Pre-processor directives:
- the preprocessor is a separate program invoked by the compiler as the first part of the
translation
- always begin with the hash symbol “#” ()
- tell the preprocessor to perform specific actions:
o replace tokens in the text (#define)
o insert the contents of other files into the source file (#include)
o control compilation of portions of a source file (#ifdef, #ifndef, #else, #endif)
DATA TYPES
- int, float, double, char, void
3. SIMPLE PROGRAMS
1. Write a program which prints the sum of two given integer numbers.
#include <stdio.h>
int main()
{
int m = 0;
int n = 0;
int sum = 0;
sum = m + n;
printf("The total is: %d.\n", sum);
return 0;
}
2. Write a program which asks for your name and surname. The application will print a greeting
containing your entire name, as well as the number of characters your entire name contains.
int main()
{
char surname[50];
char firstName[100];
printf("Enter your first name: ");
scanf("%49s", firstName);
firstName[0] = toupper(firstName[0]);
return 0;
}
1. Create a C function that receives as input 2 integer numbers and returns their sum and their
product.
Observation: In C, to return more than one value from a function one should use pointers, passed as
arguments to the function. Please see below.
/*
Computes the sum and product of 2 given numbers.
Input: a, b - integer numbers
Output: s, p - integer numbers representing the sum and product of a and b.
*/
void sumAndProduct(int a, int b, int* s, int* p)
{
*s = a + b;
*p = a * b;
}
int main()
{
int a = 0;
int b = 0;
int s = 0;
int p = 1;
sumAndProduct(a, b, &s, &p);
printf("The sum is: %d and the product is: %d.\n", s, p);
system("pause");
return 0;
}
Each requirement must be resolved using at least one function. All functions need to be specified.