Top Banner
Welcome to Programming World! C Programming Topic: Keywords and Identifiers Keywords are predefined, reserved words used in programming that have a special meaning. Keywords are part of the syntax and they cannot be used as an identifier. For example: int money; Here, int is a keyword that indicates 'money' is a variable of type integer. Keywords List: Samsil Arefin
39

C programming

Jan 22, 2017

Download

Engineering

Samsil Arefin
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: C programming

Welcome to Programming World!

C Programming

Topic: Keywords and Identifiers

Keywords are predefined, reserved words used in programming that have a

special meaning. Keywords are part of the syntax and they cannot be used as an

identifier.

For example: int money;

Here, int is a keyword that indicates 'money' is a variable of type integer.

Keywords List:

Samsil Arefin

Page 2: C programming

Identifiers : Identifier refers to name given to entities such as variables,

functions, structures etc.

int money;

double accountBalance;

Here, money and accountBalance are identifiers.

Topic: Constants and Variables

Page 3: C programming

In programming, a variable is a container (storage area) to hold data.

To indicate the storage area, each variable should be given a unique name

(identifier). Variable names are just the symbolic representation of a memory

location.

For example: int potato=20;

Here potato is a variable of integer type. The variable is assigned value: 20

Constants/Literals : A constant is a value or an identifier whose value cannot be altered in a program.

For example: const double PI = 3.14

Here, PI is a constant. Basically what it means is that, PI and 3.14 is same for this

program.

Page 4: C programming

Escape Sequences :

Page 5: C programming

Data Types :

Page 6: C programming

Programming Operators :

Increment and decrement operators: C programming has two operators increment ++ and decrement -- to change the

value of an operand (constant or variable) by 1.

Increment ++ increases the value by 1 whereas decrement -- decreases the value

by 1. These two operators are unary operators, meaning they only operate on a

single operand.

Page 7: C programming

#include <stdio.h>

int main()

{

int a = 10, b = 100;

float c = 10.5, d = 100.5;

printf("++a = %d \n", ++a);

printf("--b = %d \n", --b);

printf("++c = %f \n", ++c);

printf("--d = %f \n", --d);

return 0;

}

Output:

++a = 11

--b = 99

++c = 11.500000

++d = 99.500000

Page 8: C programming

Ternary Operator (?:) :

conditionalExpression ? expression1 : expression2

The conditional operator works as follows:

1.The first expression conditionalExpression is evaluated at first. This expression

evaluates to 1 if it's and evaluates to 0 if it's false.

2.If conditionalExpression is true, expression1 is evaluated.

3.If conditionalExpression is false, expression2 is evaluated.

For example:

#include<stdio.h>

int main(){

int a=10,b=15;

// If test condition (a >b) is true, max equal to 10.

// If test condition (a>b) is false,max equal to 15.

int max=(a>b)?a:b;

printf("%d",max);

return 0;

}

Page 9: C programming

Topic: if, if...else and Nested if...else Statement

if statement :

if(boolean_expression) {

/* statement(s) will execute if the boolean expression is true */

}

Samsil Arefin

Page 10: C programming

Example :

#include <stdio.h>

int main () {/* local variable definition */

int a = 10;

/* check the boolean condition using if statement */

if( a < 20 ) {/* if condition is true then print the following */

printf("a is less than 20\n" );

}

printf("value of a is : %d\n", a); return 0; }

Output:

a is less than 20

Page 11: C programming

value of a is : 10

If...else if...else Statement : if(boolean_expression 1) {

/* Executes when the boolean expression 1 is true */

}

else if( boolean_expression 2) {

/* Executes when the boolean expression 2 is true */

}

else if( boolean_expression 3) {

/* Executes when the boolean expression 3 is true */

}

else {

/* executes when the none of the above condition is true */

}

#include <stdio.h>

void main () { int a = 100; /* local variable definition */

/* check the boolean condition */

if( a == 10 ) {

/* if condition is true then print the following */

printf("Value of a is 10\n" );

}

else if( a == 20 ) {

Page 12: C programming

/* if else if condition is true */

printf("Value of a is 20\n" );

}

else if( a == 30 ) {

/* if else if condition is true */

printf("Value of a is 30\n" );

}

else {

/* if none of the conditions is true */

printf("None of the values is matching\n" ); } }

