http://boredzo.org/pointers/
http://www.exforsys.com/tutorials/c-language/c-pointers.html
Wednesday, October 13, 2010
Format Specifiers for C
Format Specifiers for C
%c The character format specifier.
%d The integer format specifier.
%i The integer format specifier (same as %d).
%f The floating-point format specifier.
%e The scientific notation format specifier.
%E The scientific notation format specifier.
%g Uses %f or %e, whichever result is shorter.
%G Uses %f or %E, whichever result is shorter.
%o The unsigned octal format specifier.
%s The string format specifier.
%u The unsigned integer format specifier.
%x The unsigned hexadecimal format specifier.
%X The unsigned hexadecimal format specifier.
%p Displays the corresponding argument that is a pointer.
%n Records the number of characters written so far.
%% Outputs a percent sign.
Wednesday, October 6, 2010
functions in c programming
Introduction 1.
Using functions we can structure our programs in a more modular way, accessing all the potential that structured programming can offer to us in C++.
A function is a group of statements that is executed when it is called from some point of the program. The following is its format:
type name ( parameter1, parameter2, ...) { statements }
where:
A function is a group of statements that is executed when it is called from some point of the program. The following is its format:
type name ( parameter1, parameter2, ...) { statements }
where:
- type is the data type specifier of the data returned by the function.
- name is the identifier by which it will be possible to call the function.
- parameters (as many as needed): Each parameter consists of a data type specifier followed by an identifier, like any regular variable declaration (for example: int x) and which acts within the function as a regular local variable. They allow to pass arguments to the function when it is called. The different parameters are separated by commas.
- statements is the function's body. It is a block of statements surrounded by braces { }.
Introduction 2.
Functions are easy to use; they allow complicated programs to be parcelled up into small blocks, each of which is easier to write, read, and maintain. We have already encountered the function main and made use of I/O and mathematical routines from the standard libraries. Now let's look at some other library functions, and how to write and use our own.
Calling a Function
The call to a function in C simply entails referencing its name with the appropriate arguments. The C compiler checks for compatibility between the arguments in the calling sequence and the definition of the function.Library functions are generally not available to us in source form. Argument type checking is accomplished through the use of header files (like stdio.h) which contain all the necessary information. For example, as we saw earlier, in order to use the standard mathematical library you must include math.h via the statement
Writing Your Own Functions
A function has the following layout:return-type function-name ( argument-list-if-necessary ) { ...local-declarations... ...statements... return return-value; }If return-type is omitted, C defaults to int. The return-value must be of the declared type.
A function may simply perform a task without returning any value, in which case it has the following layout:
void function-name ( argument-list-if-necessary ) { ...local-declarations... ...statements... }Arguments are always passed by value in C function calls. This means that local ``copies'' of the values of the arguments are passed to the routines. Any change made to the arguments internally in the function are made only to the local copies of the arguments. In order to change (or define) an argument in the argument list, this argument must be passed as an address, thereby forcing C to change the ``real'' argument in the calling routine.
As an example, consider exchanging two numbers between variables. First let's illustrate what happen if the variables are passed by value:
#include < stdio.h> void exchange(int a, int b); void main() { /* WRONG CODE */ int a, b; a = 5; b = 7; printf("From main: a = %d, b = %d\n", a, b); exchange(a, b); printf("Back in main: "); printf("a = %d, b = %d\n", a, b); } void exchange(int a, int b) { int temp; temp = a; a = b; b = temp; printf(" From function exchange: "); printf("a = %d, b = %d\n", a, b); }
Run this code and observe that a and b are NOT exchanged! Only the copies of the arguments are exchanged. The RIGHT way to do this is of course to use pointers:
Introduction 3
Writing Functions
Functions divide a program into smaller manageable chunks. It's usual to group related functions into libraries and then use a header file to make public those functions. That way, other parts of your application can call them.Return Type
All functions have a return type. If you omit it, then int is assumed. So you should always specify the return type.A function can return nothing- just like a procedure in pascal. In C this is done with the void type. E.g.
void StartLogging() { // Logging Code } Return Value
Other than in a void function, a value of the function's return type must be returned. This is done with the return statement. This also exits the function. For a void function, return by itself will do.This adds two numbers and outputs C= 13.//ex6_11 #include <stdio.h>int addtwonumbers(int a,int b){ return a + b; }int main(){ int c = addtwonumbers(6,7) ;printf("C= %i ",c) ;return 0;}
Learn about Function Prototypes
Applications are made up of one or more source files. Because we want to group functions into libraries, we can specify functions before we fully implement them. This makes it possible for teams of developers working on a project to compile their code, with calls to external functions before those functions are written.
Compiling precedes Linking so a file can be compiled, even if it can't be linked into an application. If your source file includes a header file with prototypes of functions that you call, then you can compile your file. Function prototypes are one line stubs. Instead of the function block with code inside curly braces {}, it ends with a semi-colon.
Example
Taking the getpercent() function in example ex6_2 on the previous page, the prototype looks like this. double getpercent(double a, double b ) ; Prototypes enable the compiler to check the number and types of parameters before generating code. Before prototypes were introduced in ANSI C, earlier C compilers weren't strict about checking. As a programmer, you expect the compiler to spot silly mistakes like that, not generate faulty code! No Parameter Functions
If your function has no parameters, use the keyword void to specify this. The function InitializeData() has no parameters. int InitializeData( void ) ; void EndGame( char * Message) ; Note : InitializeData() returns an int but EndGame("Message") doesn't. Thursday, September 30, 2010
while / do while loop
#include <stdio.h>
main()
{
int i = 5;
int j = 5;
printf("%s\n"," --------------While loop ----------------------------");
while(i < 5)
{
printf("\n%s%d","value - ",i);
i = i + 1;
}
printf("%s\n"," ----------------do while loop ------------- ");
do {
printf(" \n Value - %d \n " , j);
j = j + 1;
}while(j < 5);
}
main()
{
int i = 5;
int j = 5;
printf("%s\n"," --------------While loop ----------------------------");
while(i < 5)
{
printf("\n%s%d","value - ",i);
i = i + 1;
}
printf("%s\n"," ----------------do while loop ------------- ");
do {
printf(" \n Value - %d \n " , j);
j = j + 1;
}while(j < 5);
}
SUM 1 to 2o
#include <stdio.h>
main()
{
int i = 1;
int s = 0;
while(i <=20)
{
s = s + i;
printf("%d%s",i, "+");
i = i + 1;
}
printf("\n%s%d \n\n","SUM IS - ",s );
}
main()
{
int i = 1;
int s = 0;
while(i <=20)
{
s = s + i;
printf("%d%s",i, "+");
i = i + 1;
}
printf("\n%s%d \n\n","SUM IS - ",s );
}
Numbers devide by 2 and 3
# include <stdio.h>
main()
{
int remainder = 0;
int startNumber = 500;
while(startNumber <750 )
{
startNumber = startNumber + 1;
if((startNumber % 2 == 0) &&(startNumber % 3 == 0) )
printf("%d\n",startNumber);
}
}
main()
{
int remainder = 0;
int startNumber = 500;
while(startNumber <750 )
{
startNumber = startNumber + 1;
if((startNumber % 2 == 0) &&(startNumber % 3 == 0) )
printf("%d\n",startNumber);
}
}
Find Max In a array
# include <stdio.h>
main()
{
int mx = 0;
int array[5];
array[0] = 32;
array[1] = 33;
array[2] = 34;
array[3] = 28;
array[4] = 28;
int i;
mx = array[0] ;
for (i=0 ; i<5 ; i++)
{
if(mx < array[i])
{
mx = array[i];
}
}
printf("%s%d\n","MAX = " , mx);
}
main()
{
int mx = 0;
int array[5];
array[0] = 32;
array[1] = 33;
array[2] = 34;
array[3] = 28;
array[4] = 28;
int i;
mx = array[0] ;
for (i=0 ; i<5 ; i++)
{
if(mx < array[i])
{
mx = array[i];
}
}
printf("%s%d\n","MAX = " , mx);
}
Wednesday, September 29, 2010
Question 2
| 1. # include <stdio.h> 2. main() 3. { 4. } |
\
Explain the purpose of line 1 (# include <stdio.h>) of the above source program.
(4 marks)stdio.h, which stands for "standard input/output header", is the header in the C standard library that contains macro definitions, constants, and declarations of functions and types used for various standard input and output operations.
http://en.wikipedia.org/wiki/Stdio.h
Question 1
Question 1
Human languages are not understood by the computer. The instructions the computers are understood called machine codes or machine language. And programming languages are a vocabulary and set of grammatical rules for instructing a computer to perform specific tasks.
a. What do you mean by a source program? (4 marks)
In computer science, source code is any collection of statements or declarations written in some human-readable computer programming language. Source code is the means most often used by programmers to specify the actions to be performed by a computer.
Program is written in the form of a number of text files using a screen editor. This form of the program is called the source program. It is not possible to execute this file directly.
b. What do you mean by an Object/executable Program? (4 marks)
A Program which contain instructions in computer understandable way.
Program is made by running a compiler which takes the typed source program and converts it into an object file that the computer can execute. A compiler usually operates in two or more phases (and each phase may have stages within it). These phases must be executed one after the other.
d. What is the difference between compiler and interpreter? (4 marks)
A compiler, in general, reads higher level language computer code and converts it to native machine code. An interpreter runs directly from source code or an interpreted code such as Basic or Lisp. Typically, compiled code runs much faster, is more compact and has already found all of the syntax errors and many of the illegal reference errors. Interpreted code only finds such errors after the application attempts to interpret the affected code. Interpreted code is often good for simple applications that will only be used once or at most a couple times, or maybe even for prototyping.
YES
Thursday, September 23, 2010
++ / --
# include <stdio.h>
main()
{
int i;
int j;
i = 3 ;
printf("%s%d\n" , "Value i " , i);
i++; /* i = i + 1*/
printf("%s%d\n" , "Value i++ " , i);
i--;/* i = i - 1*/
printf("%s%d\n" , "Value i-- " , i);
++i; /* i = i + 1*/
printf("%s%d\n" , "Value ++i " , i);
--i;/* i = i - 1*/
printf("%s%d\n" , "Value ++i " , i);
printf("%s" , "------------------------------------------------\n");
j = 10;
printf("%s%d\n" , "Value j++ " , j++);
printf("%s%d\n" , "Value j = " , j);
printf("%s%d\n" , "Value ++j " , ++j);
printf("%s%d\n" , "Value j-- " , j--);
printf("%s%d\n" , "Value j = " , j);
printf("%s%d\n" , "Value --j " , --j);
}
main()
{
int i;
int j;
i = 3 ;
printf("%s%d\n" , "Value i " , i);
i++; /* i = i + 1*/
printf("%s%d\n" , "Value i++ " , i);
i--;/* i = i - 1*/
printf("%s%d\n" , "Value i-- " , i);
++i; /* i = i + 1*/
printf("%s%d\n" , "Value ++i " , i);
--i;/* i = i - 1*/
printf("%s%d\n" , "Value ++i " , i);
printf("%s" , "------------------------------------------------\n");
j = 10;
printf("%s%d\n" , "Value j++ " , j++);
printf("%s%d\n" , "Value j = " , j);
printf("%s%d\n" , "Value ++j " , ++j);
printf("%s%d\n" , "Value j-- " , j--);
printf("%s%d\n" , "Value j = " , j);
printf("%s%d\n" , "Value --j " , --j);
}
Nested Loops (2010 09 23)
# include <stdio.h>
main()
{
int i;
int j;
for(i = 1 ; i<=12 ; i++)
{
printf("%s%d\n"," ------------------ " , i);
for(j=1 ; j <=12 ;j++)
{
printf("%d%s%d%s%d\n",i," X ",j ," = " , i*j );
}
}
}
main()
{
int i;
int j;
for(i = 1 ; i<=12 ; i++)
{
printf("%s%d\n"," ------------------ " , i);
for(j=1 ; j <=12 ;j++)
{
printf("%d%s%d%s%d\n",i," X ",j ," = " , i*j );
}
}
}
Arrays (2010 09 23)
An array is a series of elements of the same type placed in contiguous memory locations that can be individually referenced by adding an index to a unique identifier.
Ref Links -
http://www.idleloop.com/tutorials/introC/introC-10.php
Samples -
----------------------------------------------------------------------------------------------
# include <stdio.h>
main()
{
int array[5];
array[0] = 32;
array[1] = 33;
array[2] = 34;
array[3] = 28;
array[4] = 28;
printf("%s%d\n" , "array[0] - " , array[0] );
printf("%s%d\n" , "array[1] - " , array[1] );
printf("%s%d\n" , "array[2] - " , array[2] );
printf("%s%d\n" , "array[3] - " , array[3] );
printf("%s%d\n" , "array[4] - " , array[4] );
printf("----------------------------------------\n");
int i;
for (i=0 ; i<5 ; i++)
{
printf("%s%d%s%d\n" , "array[ " , i , "] = " ,array[i]);
}
printf("---------------array2[] -----------------\n");
int array2[] = {32,33,34,28,28};
for (i=0 ; i<5 ; i++)
{
printf("%s%d%s%d\n" , "array2[ " , i , "] = " ,array2[i]);
}
printf("---------------array3[] -----------------\n");
int array3[10];
array3[5] = 100;
for (i=0 ; i<10 ; i++)
{
printf("%s%d%s%d\n" , "array3[ " , i , "] = " ,array3[i]);
}
printf("---------------array4[] -----------------\n");
double array4[10];
}
-----------------------------------------------------------------------------------------------
TO DO - find the maximum stored in a array
# include
main()
{
int array[5];
array[0] = 32;
array[1] = 33;
array[2] = 34;
array[3] = 28;
array[4] = 28;
int i;
for (i=0 ; i<5 ; i++)
{
printf("%s%d%s%d\n" , "array[ " , i , "] = " ,array[i]);
}
}
Ref Links -
http://www.idleloop.com/tutorials/introC/introC-10.php
Samples -
----------------------------------------------------------------------------------------------
# include <stdio.h>
main()
{
int array[5];
array[0] = 32;
array[1] = 33;
array[2] = 34;
array[3] = 28;
array[4] = 28;
printf("%s%d\n" , "array[0] - " , array[0] );
printf("%s%d\n" , "array[1] - " , array[1] );
printf("%s%d\n" , "array[2] - " , array[2] );
printf("%s%d\n" , "array[3] - " , array[3] );
printf("%s%d\n" , "array[4] - " , array[4] );
printf("----------------------------------------\n");
int i;
for (i=0 ; i<5 ; i++)
{
printf("%s%d%s%d\n" , "array[ " , i , "] = " ,array[i]);
}
printf("---------------array2[] -----------------\n");
int array2[] = {32,33,34,28,28};
for (i=0 ; i<5 ; i++)
{
printf("%s%d%s%d\n" , "array2[ " , i , "] = " ,array2[i]);
}
printf("---------------array3[] -----------------\n");
int array3[10];
array3[5] = 100;
for (i=0 ; i<10 ; i++)
{
printf("%s%d%s%d\n" , "array3[ " , i , "] = " ,array3[i]);
}
printf("---------------array4[] -----------------\n");
double array4[10];
}
-----------------------------------------------------------------------------------------------
main()
{
int array[5];
array[0] = 32;
array[1] = 33;
array[2] = 34;
array[3] = 28;
array[4] = 28;
int i;
for (i=0 ; i<5 ; i++)
{
printf("%s%d%s%d\n" , "array[ " , i , "] = " ,array[i]);
}
}
Subscribe to:
Comments (Atom)