• No results found

Hands on of C Language Programming

N/A
N/A
Protected

Academic year: 2021

Share "Hands on of C Language Programming"

Copied!
56
0
0

Loading.... (view fulltext now)

Full text

(1)

Ponta Grossa, Paraná - Brazil

Hands on of C Language Programming

February 3, 2021 Max Mauro Dias Santos

(2)

Motivation

1941 - 2011

Dennis Ritchie – Father of C and UNIX

He created the C programming language and, with long-time colleague Ken Thompson, the Unix operating system and B programming language. Ritchie and Thompson were awarded the Turing Award from the ACM in 1983, the Hamming Medal from the IEEE in 1990 and the National Medal of Technology from President Bill Clinton in 1999. Ritchie was the head of Lucent Technologies System Software Research Department when he retired in 2007. He was the "R" in K&R C, and commonly known by his username dmr.

https://en.wikipedia.org/wiki/Dennis_Ritchie

C is a general-purpose, procedural computer programming

language supporting structured programming, lexical variable

scope, and recursion, with a static type system. By design, C

provides constructs that map efficiently to typical machine

instructions. It has found lasting use in applications previously

coded in assembly language. Such applications include operating

systems

and

various

application

software

for

computer

architectures that range from supercomputers to PLCs and

embedded systems.

(3)

Motivation

(4)
(5)

Embedded Programmig Languages

Differences Between BIN, DAT and HEX Files

Here are some brief details about these files :

.BIN File

: The BIN file type is primarily associated with 'Binary File'. Binary files are used for a wide variety

of content and can be associated with a great many different programs. In general, a .BIN file will look like

garbage when viewed in a file editor.

.DAT File

: The DAT file type is primarily associated with 'Data'. Can be just about anything: text, graphic,

or general binary data. Data file in special format or ASCII.

.HEX File

: The HEX file type is the format used to store machine language code in hexadecimal form. It is

commonly used for programming microcontrollers, EPROMs, and other types of programmable logic

devices.

So in a nutshell, .BIN file is binary file which contains binary data, .DAT file is Data File which can contain

any type of data including binary data and .HEX file is the files which stores the data in hexadecimal form.

(6)

GCC – GNU Compiler Collection

The Operating System

(7)

GCC – GNU Compiler Collection

The GNU Compiler Collection (GCC) is an optimizing compiler produced by the GNU Project

supporting various programming languages, hardware architectures and operating systems.

https://en.wikipedia.org/wiki/GNU_Compiler_Collection

GCC is the GNU Compiler Collection that

provides the compilers for the following

programming languages:

 C

 C++

 Objective C

 Fortran

 ADA

 Java

Basic steps in compilation

 Pre-Process directives like #include

 Compilation of the C Code to generate the

assembly

 Assembly Code to object file generation

 Link the object code to generate the

executable

https://www.isical.ac.in/~pdslab/2015/

The original GNU C Compiler (GCC) is developed by Richard

Stallman, the founder of the GNU Project. Richard Stallman

founded the GNU project in 1984 to create a complete Unix-like

operating system as free software, to promote freedom and

(8)

GCC – GNU Compiler Collection

From compiling to run the program

Step 1: Compiling a simple C Program

Step 2: Compiling a simple C++ Program

Step 3: Providing the executable name

Step 4: Compiling a simple multi-file program

Step 5: Multi-step compilation and linking

Step 6: Including files from other directories

Step 7: Linking with external libraries

Step 8: Linking to a library at non-standard path

(9)

GCC – GNU Compiler Collection

Difference Between C and C++

As we know both C and C++ are programming languages and used for application development. The main difference

between both these languages is C is a procedural programming language and does not support classes and objects,

while C++ is a combination of both procedural and object-oriented programming languages.

Sr. No. Key C C++

1 Introduction C was developed by Dennis Ritchie in around 1969 at AT&T Bell Labs. C++ was developed by Bjarne Stroustrup in 1979. 2

Language Type As mentioned before C is procedural programming.

On the other hand, C++ supports both

procedural and object-oriented programming paradigms.

3

OOPs feature Support

As C does not support the OOPs concept so it has no support for polymorphism, encapsulation, and inheritance.

