----------------------------------------------------------------------------------
int add( int, int); /* Function declaration */
main()
{
int i=1;
printf("i starts out life as %d.", i);
i = add(1, 1); /* Function call */
printf(" And becomes %d after function is executed.\n", i);
}
/************************************************************************/
int add( int a, int b) /* Function definition */
{
int c;
c = a + b;
return c;
}
-----------------------------------------------------------------------------------------
int counter = 0; /* global because we are outside
all blocks. */
int func(void);
main()
{
counter++; /* global because it has not been
declared within this block */
printf("counter is %2d before the call to func\n", counter);
func(); /* call a function. */
printf("counter is %2d after the call to func\n", counter);
}
int func(void)
{
int counter = 10; /* local. */
printf("counter is %2d within func\n", counter);
}
--------------------------------------------------------------------------------------------
Your first C program.
#include
main()
{
puts ("your first C program");
}
---------------------------------------------------------------------------------
#include
main()
{
int i=1; /* Define an integer variable. */
/*
* i == 1 expression is evaluated. If TRUE the
* first block is executed.
* if i == 1 is FALSE the optional else block
* is executed */
if (i == 1)
{
puts ("i is equal to 1\n");
}
else
{
puts("i is NOT equal to 1");
}
}
---------------------------------------------------------------------------
#include
main()
{
int i=1; /* Define an integer variable. */
/*
* i <= 10 expression is evaluated. If TRUE the
* block is executed.
*/
while (i <= 10)
{
printf ("i is %i\n", i);
i++;
}
}
-----------------------------------------------------------------
#include
main()
{
int i=1; /* Define an integer variable. */
/*
* The block is executed. Then the i <= 10
* expression is evaluated. If TRUE the block
* is executed again.
*/
do
{
printf ("i is %i\n", i);
i++;
} while (i <= 10);
}
-----------------------------------------------------------------------
#include
main()
{
int i; /* Define an integer */
/*
* i=1 is executed the first time into the loop.
* i<=10 is then tested, if true, the block is executed.
* ++1 is the increment, before i<=10 is retested.
*/
for (i=1; i<=10; ++i)
{
printf ("loop counter = %i\n", i);
}
}
-----------------------------------------------------------
#include
main()
{
int i,j; /* Define integers */
/* 'i' and 'j' get initalised on the 'for'.
* Then they both are incremented and decremented
* before 'i' is tested.
*/
for (i=1, j=10; i<=10; ++i, --j)
{
printf (" i = %02d j = %02d\n", i, j);
}
}
/************************************************************************
O/P will look like this:
i = 01 j = 10
i = 02 j = 09
i = 03 j = 08
i = 04 j = 07
i = 05 j = 06
i = 06 j = 05
i = 07 j = 04
i = 08 j = 03
i = 09 j = 02
i = 10 j = 01
**************************************************************************/
-------------------------------------------------------------------
main(int argc, char *argv[])
{
switch (argc) /* Switch evaluates an expression (argc) */
{
/* If expression resolves to 1, jump here */
case 1:
puts("Only the command was entered.");
break; /* break - cases the execution to jump
out of the 'switch' block. */
/* If expression resolves to 2, jump here */
case 2:
puts("Command plus one parm entered");
break;
/* If expression resolves to 3, jump here */
case 3:
puts("Command plus two parm entered");
break;
/* Any other value jumps here. */
default:
printf("Command plus %d parms entered\n", argc-1);
break;
}
}
------------------------------------------------------------------
main()
{
/*
* ++i - i incremented before i is used.
* --i - i decremented before i is used.
* j++ - j is incremented AFTER j has been used.
* j-- - j is decremented AFTER j has been used.
*/
int i=1,j=1;
puts("\tDemo 1");
printf("\t%d %d\n",++i, j++); /* O/P 2 1 */
printf("\t%d %d\n",i, j); /* O/P 2 2 */
i=1;j=1;
puts("\n\tDemo 2");
printf("\t%d \n",i=j++); /* O/P 1 */
printf("\t%d \n",i=++j); /* O/P 3 */
/************************************************************************
This is a GOT YA
*************************************************************************/
/* Consider this code */
i = 0; j = 0;
puts("\n\tDemo 3");
if ( (i++ == 1) && (j++ == 1)) puts("Some text");
/* Will i and j get incremented? The answer is NO! Because
* the expression in the left of '&&' resolves to false the
* compiler does NOT execute the expression on the right and
* so 'j' does not get executed!!!!! */
printf("\t%d %d\n",i, j); /* O/P 1 0 */
}
--------------------------------------------------------------------------------
main(int argc, char *argv[])
{
int count;
/* Main takes two variables 'argc' is the number of parms on the
* command line and 'argv' is a pointer to each of the parameters.
*
* int argc -- integer number called 'argc'
* char *argv[] -- Character pointer array!
*/
printf("%i parameters entered on the command line.\n", argc);
/*
* progname argc = 1
* progname parm1 parm2 argc = 3
*/
/*
* We take 1 from argc because
* the argv array starts at zero
* an ends at argc -1
*/
for ( count = 0; count <= argc -1 ; count++)
{
/* printf expects a pointer
* to the text
*/
printf("parm %d is %s\n", count, argv[count]);
}
}
----------------------------------------------------------------------
/* version 1 */
#include
main()
{
int a=10;
while (a >= 1)
{
printf("a is %2d\n",a);
a=a-1;
}
}
/*******************************************************************/
/* version 2 */
main()
{
a =11;
while (0 < (a = a - 1) )
{
printf("a is %2d\n",a);
}
}
/*******************************************************************/
/* version 3 */
main()
{
/*
* This works because the 'while' loop
* will keep spinning while 'i' is TRUE.
* That is while 'i' is NOT equal to 0
*/
a=11;
while (i)
{
printf ("%i",i=i-1);
}
}
-------------------------------------------------------------------
#include
main()
{
int c; /* Character read from the file. */
FILE *ptr; /* Pointer to the file. FILE is a
structure defined in
/* Open the file - no error checking done */
ptr = fopen("/etc/hosts","r");
/* Read one character at a time, checking
for the End of File. EOF is defined
in
while ((c = fgetc(ptr)) != EOF)
{
printf("%c",c); /* O/P the character to the screen */
}
fclose(ptr); /* Close the file. */
}
--------------------------------------------------------
#include
main()
{
char text1[20]="martin"; /* string buffer */
char text2[20]="leslie"; /* string buffer */
printf (" original string contents are: %s\n", text1);
/* Copy text2 into text1.
If text1 is smaller that text2
it will probably overwrite
something! */
strcpy(text1, text2);
printf (" new string contents are: %s\n", text1);
strcpy(text1, "linux");
printf (" final string contents are: %s\n", text1);
}
-------------------------------------------------------------------------
#include
main()
{
int i;
char buffer[80]; /* work buffer */
initscr(); /* initialize the screen */
printw("Please enter a password => "); /* update screen image */
refresh(); /* Update screen with screen image */
noecho(); /* Suppress echo to the screen */
/* Read characters until C/R */
while((buffer[i] = getch()) != '\n') i++;
printw("\nPassword is %s - press return to continue.",buffer);
refresh();
getch();
endwin(); /* Shut down curses */
}
-----------------------------------------------------------------------
No comments:
Post a Comment