//////////Variable Pointer/////////////
Output value at the address
#include <stdio.h>
int main(void)
{
int number = 0;
int *pointer = NULL;
number = 10;
pointer = &number;
printf("\npointer's value: %p", pointer); /* Output the value (an address) */
printf("\nvalue pointed to: %d\n", *pointer); /* Value at the address */
return 0;
}
/////////Put values in the memory locations by using pointers////////
#include <stdio.h>
main(){
int a[5];
int *b;
int *c;
int i;
for(i = 0;i<5;i++){
a[i]=i;
}
for(i = 0;i<5;i++) {
printf("value in array %d\n",a[i]);
}
b=a;
b++;
*b=4;
b++;
*b=6;
b++;
*b=8;
b++;
*b=10;
b++;
*b=12;
printf("after\n\n\n");
for(i = 0;i<5;i++) {
printf("value in array %d\n",a[i]);
}
}
///////////COMMENTS////////////
Adding Comments
Comments in a C program have a starting point and an ending point. |
Everything between those two points is ignored by the compiler.
/* This is how a comment looks in the C language */
|
The beginning of the comment is marked by the slash and the asterisk: /*. |
The end of the comment is marked by the asterisk and the slash: */. |
#include <stdio.h>
int main(){
/* This is the comment.*/
printf("%15s","right\n");
printf("%-15s","left\n");
return(0);
}
////////Using Comments to Disable///////
#include <stdio.h>
int main(){
/* printf("%15s","right\n"); */
printf("%-15s","left\n");
return(0);
}
The comments are enclosed in '/*...*/'
#include <stdio.h>
main(){
printf("Hi Amber \n"); /* This is the comments.*/
}
////Source code header comments/////////
- At the beginning of the program is a comment block.
- The comment block contains information about the program.
- Boxing the comments makes them stand out.
|
The some of the sections as follows should be included. |
- Heading.
- Author.
- Purpose.
- Usage.
- References.
- File formats. A short description of the formats which will be used in the program.
- Restrictions.
- Revision history.
- Error handling.
- Notes. Include special comments or other information that has not already been covered.
|
/********************************************************
* hello -- program to print out "Hello World". *
* *
* Author: FirstName, LastName *
* *
* Purpose: Demonstration of a simple program. *
* *
* Usage: *
* Runs the program and the message appears. *
********************************************************/
#include <stdio.h>
int main()
{
/* Tell the world hello */
printf("Hello World\n");
return (0);
}
/////CONVENTION/////
Indentation and Code Format
while (! done) {
printf("Processing\n");
next_entry();
}
if (total <= 0) {
printf("You owe nothing\n");
total = 0;
} else {
printf("You owe %d dollars\n", total);
all_totals = all_totals + total;
} |
|
In this case, curly braces ({}) are put on the same line as the statements. |
The other style puts the {} on lines by themselves: |
while (! done) { printf("Processing\n");
next_entry(); }
if (total <= 0) {
printf("You owe nothing\n");
total = 0;
} else {
printf("You owe %d dollars\n", total);
all_totals = all_totals + total;
} |
|
////Header Files////
#include <stdio.h>
int main(void)
{
printf("\nBe careful!!\a");
return 0;
}
////////Common Headers //////////
Header | Purpose |
assert.h | Defines the assert() macro |
ctype.h | Character handling |
errno.h | Error reporting |
float.h | Defines implementation-dependent floating-point limits |
limits.h | dependent limits |
locale.h | Localization |
math.h | math library |
setjmp.h | Nonlocal jumps |
signal.h | Signal handling |
stdarg.h | Variable argument lists |
stddef.h | Constants |
stdio.h | I/O system |
stdlib.h | Miscellaneous declarations |
string.h | string functions |
time.h | system time functions
|
//////Headers Added by C99///////////
Header | Purpose |
complex.h | complex arithmetic. |
fenv.h | Gives access to the floating-point status flags and other aspects of the floating-point environment. |
inttypes.h | Defines a standard, portable set of integer type names. Also supports functions that handle greatest-width integers. |
iso646.h | Added in 1995 by Amendment 1. Defines macros that correspond to various operators, such as && and ^. |
stdbool.h | Supports Boolean data types. Defines the macro bool, which helps with C++ compatibility. |
stdint.h | Defines a standard, portable set of integer type names. This file is included by . |
tgmath.h | Defines type-generic floating-point macros. |
wchar.h | multibyte and wide-character functions. |
wctype.h | multibyte and wide-character classification functions |
////////External references//////////
Extern definition is used when referencing a function or variable defined outside.
// Program in file externa1.c
#include <stdio.h>
#include <c:\f1.cpp>
extern int i;
main()
{
i =0;
printf("value of i %d\n",i);
}
// Program in file f1.cpp
int i =7;
//////////////GCC/////////////
Compile and link the test.c source code:
gcc filename.c -o filename
#include <stdio.h>
int main()
{
printf("Goodbye!\n");
return(0);
}
Comments
Post a Comment