C++ has support for polymorphism,

encapsulation, and inheritance as it is being an object-oriented programming language

4

Data Security As C does not support encapsulation so data behave as a free entity and can be manipulated by outside code.

On another hand in the case of C++

encapsulation hides the data to ensure that data structures and operators are used as intended.

5 Driven type C in general known as function-driven language. On the other hand, C++ is known as object driven language.

6

Feature supported

C does not support function and operator overloading also do not have namespace feature and reference variable functionality.

On the other hand, C++ supports both function and operator overloading also have namespace feature and reference variable functionality.

(10)

GCC – GNU Compiler Collection

Difference Between C and C++

(11)

GCC – GNU Compiler Collection

Step 1: Compiling a simple C Program

 Syntax: gcc <filename.c>

 Output: An executable called a.out

 To run: ./a.out

user@ws$ gcc HelloWorld.c

user@ws$ ./a.out

Hello World

(12)

GCC – GNU Compiler Collection

Step 2: Compiling a simple C++ Program

 Syntax: g++ <filename.cpp>

 Output: An executable called a.out

 To run: ./a.out

user@ws$ g++ HelloWorld.cpp

user@ws$ ./a.out

Hello World

(13)

GCC – GNU Compiler Collection

Step 3: Providing the executable name

 Extra option: -o

 Syntax: gcc <filename.c> -o <outputname>

 Output: An executable called outputname

 To run: ./outputname

user@ws$ gcc HelloWorld.c -o myapp

user@ws$ ./myapp

Hello World

(14)

GCC – GNU Compiler Collection

The Multi-File Programs

Why create multi-file programs?

 Manageability

 Modularity

 Re-usability

 Abstraction

Components:

 Header files

 Implementation Source files

 Application source file (contains the main function)

(15)

GCC – GNU Compiler Collection

Header Files

Contents:

 Pre-processor directives and macros

 Constant declarations

 Type declarations (enum, typedef, struct, union, etc)

 Function prototype declarations

 Global variable declarations

 May also contain static function definitions

Example: HelloWorld.h

#ifndef _HELLOWORLD_H_

#define _HELLOWORLD_H_

typedef unsigned int my_uint_t;

extern void printHelloWorld();

extern int iMyGlobalVar;

...

#endif

Union is an user defined datatype in C

programming language.

typedef is used to define new data type

names to make a program more readable

to the programmer.

In C programming, an enumeration type

(also called enum) is a data type that

consists of integral constants.

In C programming, a struct (or structure) is

a collection of variables (can be of different

types) under a single name.

(16)

GCC – GNU Compiler Collection

Implementation Source Files

Contents:

 Function body for functions declared in corresponding header files

 Statically defined and inlined functions

 Global variable definitions

Example: HelloWorld.c

#include <stdio.h>

#include

“HelloWorld.h”

int iMyGlobalVar;

void print HelloWorld()

{

iMyGlobalVar = 20;

printf(“Hello World\n”);

return;

}

Global variables are declared outside

any function, and they can be accessed

(used)

on

any

function

in

the

program. Local variables are declared

inside a function, and can be used only

inside that function.

(17)

GCC – GNU Compiler Collection

Application Source Files

Contents:

 Function body for the main function

 Acts as client for the different modules

Example: app.c

#include <stdio.h>

#include

“HelloWorld.h”

int main()

{

iMyGlobalVar = 10;

printf(“%d\n”, iMyGlobalVar);

printHelloWorld();

printf(“%d\n”, iMyGlobalVar);

return 0;

}

int main

– 'int main' means that our function needs

to return some integer at the end of the execution

and we do so by returning 0 at the end of

the program. 0 is the standard for the

“successful

execution of the program”

(18)

GCC – GNU Compiler Collection

(19)

GCC – GNU Compiler Collection

Step 4: Compiling a simple multi-file program

 Syntax: gcc <file1.c> <file2.c> ... -o filename

 Example:

user@ws$ gcc HelloWorld.c app.c -o my_app

user@sw$ ./my_app

10

Hello World

20

(20)

GCC – GNU Compiler Collection

Step 5: Multi-step compilation and linking

Steps:

 Compile source files to object files

 Link multiple object files into executables

