Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Meaning of statement //while (s && *s)send_char (*s++);

Status
Not open for further replies.

dattlara76

Junior Member level 2
Joined
Mar 1, 2019
Messages
21
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
291
Hi,

Below part code for 16x2 LCD,


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
------------------
 
void send_string(const char *s)         //send a string to display in the lcd
{          
     while (s && *s)send_char (*s++);
}
 
---------------------
 
void send_char(unsigned char data)      //send lcd character
 
{ 
 
..loop
 
}
 
----------------------

I trying to understand how the code //while (s && *s)send_char (*s++); is executed ?? if the argument is passed like "ABCD" ????

I only known the statement in c that //while(), but what is the meaning of that while()with the passing argument by calling function ..ie, //while (s && *s)send_char (*s++);

could you please clear anybody. and can you share a link whare such statement is exist please.

tnx
 

If '&&' is logical-AND and '*s' is "not-s" then the logic
is "never" and nothing happens.

I don't know much of nuthin' about C programming.
 

The function will send all characters in a string, one at a time. The function will do nothing if it gets a null pointer.

The first " s " is false if a null pointer was sent to the function, and nothing will be done. It will be checked every turn in the loop, but it would be enough to check it once before the loop.
The second " *s " is a check for the end of the string. It will be false when all characters in the string have been processed, because the #next character" will be zero.
" *s++ " is just a compact way to take the current character in the string and then increment the pointer so it points to the next character.


A null pointer is illegal to use, it will normally trigger a segmentation fault if the software runs on a linux system.
A null character is used to indicate the end of a string.
 

it would be enough to check it once before the loop.

You can save a few cycles by writing


Code C - [expand]
1
2
3
4
5
void send_string(const char *s)    
{
     if (s)          
        while (*s)send_char (*s++);
}

 

tnx all for the reply..
i need one more eplanation please,
while loop syntex is as i know,
WHILE(CONDITION),//abd if condition true then enetr in body of the loop.
but here in the statement //while (s && *s)send_char (*s++);
this line(function) is also included.
will not it be syntex ERROR of while loop??
tnx
 

I think it's time to get a C text book or tutorial. Please notice that there are two loop constructs involving while:

while(<expression>) <statement>;

and

do <statement> while(<expression>);
 

this line(function) is also included.

Newlines and indentation don't matter for C. It doesn't matter to the compiler if send_char(*s++) is on its own line or not.

(excludes the C preprocessor where newlines do end a preprocessor directive.
 

If there is only one statement in the loop, it can be written immediately after while(), without brackets.
A good advice is to always use brackets {}, even when they aren't necessary.

Unlike python, C source can be divided into lines in any way.
There are even competitions for writing the "best" C program in one line.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top