0% found this document useful (0 votes)
97 views15 pages

Munster Programming Training

C is a programming language that communicates instructions to computers to solve problems. It is simple, fast, and procedural. A C program consists of C routines including a main function. C uses keywords like int and if to define data types and statements. Variables are declared with names using letters, digits, and underscores. A basic C program includes header files, declares variables, reads inputs, performs calculations, generates output, and returns a value. C supports basic arithmetic operators and functions for input/output and mathematics. Variables are of built-in types like int and float and are declared before use. Conditional statements like if/else select between blocks of code.

Uploaded by

ruthereno
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
97 views15 pages

Munster Programming Training

C is a programming language that communicates instructions to computers to solve problems. It is simple, fast, and procedural. A C program consists of C routines including a main function. C uses keywords like int and if to define data types and statements. Variables are declared with names using letters, digits, and underscores. A basic C program includes header files, declares variables, reads inputs, performs calculations, generates output, and returns a value. C supports basic arithmetic operators and functions for input/output and mathematics. Variables are of built-in types like int and float and are declared before use. Conditional statements like if/else select between blocks of code.

Uploaded by

ruthereno
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd

Munster Programming Training

What is C?
C is a programming language.
- It communicates to the computers what to do. - It solves various problems from scientific to entertaining.

Why C?
- It is simple, fast, procedural etc. - It has several extensions C++, C# - It is the language of Sciences.

A C program is a sequence of C routines which must include main.

The words of C?
C programs communicate to the computer using words. C Key words are names for:
- Data types: void, int, long, double, float - Statements: if, while, for,

Programmers words / user words.


- Names for variable, routines.

Rules:
- User names use letters, digits, _. - C is case sensitive

Example: a, b, c, A, B1,C_1, max_two_numbers

What is a C program like?


Sections of C programs: 1. Include declarations 2. The header of main. 3. The block of main.
Declare all the variables. Read all the inputs. Generate the output. return 1;
# include <stdio.h> # include <math.h> int main(int args, char ** argc) { printf("Hello from C \n"); return 1; }

# include <stdio.h> # include <math.h> int main(int args, char ** argc) { // nothing in the program return 1; }

More on these examples


1.
-

Include declarations for the libraries we use.


stdio.h for input/output. math.h for maths functions. stdlib.h for standard c functions.

2.
-

In the main function


// are comments printf(); is a C function to print/display/write etc. scanf (); is a C function to read an input value. A sequence of characters between is a string.

Another program.
Three variables a, b, mean.
They are declared. The inputs a, b are read. The output mean is calculated and written.
# include <stdio.h> # include <math.h> int main(int args, char ** argc) { int a, b; double mean; printf("a="); scanf("%d", &a); printf("b="); scanf("%d", &b); mean=(a+b)/2.; printf("mean=%lf \n\n", mean); return 1; }

Each command ends with ;. Writing style:


indent your lines write several statement on the same line align brackets Finish each line with ;

Same rules as in Maths: +, -, /, *, () etc

Read and Write


printf(format, var) for writing scanf(format,&var) for reading printf(text %spec \n, var1); printf(text %s1 text %s2 text \n, var1,var2,); - format specificators: %d, %ld, %f, %lf. - \n special character for newline. scanf(%spec,&var); No text is allowed in the format.
printf("a="); scanf("%d", &a); printf(a=%d,a);

Read and Write - Examples


Read an int variable a. printf(a=); scanf(%d,&a); Write an int variable a. printf(a = %d \n, a);

Read two variables x and y. printf(Type the Values for x and y); scanf(%d %d,&x, &y); Write two int variables x,y. printf(a = %d b= %d \n, x, y);

C Data Types.
In Mathematics there are several sets of numbers: N, Z, Q, R. In C there are several data types (sets of C numbers):
Integers: char, short, int, long. Reals: float and double.

int -2^15, 2^15-1 unsigned int 0, 2^16 long -2^31, 2^31-1 unsigned short 0, 2^32 float 6 exact decimals double 10 exact decimals C literals: integer numbers: sequence_of_digits. real numbers: sequence_of_digits.sequence_of_digits characters: ch string: sequence_of_characters

C Declaration.
In a C program ALL VARIABLES MUST BE DECLARED once. Types of declarations:
type var; type var=value; type var1,var2,..;

Example:
int a,b,c; double x,y=0.; double x, a;

Declarations go in the first part of a block. ALL VARIABLES MUST BE READ/INITIALISED.

C Arithmetic
C variables form expressions using operators: Arithmetic operators:
+, -, *, / ++, -- for incrementing and decrementing. % is the remainder of the integer division.

Relational operators:
<, >, <=, >= ==, !=

Logical: &&=and, ||= or, !=not Assignment: = In addition of those operators there are several math routines in math.h sqrt, sin, cos, asin, acos etc.

Examples
int a,b,c; double x,y; a=1;b=2;c=4; a=b+c; // a=6 b=c+2; // b=6 a=a+1; // a increases with 1 ==> a++; x=PI;y=sin(x); x=asin(y/2); a=b=2; // initialise a and b with 2. a==b; // compare a and b.

C tests - if statement.
If statement chooses between two alternatives
if(test)statement1; else statement 2;

or chooses one alternative


if(test)statement1;

On the branches we have only one statement. If more use a block {} Example:
if(a<b) max=b; else max=a; if(a<b){max=b;min=a;} else {max=a;min=b;}

if(test) { statement1; } else { statement 2; } or chooses one alternative if(test) { statement1; }

Max of two numbers


# include <stdio.h> # include <math.h> # include <conio.h> int main(int args, char ** argc) { int a, b, max; printf("a="); scanf("%d", &a); printf("b="); scanf("%d", &b); if(a<b) { max=b; } else { max=a; } printf(max=%d \n\n", max); return 1; }

To do List
1. Read more about C types, declarations, operators from the e-tutorial. Solve the HW problems Visit the page of AISPC.

2. 3.

You might also like