Compilation to object files

 Special Option: -c

 Syntax: gcc -c <filename(s).c>

Link multiple files into the final executables

 Syntax: gcc <filename1.o> <filename2.o> [-o output]

 Example:

user@ws$ gcc -c HelloWorld.c

user@ws$ gcc -c app.c

user@ws$ gcc HelloWorld.o app.o -o my_app

user@sw$ ./my_app

10

Hello World

20

(21)

GCC – GNU Compiler Collection

Step 6: Including files from other directories

 Special Option: -I<directory_name>

 Syntax:

gcc -I<directory1> -I<directory2> <filename(s).c>

 Example:

user@ws$ cd HelloWorld/src

user@ws$ gcc -c -I../../HelloWorld/include HelloWorld.c

user@ws$ cd ../../app/src

user@ws$ gcc -c -I../../HelloWorld/include main.c

user@ws$ cd ../../

user@ws$ gcc HelloWorld/src/HelloWorld.o app/src/main.o -o

my_app

(22)

GCC – GNU Compiler Collection

Object Libraries

 Libraries contain pre-compiled object codes are of 2 types:

Statically Linked: Object codes are linked into and placed inside the executable during compilation.

Name format: lib<name>.a

Dynamically Linked: Object code is loaded dynamically into memory at runtime.

Name format: lib<name>.so

Statically Linked Libraries

 Consists of a set of routines which are copied into

a target application

 An archive of object files

 Object code corresponding to the required functions

are copied directly into the executable

 Library format is dependent on linkers

 Increases executable size

Dynamically Linked Libraries

 Contains position independent code for different

functions

 Executable code is loaded by the loader at runtime

 The symbol table in the library contains blank

addresses which are filled up later by the loader

 Increases reuse

(23)

GCC – GNU Compiler Collection

Step 7: Linking with external libraries

 Static linking: Link to libname.a

Special option: -static and -l

Syntax: gcc -static <filename(s)> -lname

 Dynamic linking: Link to libname.so

Special option: -l

Syntax: gcc <filename(s)> -lname

user@ws$ gcc -static math_test.c -lm

(24)

GCC – GNU Compiler Collection

Step 8: Linking to a library at non-standard path

 Special option: -L<path>

 Syntax:

gcc <filename> -l<name> -L<path>

(25)

GCC – GNU Compiler Collection

Step 9: Building Static libraries

Required external tools: ar, ranlib

1. Create object files for the source codes

2. User ar to create the archives with names of the form: lib<libraryname>.a

3. Use ranlib to generate the index within the library

user@ws$ gcc -c HelloWorld.c

user@ws$ ar rcs libHW.a HelloWorld.o

user@ws$ ranlib libHW.a

user@ws$ gcc app.c -lHW -L. -o my_app

user@ws$ ./my_app

10

Hello World

20

(26)

GCC – GNU Compiler Collection

Step 10: Building Dynamically linked libraries

Required external tools: ar, ranlib

Requirements: Object code needs to be position independent

Steps:

1.Compile sources in a Position Independent manner

Option: -fPIC

2. Combine objects to create shared library:

Option: -shared -W1, -soname,lib<name>.so.<version>

LD_LIBRARY_PATH

 Set to the directory containing the .so file

user@ws$ gcc -c -fPIC HelloWorld.c

user@ws$ gcc -shared -W1,

-soname,libHW.so.1 -o libHW.so

HelloWorld.o

user@ws$ gcc app.c -lHW -L. -o my_app

user@ws$ export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH

user@ws$ ./my_app

Hello World

20

(27)

GCC – GNU Compiler Collection

Some more details about linking

If -static is specified, linking is always static

 if lib<name>.a is not found gcc errors out

Otherwise

 If lib<name>.so is found linking is dynamic  otherwise linking is static

In case of dynamic linking, lib<name>.so should be placed in a standard location, otherwise LD_LIBRARY_PATH

needs to be set

(28)

GCC – GNU Compiler Collection

(29)

GCC – GNU Compiler Collection

Example of Compiler Warnings

Special option: -Wall

Syntax : gcc -Wall ....

user@ws$ gcc warning.c

