You are on page 1of 71

Fundamentals of C-Programming Introduction to C Programming Integrated Development Environment (IDE) Structure of C Programming Elements of Style Variables in C Control Structure

ucture Loop Structure

Computer Programming -- is often called coding. It is the process of writing, testing, debugging and maintaining the source code of computer programs.

Computer Program is known as software program. It is a structured collection of instructions to the computer.

Computer Programmer is a professional who writes programs.

It is a software application that provides comprehensive facilities to computer programmers for software development. An IDE normally consists of a source code editor, a compiler and/or interpreter, build automation tools, and usually a debugger.

This window contains the interface 'Projects' which will in the following text be referred to as the project view. This view show all the projects opened in CodeBlocks at a certain time. The 'Symbols' tab of the Management window shows symbols and variables.

A part of the environme nt where we can write the codes

It is a computer program that translates text written in a computer language (the source language) into another computer language (the target language)

It

is a computer program that is used to test and debug other programs

Preprocessor directive main() { Variables Statements }

Head Body

Preprocessor Commands Functions Variables Statements & Expressions Comments

These commands tells the compiler to do preprocessing before doing actual compilation.

#include <stdio.h> int main() { double C, F; /*read in value for C*/ printf(Enter Celsius temp: ); scanf(%lf,&C); /* Calculate F */ F = (9/5) * C + 32; /* Display the value of F */ printf(The equivalent Fahrenheit is %lf \n, F); return 0; }

are main building blocks of any C Program

#include <stdio.h> int main() { double C, F; /*read in value for C*/ printf(Enter Celsius temp: ); scanf(%lf,&C); /* Calculate F */ F = (9/5) * C + 32; /* Display the value of F */ printf(The equivalent Fahrenheit is %lf \n, F); return 0; }

are used to hold numbers, strings and complex data for manipulation

#include <stdio.h> int main() { double C, F; /*read in value for C*/ printf(Enter Celsius temp: ); scanf(%lf,&C); /* Calculate F */ F = (9/5) * C + 32; /* Display the value of F */ printf(The equivalent Fahrenheit is %lf \n, F); return 0; }

Expressions combine variables and constants to create new values. Statements are expressions, assignments, function calls, or control flow statements which make up C programs.

#include <stdio.h> int main() { double C, F; /*read in value for C*/ printf(Enter Celsius temp: ); scanf(%lf,&C); /* Calculate F */ F = (9/5) * C + 32; /* Display the value of F */ printf(The equivalent Fahrenheit is %lf \n, F); return 0; }

Doing it the right way...

Write one statement per line. Put spaces before and after each arithmetic operator, just like you put spaces between words when you write.

/* Poor programming practice */ biggest=-l;first=0;count=57;init_key_words(); If (debug)open_log_files();table_size=parse_size+lexsize; /* Better programming practice*/ biggest=-l; first=0; count=57; init_key_words(); if(debug) open_log_files(); table_size=parse_size + lex_size;

Change a long, complex statement into several smaller, simpler statements.

/* This is a big mess */ gain = (old_value - new_value) /(total_old - total_new) * 100.0;

/* Good practice */ delta_value = (old_value - new_value); delta_total = (total_old - total_new); gain = delta_value / delta_total * 100.0;

Declaration and Data Types

Naming

Variables Declaring Variables Using Variables The Assignment Statement

Variables
Example

use represent some unknown value


: Perimeter = 2L + 2W;

Variables

in programming can be represented containing multiple characters

Example: full_name = first_name + last_name;

Variable names in C
May only consist of letters, digits, and underscores May be as long as you like, but only the first 31 characters are significant May not begin with a number May not be a C reserved word (keyword)

auto case const default double enum float goto

break char continue do else extern for if

int register return short sizeof struct switch typedef unsigned volatile

long

signed static

union void while

Begin variable names with lowercase letters Use meaningful identifiers Separate words within identifiers with underscores or mixed upper and lower case.
Examples: surfaceArea surface_Area surface_area

Be consistent!

