C Programming For Dummies

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

C Programming For Dummies

http://see-programming.blogspot.com/[18-Mar-14 4:46:59 PM]


see-programming is a popular blog that provides information on C programming basics, data structure, advanced
unix programming, network programming, basic linux commands, interview question for freshers, video tutorials and
essential softwares for students.
C Programming For Dummies
CTutorials Pointers Programs C Graphics DataStructures C Library
SATURDAY, 18 J ANUARY 2014
Operator precedence and associativity in C language
[ ]
( )
.
->
++--
Array subscript
Function Call
Structure reference
Structure dereference
Postfix increment/Postfix decrement
Left to right
++--
+-
! ~
(type)
* &
sizeof
Prefix increment/Prefix decrement
Unary plus/Unary minus
Logical negation/One's complement
Typecast operator
Pointer dereference/Address of
Size of type/variable in bytes
Right to left
* / % Multiplication/Division/Modulo Left to Right
+- Addition/Subtraction Left to Right
<< >> Bitwise left shift/ Bitwise right shift Left to Right
< >
<=
>=
Comparison less than/Comparision greater than
Comparison less than or equal to
Comparison greater than or equal to
Left to Right
== != Comparison equal to/Comparison not equal to Left to Right
& Bitwise AND Left to Right
^ Bitwise XOR Left to Right
| Bitwise OR Left to Right
&& Logical AND Left to Right
|| Logical OR Left to Right
?: Ternary Conditional Operator Right to Left
=
*= /= %=
+= -=
<<= >>=
Assignment Operator
Mulplication/division/modulo assignment
Addition/Subtraction assignment
Bitwise left shift/right shift assignment
Right to Left
SUBSCRIBE
Analog clock
Digital Clock
Solar System
Moving Fish Animation
Tic Tac Toe Game
Moving Car animation
ANIMATION USING C GRAPHICS
BESTSELLERS
The Online Megastore
Buy Now
WD My Passport Ultra
2.5 inch 1...
List Price Rs.20000
Our Price Rs.4170
LIBRARIES
Home
Share
2
More Next Blog Create Blog Sign In
Search
Submit
C Programming For Dummies
http://see-programming.blogspot.com/[18-Mar-14 4:46:59 PM]
Posted by CProgrammer at 14:37 0 comments
Labels: C Tutorials
&= ^=
|=
Bitwise AND/XOR assignment
Bitwise OR assignment
, Comma Operator Left to Right
Recommend this on Google
SUNDAY, 12 J ANUARY 2014
Posted by CProgrammer at 22:27 0 comments
Labels: C Tutorials
#ifdef, #ifndef and #endif directives in C
#ifdef
If the named macro is defined, then the code between #ifdef and #endif will be
compiled.
#ifndef
If the named macro is not defined, then the code between #ifndef and #endif will
be compiled.
#ifdef, #ifndef and #endif example in C
Output:
jp@jp-VirtualBox:~/$ ./a.out
#ifdef: Value of NAME is Raj
#ifndef: macro VAL is not defined
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdio.h>
#define NAME "Raj"