user@ws$ gcc warning.c -Wall

warning.c: In function

‘main’:

warning.c:8: warning: suggest explicit braces to avoid

ambiguous

‘else’

(30)

Hands on of C Language Programming

Program to Display "Hello, World!“

Example 1: C Output

Example 2: Integer Output

Example 3: float and double Output

Example 4: Print Characters

Example 5: Integer Input/Output

Example 6: Float and Double Input/Output

Example 7: C Character I/O

Example 8: ASCII Value

helloworld.c

coutput.c

integeroutput.c

floatdouble.c

printcharacter.c

integerio.c

floatdoubleio.c

ccharacterio.c

integerio.c

(31)

Hands on of C Language Programming

Program to Display "Hello, World!"

Answer

helloworld.c

The #include is a preprocessor command that tells the compiler to include the contents of stdio.h (standard input and output) file in the program. The stdio.h file contains functions such as scanf() and printf() to take input and display output respectively.

If you use the printf() function without writing #include <stdio.h>, the program will not compile.

The execution of a C program starts from the main() function.

printf() is a library function to send formatted output to the screen. In this

program, printf() displays Hello, World! text on the screen.

The return 0; statement is the "Exit status" of the program. In simple terms, the program ends with this statement.

#include <stdio.h> int main() {

// printf() displays the string inside quotation printf("Hello, World!");

return 0; }

Output

(32)

Hands on of C Language Programming

Example 1: C Output

coutput.c

#include <stdio.h> int main() {

// Displays the string inside quotations printf("C Programming");

return 0; }

Example 2: Integer Output

#include <stdio.h> int main() { int testInteger = 5; printf("Number = %d", testInteger); return 0; }

integeroutput.c

Example 3: float and double Output

floatdouble.c

#include <stdio.h> int main() { float number1 = 13.5; double number2 = 12.4; printf("number1 = %f\n", number1); printf("number2 = %lf", number2); return 0; }

(33)

Hands on of C Language Programming

Example 4: Print Characters

printcharacter.c

#include <stdio.h> int main() { char chr = 'a'; printf("character = %c", chr); return 0; }

Example 5: Integer Input/Output

#include <stdio.h> int main() { int testInteger; printf("Enter an integer: "); scanf("%d", &testInteger); printf("Number = %d",testInteger); return 0;

integerio.c

Example 6: Float and Double Input/Output

floatdoubleio.c

#include <stdio.h> int main() { float num1; double num2; printf("Enter a number: "); scanf("%f", &num1);

printf("Enter another number: "); scanf("%lf", &num2);

printf("num1 = %f\n", num1); printf("num2 = %lf", num2);

return 0; }

(34)

Hands on of C Language Programming

Example 6: Float and Double Input/Output

floatdoubleio.c

#include <stdio.h> int main() { float num1; double num2; printf("Enter a number: "); scanf("%f", &num1);

printf("Enter another number: "); scanf("%lf", &num2);

printf("num1 = %f\n", num1); printf("num2 = %lf", num2);

return 0; }

Example 6: Float and Double Input/Output

#include <stdio.h> int main()

{

float num1; double num2;

int sizenum1, sizenum2; printf("Enter a number: "); scanf("%f", &num1);

printf("Enter another number: "); scanf("%lf", &num2);

sizenum1 = sizeof(num1); sizenum1 = sizeof(num2); printf("num1 = %f\n", num1); printf("num2 = %lf\n", num2);

printf("Size of num1 = %d\n", sizenum1); printf("Size of num2 = %d", sizenum2); return 0;

(35)

Hands on of C Language Programming

Example 7: C Character I/O

ccharacterio.c

#include <stdio.h> int main() { char chr; printf("Enter a character: "); scanf("%c",&chr); printf("You entered %c.", chr); return 0; }

Example 8: ASCII Value

#include <stdio.h> int main() { char chr; printf("Enter a character: "); scanf("%c", &chr);

// When %c is used, a character is displayed printf("You entered %c.\n",chr);

// When %d is used, ASCII value is displayed printf("ASCII value is %d.", chr);

return 0; }

integerio.c

http://each.uspnet.usp.br/digiampietri/ACH2023/tabelasemc.html

(36)