Topic: Switch statement

Page 13: C programming

switch (n)

{

case constant1:

// code to be executed if n is equal to constant1;

break;

case constant2:

// code to be executed if n is equal to constant2; break;

.

.

.

default:

// code to be executed if n doesn't match any constant

}

Samsil Arefin

Page 14: C programming
Page 15: C programming

For example :

#include <stdio.h>

void main () {

char grade = 'B'; /* local variable definition */

switch(grade) {

case 'A' :

printf("Excellent!\n" ); break;

case 'B' :

printf("Well done\n" ); break;

case 'C' :

printf("You passed\n" ); break;

case 'D' :

printf("Better try again\n" ); break;

default : printf("Invalid grade\n" ); } }

Topic : LOOP

There are three loops in C programming:

1.for loop

2.while loop

3.do..while loop

Samsil Arefin

Page 16: C programming

For loop :

Example :

#include <stdio.h>

void main () { int a;

/* for loop execution */

for( a =0; a <10; a++ ){

Page 17: C programming

printf("value of a: %d\n", a); } }

Output:

value of a: 0

value of a: 1

value of a: 2

value of a: 3

value of a: 4

value of a: 5

value of a: 6

value of a: 7

value of a: 8

value of a: 9

while loop : while(condition) {

statement(s); }

Example:

#include <stdio.h>

Void main () {

/* local variable definition */

Page 18: C programming

int a = 10;

/* while loop execution */

while( a < 20 ) {

printf("value of a: %d\n", a);

a++;

} }

Output same.

DO..while loop : do {

statement(s);

} while( condition );

Page 19: C programming

Example:

#include <stdio.h>

Void main () {

/* local variable definition */

int a = 10;

/* do loop execution */

do {

printf("value of a: %d\n", a);

a++;

}while( a < 20 ); }

Page 20: C programming

Output same.

Nested for loop : for (initialization; condition; increment/decrement)

{

statement(s);

for (initialization; condition; increment/decrement)

{

statement(s);

... ... ...

}

... ... ...

}

Page 21: C programming

Samsil Arefin

#include <stdio.h>

int main()

{

int i,j;

Page 22: C programming

for( i=1;i<=5;i++)

{

for( j=1;j<=i;j++)

{

printf("%d ",j);

}

printf("\n");

}

return 0;

}

Output:

1

1 2

1 2 3

1 2 3 4

1 2 3 4 5

Nested while loop :

Page 23: C programming

#include <stdio.h>

int main()

{

int i=1,j;

while (i <= 5)

{

j=1;

while (j <= i )

{

printf("%d ",j);

j++;

Page 24: C programming

}

printf("\n");

i++;

}

return 0;

}

Output:

1

1 2

1 2 3

1 2 3 4

1 2 3 4 5

Samsil Arefin

Page 25: C programming

break statement :

Example :

#include <stdio.h>

int main () {

/* local variable definition */

Page 26: C programming

int a = 10;

/* while loop execution */

while( a < 20 ) {

printf("value of a: %d\n", a);

a++;

if( a > 15) {

/* terminate the loop using break statement */

break;

}

}

return 0;

}

Result:

value of a: 10

value of a: 11

value of a: 12

Page 27: C programming

value of a: 13

value of a: 14

value of a: 15

continue Statement :

Example:

#include <stdio.h>

int main()

{ int i;

for( i=0;i<=8;i++)

{

if(i==5) continue;

printf("value of i: %d\n", i);

}

return 0;

}

Output:

value of i: 0

value of i: 1

value of i: 2

value of i: 3

Page 28: C programming

value of i: 4

value of i: 6

value of i: 7

value of i: 8

Topic: Function

Page 29: C programming

Example #1: No arguments passed and no return Value

#include <stdio.h>

void add()

{

int a=10,b=20,c;

c=a+b;

printf("C= %d",c);

Page 30: C programming

// return type of the function is void becuase no value is returned from the

function

}

int main()

{

add(); // no argument is passed to add()

return 0;

}

Output: c= 30

Example #2: No arguments passed but a return value

#include <stdio.h>

int get()