int main() {

#ifdef NAME
printf("#ifdef: Value of NAME is %s\n", NAME);
#endif

#ifndef VAL
printf("#ifndef: macro VAL is not defined\n");
#endif
return 0;
}
Recommend this on Google
#if, #elif, #else and #endif example
#if
If the resultant value of the arithmetic expression is non-zero, then the code
between #if and #endif will be compiled.
Example:
#if 10 >5
printf("10 is greater than 5");
assert.h (1)
ctype.h (13)
execinfo.h (3)
getopt.h (2)
math.h (22)
setjmp.h (1)
signal.h (2)
stdarg.h (1)
stdio.h (41)
stdlib.h (23)
string.h (20)
time.h (9)
unistd.h (1)
Blog Archive
BLOG ARCHIVE
Philips SHE1360/97 Wi...
Rs.150
SanDisk MicroSDHC 8
G...
Rs.353
Rs.243
SanDisk Cruzer Blade...
Rs.899
Rs.275
PAGEVIEWS
2 3 0 1 0 7
Micromax Canvas
Magnu...
Rs.14999
Rs.13990
Micromax Canvas
Turbo...
Rs.19878
Rs.14415
Moto G
Rs.12999
Rs.12499
Blog Archive
C Programming For Dummies
http://see-programming.blogspot.com/[18-Mar-14 4:46:59 PM]
#endif
The code between #if and #endif will be compiled since the resultant value of the
expression(10 > 5) is non-zero.
#elif
This provides an alternate expression to evaluate
Example:
#if 10 <5
printf("10 is less than 5");
#elif 10 >5
printf("10 is greater than 5");
#endif
The expression at #if directive is evaluates to 0. So, the expression at #elif is
evaluated. If it is non-zero, then the code between #elif and #endif will be
compiled.
#else
If the resultant value of the arithmetic expression is false for #if, #ifdef or #ifndef,
then the code between #else and #endif will be compiled.
Example:
#if 10 <5
printf("10 is less than 5");
#else
printf("10 is greater than 5");
#endif
#endif
This acts as an end directive for #if, #ifdef, #ifndef, #elif or #if
#if 100 <50 // resultant value of expression is 0
printf("#if directive");
#elif 50 <10 // resultant value of the expression is 0
printf("#elif directive");
#else
printf("#else directive"); // this statement would be executed
#endif
#include <stdio.h>
#define VAL1 10
#define VAL2 20
#define VAL3 30
int main() {
#if VAL1 > VAL2
printf("%d is greater than %d\n", VAL1, VAL2);
#elif VAL1 > VAL3
printf("%d is greater than %d\n", VAL1, VAL3);
#else
printf("%d is less than %d and %d\n", VAL1, VAL2, VAL3);
#endif
return 0;
}
Output:
C Programming For Dummies
http://see-programming.blogspot.com/[18-Mar-14 4:46:59 PM]
Posted by CProgrammer at 21:51 0 comments
Labels: C Tutorials
jp@jp-VirtualBox:~/$ ./a.out
10 is less than 20 and 30
Recommend this on Google
SATURDAY, 11 J ANUARY 2014
How to access union members in C
Dot operator is used to access(or assign) the data members of a union variable.
Consider the following example,
union value {
int x;
float y;
};
union value obj ;
Here, obj is a union variable of type union value. x and y are data members of the
union value. Let us see how to access data members of union using dot operator.
obj.x = 10;
The above statement assigns value 10 to the data member x of the union variable
obj.
obj - object name(union variable name)
x - data member in union value
. - dot operator
Example C program to illustrate accessing union members using dot operator
#include <stdio.h>
union value {
int x;
float y;
}obj;
int main() {
obj.x = 5;
printf("Value of x is %d\n", obj.x);
obj.y = 19.22;
printf("Value of y is %f\n", obj.y);
printf("Value of x is %d\n", obj.x);
return 0;
}
Output:
jp@jp-VirtualBox:~/$ ./a.out
Value of x is 5
Value of y is 19.219999
Value of x is 1100595855
The value of data member y overwrites the value of x and that is reason for junk
C Programming For Dummies
http://see-programming.blogspot.com/[18-Mar-14 4:46:59 PM]
Posted by CProgrammer at 18:11 0 comments
Labels: C Tutorials
value in x. Basically, all data members in a union shares same storage place.
Recommend this on Google
How to initialize union variables in C
Let us see all possible ways to initialize a union variable. Please remember that we
can access one data member at a time in the case of union.
Method 1:
union value {
int x;
float y;
};
union value obj ={5};
Here, initialization is done after union definition. 5 is assigned to the member x of
union variable obj .
Method 2:
union value {
int x;
float y;
}obj ={5};
Here, initialization is done while defining the union. 5 is assigned to the
member x of union variable obj .
Method 3:
union value {
int x;
float y;
};
union value obj ={.y =5.0};
Here, initialization is done after union definition. 5.0 is assigned to the member
y of union variable obj . And we have used dot(.) operator for initialization.
Method 4:
union value {
int x;
float y;
};
union value obj ={y:5.0};
This method is similar to method 3. Here, color(:) is used instead of '=' and period
before union member is removed
Example C program to illustrate union variable initialization:
#include <stdio.h>
union value {
int x;
float y;
}obj1 = {5}; //initializing union variable
int main() {
C Programming For Dummies
http://see-programming.blogspot.com/[18-Mar-14 4:46:59 PM]
Subscribe to: Posts (Atom)
Older Posts Home
Posted by CProgrammer at 17:42 0 comments
Labels: C Tutorials
/* declaration and initialization for union variables */
union value obj2 = {6};
union value obj3 = {.y = 7.1};
union value obj4 = {y:8.2};
/* printing the values */
printf("obj1.x : %d\n", obj1.x);
printf("obj2.x : %d\n", obj2.x);
printf("obj3.y : %f\n", obj3.y);
printf("obj4.y : %f\n", obj4.y);
return 0;
}
Output:
jp@jp-VirtualBox:~/$ ./a.out
obj1.x : 5
obj2.x : 6
obj3.y : 7.100000
obj4.y : 8.200000
Recommend this on Google
Arrays Programs (78) Basic C Programs (77) C Graphics (44) C Library (127) C Tutorials (108)
control flow programs (71) Conversions (34) ctype.h (13) File Programs (43) Function programs (15)
graphics.h (78) Logical operator programs (17) math.h (22) Pointer Programs (22) pointers (34) recursion (29) series
programs (31) stdio.h (41) stdlib.h (23) string.h (20) Strings (63) Structure programs (10) time.h (9)
MY BLOG CONTENTS
assert.h (1)
execinfo.h (3) faqs (3) getopt.h (2)
macros programs (1)
setjmp.h (1) signal.h (2) stdarg.h (1) unistd.h (1)
CProgrammer. Picture Window template. Template images by merrymoonmary. Powered by Blogger.

You might also like