Hands on of C Language Programming

Program to Display "Hello, World!"

Recommendation

helloworld.c

#include <stdio.h> int main() {

// printf() displays the string inside quotation printf("Hello, World!");

return 0; }

Output

Hello, World!

Try to install a certain GCC and execute compilation through DOS

window

(37)

Hands on of C Language Programming

Timing

How to measure time taken by a function in C?

CLOCKS_PER_SEC is a constant which is declared in <time.h>. To get the CPU time used by a task within a C application, use:

clock_t begin = clock();

/* here, do your time-consuming job */

clock_t end = clock();

double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;

#include <stdio.h> #include <stdlib.h> int main() { printf("Hello world!\n"); return 0; }

Check the difference of runtime for the code A and code B.

How do you instrument them to measure runtime?

#include <stdio.h> #include <stdlib.h> int main() { printf("Hello world!\n"); printf("\nPalmeiras!\n"); return 0; }

Code A

Code B

(38)

Hands on of C Language Programming

Time Delay in C

The time delay in C Code, consist to get current clock and add the required delay to that clock, till current clock is less

then required clock run an empty loop.

// C function showing how to do time delay #include <stdio.h>

// To use time library of C #include <time.h>

void delay(int number_of_seconds) {

// Converting time into milli_seconds

int milli_seconds = 1000 * number_of_seconds;

// Storing start time

clock_t start_time = clock();

// looping till required time is not achieved while (clock() < start_time + milli_seconds)

; }

// Driver code to test above function int main()

{

int i;

for (i = 0; i < 10; i++) { // delay of one second delay(1);

printf("%d seconds have passed\n", i + 1); }

return 0; }

(39)

Hands on of C Language Programming

printinteger.c

addtwointeger.c

multiplytwofloat.c

findascii.c

computequotient.c

sizevariable.c

swaptwonumberstemp.c

swaptwonumbersnotemp.c

integerio.c

Program to Print an Integer

Program to Add Two Integers

C Program to Multiply Two Floating-Point Numbers

C Program to Find ASCII Value of a Character

C Program to Compute Quotient and Remainder

C Program to Find the Size of int, float, double and char

C Program to Swap Two Numbers

C Program to Swap Two Numbers

swaptwonumberstemp.c

swaptwonumbersnotemp.c

Swap Numbers Using Temporary Variable

(40)

Hands on of C Language Programming

evenodd.c

evenoddternary.c

largestif.c

largestifladder.c

largestifnested.c

C Program to Check Whether a Number is Even or Odd

C Program to Check Whether a Number is Even or Odd

C Program to Find the Largest Number Among Three Numbers

C Program to Find the Largest Number Among Three Numbers

C Program to Find the Largest Number Among Three Numbers

Example 3: Using Nested if...else

Example 2: Using if...else Ladder

Example 1: Using if Statement

Program to Check Odd or Even

Using the Ternary Operator

(41)

Hands on of C Language Programming

Program to Print an Integer

#include <stdio.h> int main() {

int number;

printf("Enter an integer: ");

// reads and stores input scanf("%d", &number);

// displays output

printf("You entered: %d", number);

return 0; }

Output

Enter an integer: 25 You entered: 25

In this program, an integer variable number is declared.

int number;

Then, the user is asked to enter an integer number. This number

is stored in the number variable.

Finally, the value stored in number is displayed on the screen

using printf().

(42)

Hands on of C Language Programming

Program to Add Two Integers

#include <stdio.h> int main() {

int number1, number2, sum;

printf("Enter two integers: ");

scanf("%d %d", &number1, &number2);

// calculating sum

sum = number1 + number2;

printf("%d + %d = %d", number1, number2, sum); return 0;

}

Output

Enter two integers: 12 11

12 + 11 = 23

(43)

Hands on of C Language Programming

C Program to Multiply Two Floating-Point Numbers

#include <stdio.h> int main() {

double a, b, product;

printf("Enter two numbers: "); scanf("%lf %lf", &a, &b);

// Calculating product product = a * b;

// Result up to 2 decimal point is displayed using %.2lf printf("Product = %.2lf", product);

return 0; }

Output

Enter two numbers: 2.4 1.12