{

int n=10,m=10;

return m+n; //m+n return value

}

void main()

{

int c;

Page 31: C programming

c= get(); // no argument is passed to the function

// the value returned from the function is assigned to c

printf("C= %d",c);

}

Output: C= 20

Example #3: Argument passed but no return value

#include <stdio.h>

// void indicates that no value is returned from the function

void get(int c)

{

printf("C= %d",c) ;

}

int main()

{

int a=10,b=10,c;

c=a+b;

get(c); // c is passed to the function

return 0;

}

Page 32: C programming

Example #4: Argument passed and a return value

#include <stdio.h>

/* function declaration */

int max(int num1, int num2);

int main () {

/* local variable definition */

int a = 100;

int b = 200;

int ret;

// a,b are passed to the checkPrimeNumber() function

// the value returned from the function is assigned to ret variable

ret = max(a, b);

printf( "Max value is : %d\n", ret );

return 0;

Page 33: C programming

}

/* function returning the max between two numbers */

int max(int num1, int num2) {

/* local variable declaration */

int result;

if (num1 > num2)

result = num1;

else

result = num2;

return result;

}

Pass by Value: In this parameter passing method, values of actual

parameters are copied to function’s formal parameters and the two

types of parameters are stored in different memory locations. So any

changes made inside functions are not reflected in actual parameters

of caller.

Page 34: C programming

#include <stdio.h>

void fun(int x)

{

x = 30;

}

int main(void)

{

int x = 20;

fun(x);

printf("x = %d", x);

return 0;

}

Output: x = 20

Pass by Reference Both actual and formal parameters refer to same locations, so any changes made inside the function are actually reflected in actual parameters of caller. # include <stdio.h> void fun(int *ptr) { *ptr = 30; } int main() { int x = 20; fun(&x); printf("x = %d", x); return 0; } x = 30

Page 35: C programming

Topic: Recursion

A function that calls itself is known as a recursive function. And, this technique is known as recursion. The recursion continues until some condition is met to prevent it. To prevent infinite recursion, if...else statement (or similar approach) can be used where one branch makes the recursive call and other doesn't

Page 36: C programming

Example: Sum of Natural Numbers Using Recursion #include <stdio.h> int sum(int n); int main() { int number, result; printf("Enter a positive integer: "); scanf("%d", &number); result = sum(number); printf("sum=%d", result); } int sum(int num) { if (num!=0) return num + sum(num-1); // sum() function calls itself else return num; } Output : Enter a positive integer: 3 sum=6

Page 37: C programming

Samsil Arefin

Page 38: C programming

Exercise 1) Write a program and call it calc.c which is the basic calculator and receives three values from input via keyboard. The first value as an operator (Op1) should be a char type and one of (+, -, *, /, s) characters with the following meanings: o ‘+’ for addition (num1 + num2) o ‘-’ for subtraction (num1 - num2) o ‘*’ for multiplication (num1 * num2) o ‘/’ for division (num1 / num2) o ‘s’ for swap *Program should receive another two operands (Num1, Num2) which could be float or integer . *The program should apply the first given operator (Op1) into the operands (Num1, Num2) and prints the relevant results with related messages in the screen. * Swap operator exchanges the content (swap) of two variables, for this task you are not allowed to use any further variables (You should use just two variables to swap). Exercise 2) Write a C program and call it sortcheck.cpp which receives 10 numbers from input and checks whether these numbers are in ascending order or not. You are not allowed to use arrays. You should not define more than three variables.

Page 39: C programming

e.g Welcome to the program written by Your Name: Please enter 10 Numbers: 12 17 23 197 246 356 790 876 909 987 Fine, numbers are in ascending order. Exercise 3) Write a C program to calculates the following equation for entered numbers (n, x). 1+ (nx/1!) - (n(n-1)x^2/2!)...................................... Exercise 4) Write the C program for processing of the students structure. Define the array of a structure called students including following fields: * “First name” * “Family Name” * “Matriculation Number” You should first get the number of students from input and ask user to initialize the fields of the structure for the entered amount of students. Then delete the students with the same “Matriculation Number” and sort the list based on “Family Name” and print the final result in the screen.

Samsil Arefin 161-15-7197