Browsing all articles in Tips
Apr
20

Little endian and Big endian in C programming

This is the most complex concept…. i have read enough on net and some books but   still i was not getting this concept. i was not understanding how this will effect my c code. But i have did some  experiments with some example code and tried to understand things. and here now i am going to share those examples.

In computing, the term endian or endianness refers to the ordering of individually addressable sub-components within the representation of a larger data item as stored in external memory (or, sometimes, as sent on a serial connection)

You can understand basic things about at 

Little endian and Big endian

Big-endian

Feb
27

C programming interview questions and answers for freshers

i have seen that in c programming based  interview most of the question are based on some concept clear question and answer.

i have been posting that all tips on this ShareProgrammingTips but here i am just going to List down some good question on C programming.

1>

How will you set, clear ,toggle and check a single bit in C?

2>

Do you know the Concept of Octal literal in C

3>

Do you know the difference between all standard version of c language

4>

How will you detect memory leakage in C program?

5>

Do you know about Implementation limitation of c programming language

read more

Feb
26

What’s the need of using encapsulation { do..while(0) block } in define a macro in C programming?

While reading many codes in c programming based open source project i came to know one interesting things. Let me explain you by some sample code.

#define Assert(v)   if(!(v)) printf("Error occurred at: %s, in %s at %i",  #v, __FILE__, __LINE__);

At most of the experienced people used to write such macro in following style

 #define Assert(v)   do { if(!(v)) printf("Error occurred at: %s, in %s at %i",  #v, __FILE__, __LINE__); } while(0)

This method is called as making encapsulating our macro.

Now let me explain you whats the need of this….

read more

Feb
22

Short-circuiting in C Programming Expressions………….!!!

Author JIGAR PATEL    Category c, Tips     Tags

One Superb thing i learn today is  Short-circuiting in C Programming Expressions. 

Let me explain you by code

#include <stdio .h>
int main() {
   int a = 0;
   int b = 1;
   int c = 2;
   int d = 3;
   a = ++b && ++c || ++d;
   printf ("b = %d,  c = %d, d = %d, a = %d", b, c, d, a);
   return 0;
}</stdio>

if you think output of this program will be

b = 2, c = 3, d = 4, a = 1

then you need to read this whole post..!
read more

Feb
19

In embedded domain, Copying of structure is why avoided in c programming?

 

 

Generally  In embedded software domain for copying structure of same type people dont prefer to use direct assignment and do that by memcpy function or by doing assignment of each element of structure.

Look at Example

struct student {
int roll_no;
int age;
};
struct student exmple1 = {32,15};
struct student exmple2;

for copying exmple1 into exmple2.. instead of writing direct

exmple2=exmple1;

people like to use

memcpy(exmple2,exmple1,sizeof(struct student));

or

exmple2.roll_no=exmple1.roll_no;
exmple2.age=exmple1.age;
Why?

read more

Feb
11

How to change the buffering option for any specified stream in c programming?

Author JIGAR PATEL    Category c, Tips     Tags

Many of  us faced this problem. Sometimes we put some debug prints in our code this way

fprintf(logfile,"successfully reached at debug-point 1\n"); 
 
some code is here
 
fprintf(logfile,"successfully reached at debug-point 2"); 
 
Here segmantaion fault occur due to some reason

Now in this condition only debug-point1 will be print on logfile debug-point 2 print was written to buffer to be used for I/O operations with the specified stream logfile.  but its not flushed because it didnt get \n  and not written on logfile and program crash  so we thinks that crash occur after debug-point1

read more

Feb
7
Comments Off

Implement Timeout for one function in C

Author saurabh    Category c, sample programme, Tips     Tags

Here i have one function which is listen mode. this function listing something which i got form some device.

Here when my function is in listen mode that time i want to create timeout. if i will not get any response from particular device than i want o exit from this function and have to notify.

if during this timeout period if i will get some date from something or some action would be performed than i have to continue with work and stop this timeout.
read more

Jan
30

Convert file descriptor to file pointer | Convert file pointer to file descriptor in Linux by C programming

 If you all ready know the difference between file descriptor and file pointer then good otherwise please read this post

Whats is difference between file descriptor and file pointer?

 

 

File descriptor to file pointer :

for converting file descriptor to file pointer in linux fdopen() function is used

#include <stdio.h
FILE *fdopen(int fildes, const char *mode);

fildes is file descriptor

mode is the character value which indicate the mode of file operation (same as we use in fopen() function)

Example :

#include <stdio .h>
#include <fcntl .h>
 
int main(){
int fd;
FILE *fp;
fd = open("shareprogrammingtips.txt",O_WRONLY | O_CREAT | O_TRUNC);
if(fd<0)
{
   printf("open call fail");
   return -1;
}
 
fp=fdopen(fd,"w");
fprintf(fp,"we got file pointer fp bu using File descriptor fd");
fclose(fp);
close(fd);
return 0;
}
</fcntl></stdio>

read more

Jan
7

Structure of a C-Program in Memory | How Heap,Stack,Data and Code segments are stored in memory?

Author JIGAR PATEL    Category c, Tips     Tags

This is most confusing question for and beginner of c programming language.  They dont know the all segment name and dont know on which segment which data are going to store . So here i am just giving you the brief idea about the structure of any c program in memory.  

This structure can be divided in 3  parts .

  • Data Segment
    • Initialized data segment (initialized to explicit initializers by programmers)
    • Uninitialized data segment (initialized to zero data segment – BSS [Block Start with Symbol])
  • Code Segment
  • Stack and Heap areas

read more

Dec
25

Most common directory layout seen in all C/C++ based Project/Package/Library

Author JIGAR PATEL    Category c, c++, projects, projects, Tips, tips     Tags

I have been with lots of projects package or library written in C or C++ programming language has One common directory layout. So if you planning to design any new package /project/library in C or C++ programming language then this will really help you to design directory layout of your project.

read more

Follow us on Twitter! Follow us on Twitter!

Search in this website

our sponsors

latest comments

Find us on Facebook

Top Authors

35 posts
saurabh
22 posts
10 posts

Find us on stackoverflow

Polls

Tell us who you are

View Results

Loading ... Loading ...

My Bookmarks

Sponsers Link