Product = 2.69

(44)

Hands on of C Language Programming

C Program to Find ASCII Value of a Character

#include <stdio.h> int main() {

char c;

printf("Enter a character: "); scanf("%c", &c);

// %d displays the integer value of a character // %c displays the actual character

printf("ASCII value of %c = %d", c, c); return 0; }

Output

Enter a character: G ASCII value of G = 71

findascii.c

(45)

Hands on of C Language Programming

C Program to Compute Quotient and Remainder

#include <stdio.h> int main() {

int dividend, divisor, quotient, remainder; printf("Enter dividend: ");

scanf("%d", &dividend); printf("Enter divisor: "); scanf("%d", &divisor);

// Computes quotient

quotient = dividend / divisor;

// Computes remainder

remainder = dividend % divisor;

printf("Quotient = %d\n", quotient); printf("Remainder = %d", remainder); return 0; }

Output

Enter dividend: 25 Enter divisor: 4 Quotient = 6 Remainder = 1

computequotient.c

(46)

Hands on of C Language Programming

C Program to Find the Size of int, float, double and char

#include<stdio.h> int main() { int intType; float floatType; double doubleType; char charType;

// sizeof evaluates the size of a variable

printf("Size of int: %zu bytes\n", sizeof(intType)); printf("Size of float: %zu bytes\n", sizeof(floatType)); printf("Size of double: %zu bytes\n", sizeof(doubleType)); printf("Size of char: %zu byte\n", sizeof(charType));

return 0; }

Output

Enter dividend: 25 Enter divisor: 4 Quotient = 6 Remainder = 1

sizevariable.c

(47)

Hands on of C Language Programming

C Program to Swap Two Numbers

#include<stdio.h> int main() {

double first, second, temp; printf("Enter first number: "); scanf("%lf", &first);

printf("Enter second number: "); scanf("%lf", &second);

// Value of first is assigned to temp temp = first;

// Value of second is assigned to first first = second;

// Value of temp (initial value of first) is assigned to second second = temp;

printf("\nAfter swapping, firstNumber = %.2lf\n", first); printf("After swapping, secondNumber = %.2lf", second); return 0;

Output

Enter first number: 1.20 Enter second number: 2.45

After swapping, firstNumber = 2.45 After swapping, secondNumber = 1.20

Swap Numbers Using Temporary Variable

(48)

Hands on of C Language Programming

C Program to Swap Two Numbers

#include <stdio.h> int main() { double a, b; printf("Enter a: "); scanf("%lf", &a); printf("Enter b: "); scanf("%lf", &b); // Swapping // a = (initial_a - initial_b) a = a - b;

// b = (initial_a - initial_b) + initial_b = initial_a b = a + b;

// a = initial_a - (initial_a - initial_b) = initial_b a = b - a;

printf("After swapping, a = %.2lf\n", a); printf("After swapping, b = %.2lf", b); return 0;

Output

Enter a: 10.25 Enter b: -12.5 After swapping, a = -12.50 After swapping, b = 10.25

Swap Numbers Without Using Temporary Variables

(49)

Hands on of C Language Programming

C Program to Check Whether a Number is Even or Odd

#include <stdio.h> int main() {

int num;

printf("Enter an integer: "); scanf("%d", &num);

// True if num is perfectly divisible by 2 if(num % 2 == 0)

printf("%d is even.", num); else

printf("%d is odd.", num);

return 0; }

Output

Enter an integer: -7 -7 is odd.

Program to Check Even or Odd

(50)

Hands on of C Language Programming

C Program to Check Whether a Number is Even or Odd

#include <stdio.h> int main() {

int num;

printf("Enter an integer: "); scanf("%d", &num);

(num % 2 == 0) ? printf("%d is even.", num) : printf("%d is odd.", num); return 0;

}

Output

Enter an integer: 33 33 is odd.

Program to Check Odd or Even Using

the Ternary Operator

(51)

Hands on of C Language Programming

C Program to Find the Largest Number Among Three Numbers

#include <stdio.h> int main() {

double n1, n2, n3;

printf("Enter three different numbers: "); scanf("%lf %lf %lf", &n1, &n2, &n3);