Use all uppercase for symbolic constants (used in #define preprocessor directives). Examples:

#define PI 3.14159 #define AGE 52

C is case sensitive
Example: area Area AREA ArEa

All are seen as different variables by the compiler.

Syntax : <data_type> variable_name; Example: int num_of_meatballs; float area;

Integers Floating

(whole numbers) point (real numbers)

int, long int, short int, unsigned int float, double char

Characters

Data Type char


int float double

Number Minimum Value of Bytes 1 -128


4 4 8 -2147483648 1.17549435e-38

Maximum Value 127


2147483647 3.40282347e+38

2.2250738580720 1.7976931348623 14e-308 157e+308

Format %c %i or %d %f %lf %.2f

Description Single character Integer or decimal Number Floating point number or real Double Floating point number with 2 decimal places

Example A 1024 3.145


Long range of floating value

3.10

Examples:

int length = 7 ; float diameter = 5.9 ; char initial = A ; double cash = 1058635.5285

Good Programming Practice

a comment is always a good idea Example: int height = 4; // rectangle height int width = 6 ; // rectangle width int area ; // rectangle area

Place each variable declaration on its own line with a descriptive comment. Example : float inches ; // number of inches deep

Place a comment before each logical chunk of code describing what it does.
/* Get the depth in fathoms from the user*/ printf(Enter the depth in fathoms: );

Example :

Do

not place a comment on the same line as code (with the exception of variable declarations).
blank lines to enhance readability.

Use

Use

spaces around all arithmetic and assignment operators.


Examples: feet = 6 * fathoms ; a = b + c;

#include <stdio.h> int main( ) { float inches ; float feet ; float fathoms ;

// number of inches deep // number of feet deep // number of fathoms deep

// Get the depth in fathoms from the user

printf (Enter the depth in fathoms : ) ; scanf (%f, &fathoms) ;


// Convert the depth to inches feet = 6 * fathoms ; inches = 12 * feet ; . . . return 0; }

Assignment

statement is represented by an equal sign (=)


Examples:

diameter = 5.9 ; area = length * width ;

#include <stdio.h> int main( ) { int inches, feet, fathoms ; fathoms = 7 ; feet = 6 * fathoms ; inches = 12 * feet ;
. . .

It is a statement for choosing two alternative action


Conditional Expression T Execute S_1

Basic Syntax: if (conditional expr) statement_1; else statement_2;

F Execute S_2

Write a program that reads in an integer and then display whether the number is positive or negative.

Write a program that reads weight in pounds and output the weight in kilograms. Moreover, if the weight in kilogram exceeds 60 kilos, then display the message GO ON DIET, else YOU ARE IN GOOD SHAPE

Used to check the value of a certain item that may have more than one outcome

Basic Syntax:

if (conditional expr) statement_1; else if (conditional expr) statement_2; else if (conditional expr) statement_3; else if (conditional expr) statement_n;

Write a program that reads in an integer and then display whether the number is positive or negative or neither when the user enters 0.

Write a program that reads the students average grade in Programming and then output his grade standing according to the following scheme
GRADE STANDING F C B A

AVERAGE GRADE Below 75 At least 75 but below 85 At least 85 but below 95 At least 95

Elementary Arithmetic Operations 1. Addition 2. Subtraction 3. Multiplication 4. Division Input your choice (1-4): The user then asked to type two integers and the correct answer. Your program should also inform the user whether the answer is correct or not.

Write a program to display a menu system for an elementary arithmetic operation and then ask the user to select the operation he or she wants

Basic Syntax:
switch (expression) { case constant 1; statement 1; statement 2; statement n; break; case constant 2;
statement 1; statement 2; statement n; break;

Allows to execute a series of statement based on the value of an expression

case constant n;
statement 1; statement 2; statement n; break;

default: statement; }

Create a Number Base converter program that allows the user to select from the four options given
NUMBER BASE CONVERTER 1. Decimal to Hexadecimal 2. Hexadecimal to Decimal 3. Decimal to Octal 4. Octal to Decimal Input your choice (1-4):

Write a program that reads the students average grade in Programming and then output his grade standing according to the following scheme
GRADE STANDING F C B A

AVERAGE GRADE Below 75 At least 75 but below 85 At least 85 but below 95 At least 95

It does not use a counter variable to loop a number of times

The general from of while initialize loop counter; while (test loop counter using a condition) { statement_1; statement_2; increment loop counter; }

Write a program that reads in an integer and then calculate the sum of the positive integers from 1 to N.

Write a program that reads in an integer and then count the number of positive and negative numbers

The The general from of dowhile conditional expression is do { placed at the statement_1; end of the statement_2; loop statement_2; } while (conditional expression);

Write a program that output all even numbers between 1 to 15.

Write a program that reads in 10 positive integers and then calculate its sum.

It lets you execute a group of statements a certain number of times

The general from of for loop


for( initialization; test counter; increment counter) { statement_1; statement_2; statement_n; }

Create a program that displays a multiplication table of 5.

Sample Output
5*1=5 5*2=10 5*3=15 5*10=50

Modify the sample problem so that it will ask the user for the number to multiply and the number of terms.

You might also like