// if n1 is greater than both n2 and n3, n1 is the largest if (n1 >= n2 && n1 >= n3)

printf("%.2f is the largest number.", n1);

// if n2 is greater than both n1 and n3, n2 is the largest if (n2 >= n1 && n2 >= n3)

printf("%.2f is the largest number.", n2);

// if n3 is greater than both n1 and n2, n3 is the largest if (n3 >= n1 && n3 >= n2)

printf("%.2f is the largest number.", n3);

return 0; }

Output

Enter three numbers: -4.5 3.9

5.6

5.60 is the largest number.

Example 1: Using if Statement

(52)

Hands on of C Language Programming

C Program to Find the Largest Number Among Three Numbers

#include <stdio.h> int main() {

double n1, n2, n3;

printf("Enter three numbers: "); scanf("%lf %lf %lf", &n1, &n2, &n3);

// if n1 is greater than both n2 and n3, n1 is the largest if (n1 >= n2 && n1 >= n3)

printf("%.2lf is the largest number.", n1);

// if n2 is greater than both n1 and n3, n2 is the largest else if (n2 >= n1 && n2 >= n3)

printf("%.2lf is the largest number.", n2);

// if both above conditions are false, n3 is the largest else

printf("%.2lf is the largest number.", n3);

return 0;

Output

Enter three numbers: -4.5 3.9

5.6

5.60 is the largest number.

Example 2: Using if...else Ladder

(53)

Hands on of C Language Programming

C Program to Find the Largest Number Among Three Numbers

#include <stdio.h> int main() {

double n1, n2, n3;

printf("Enter three numbers: "); scanf("%lf %lf %lf", &n1, &n2, &n3); if (n1 >= n2) {

if (n1 >= n3)

printf("%.2lf is the largest number.", n1); else

printf("%.2lf is the largest number.", n3); } else {

if (n2 >= n3)

printf("%.2lf is the largest number.", n2); else

printf("%.2lf is the largest number.", n3); }

return 0; }

Output

Enter three numbers: -4.5 3.9

5.6

5.60 is the largest number.

Example 3: Using Nested if...else

(54)

Activity 3

This task is to be carried out in group of three students and delivery at February 17th, 2021.

Then, the activity 3 will be delivered to be carry out for a group of three students.

You are advised to

a) Compiling a simple multi-file program

b) Multi-step compilation and linking

c) Including files from other directories

d) Linking with external libraries

e) Linking to a library at non-standard path

f) Building Static libraries

g) Building Dynamically linked libraries

h) Create a simple program through CodeBlock using ARM Project and measure the size and execution

time. This exercise consist in build a .exe file.

i)

Instale um GCC e compile em Shell DOS usando linhas de comando como:

user@ws$ gcc HelloWorld.c

user@ws$ ./a.out

(55)

References

• https://www.programiz.com/c-programming

(56)

References

Related documents

SECTION 19-5-103, MISSISSIPPI CODE OF 1972, TO CONFORM THERETO; TO AMEND SECTION 97-29-31, MISSISSIPPI CODE OF 1972, TO PROVIDE THAT BREAST-FEEDING DOES NOT CONSTITUTE

 The Unexecuted Contract is NOT included as an exhibit to the Resolution because the Agreement(s) is with other another governmental agency and it is not feasible to obtain the

Like the channel axis and channel off-axis of the Ainsa I sandbody, the thick- bedded, coarse-grained sandstones of FA3e have a lower diversity and intensity trace- fossil

We identify the normal response of fiscal policy to a large military shock with our estimate of the dynamic response paths of government purchases, the government surplus and

Additionally, the data estimate of = 0 : 16 implies that if a country with an interest-rate spread of 10 percentage points (which is among the worst 5% of nations in terms of

In particular, the framework enables us to see how seller competition as well as the seller’s ability to purchase further verifiable information change the disclosure behavior

Sri Kila Vajra Kumara, a tutelary deity, appeared with wings at the ground floor of a house at a place called 'Kipri' (corrupted form of Yipri, which derives its name from the

In PHMM, CHMM a pair of labels is inferred representing Resident 1 activity label and Resident 2 activity label while in CL-HMM and LHMM a combined activity label